Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing row value of JTable in Pagination
    text
    copied!<p>i have JTable as below. it shows 30 records for a page. if i click on 2nd row in 2nd page. myTabel.getSeletedRow() passing value of '2', instead of '34'. How to achieve it? </p> <pre><code>public class InvestTable extends JFrame { JTable myTable; JLabel total; JButton update,print,delete,search,search2,search3,export; String tablename = "investment"; JPanel p,panel; MyTableModel tm; JScrollPane myPane; ImageIcon img = new ImageIcon("Images\\investment.png"); Connection con; Statement stat,st; TableModel model; DefaultTableModel model1; TableRowSorter&lt;MyTableModel&gt; sorter; ResultSet rs, result; private String tot; private int pageSize = 10; Box box = Box.createHorizontalBox(); Box box1 = Box.createHorizontalBox(); ForRadioButtonUI ui = new ForRadioButtonUI(); InvestTable() throws SQLException { super("Investment Table"); update = new JButton("Update"); print = new JButton("Print"); search = new JButton("Search By Date"); search2 = new JButton("Search By Name"); search3 = new JButton("Search By Invest Type"); export = new JButton("Export"); total = new JLabel(""); delete = new JButton("Delete"); p = new JPanel(null); panel = new JPanel(null); tm = new MyTableModel(tablename); myTable = new JTable(tm); model = myTable.getModel(); sorter = new TableRowSorter&lt;MyTableModel&gt; (tm); myTable.setRowSorter(sorter); showPages(30, 1); ------------------&gt; // METHOD FOR PAGINATION myTable.setIntercellSpacing(new Dimension()); myTable.setModel(tm); myPane = new JScrollPane(myTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); myTable.setSelectionForeground(Color.black); myTable.setSelectionBackground(Color.green); myTable.setAlignmentX(CENTER_ALIGNMENT); myTable.setAlignmentY(CENTER_ALIGNMENT); myTable. setPreferredSize(new Dimension(800,482)); myTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); total.setFont(new Font("Arial", Font.BOLD, 16)); p.setPreferredSize(new Dimension(1024, 700)); panel.setBounds(810,10,200,768); myPane.setBounds(0,0,800,505); update.setBounds(10,10,100,30); print.setBounds(10,50,100,30); search.setBounds(10,90,180,30); search2.setBounds(10,130,180,30); search3.setBounds(10,170,180,30); export.setBounds(10,210,100,30); delete.setBounds(10,250,100,30); box.setBounds(20, 520, 800, 30); total.setBounds(50, 550, 300, 30); p.add(myPane); panel.add(update); panel.add(print); panel.add(export); panel.add(delete); p.add(panel); p.add(box); p.add(total); delete.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int rowToDelete = myTable.getSelectedRow(); System.out.println(rowToDelete); tm.deleteRow(rowToDelete); myTable.setEditingRow(rowToDelete -1); myTable.setRowSelectionInterval(rowToDelete -1,rowToDelete -1); } }); this.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { dispose(); } }); // end windowlistener this.setContentPane(p); this.setBounds(0, 0, 1024, 700); this.setVisible(true); this.setResizable(false); this.pack(); this.setLocationRelativeTo(null); this.setIconImage(img.getImage()); this.setFocusableWindowState(true); } &lt;/code&gt; </code></pre> <p>here is the code for pagination</p> <pre><code> private void showPages(final int itemsPerPage, final int currentPageIndex) { sorter.setRowFilter(filter(itemsPerPage, currentPageIndex - 1)); ArrayList list = new ArrayList(); int startPageIndex = currentPageIndex - pageSize; if (startPageIndex &lt;= 0) startPageIndex = 1; int maxPageIndex = (model.getRowCount() / itemsPerPage) + 1; int endPageIndex = currentPageIndex + pageSize - 1; if (endPageIndex &gt; maxPageIndex) endPageIndex = maxPageIndex; if (currentPageIndex &gt; 1) list.add(createRadioButtons(itemsPerPage, currentPageIndex - 1, "&lt;&lt;&lt; Prev")); for (int i = startPageIndex; i &lt;= endPageIndex; i++) list.add(createLinks(itemsPerPage, currentPageIndex, i - 1)); if (currentPageIndex &lt; maxPageIndex) list.add(createRadioButtons(itemsPerPage, currentPageIndex + 1, "Next &gt;&gt;&gt;")); box.removeAll(); ButtonGroup bg = new ButtonGroup(); box.add(Box.createHorizontalGlue()); for (Iterator it = list.iterator(); it.hasNext();) { JRadioButton r = (JRadioButton) it.next(); box.add( r); bg.add( r); } box.add(Box.createHorizontalGlue()); box.revalidate(); box.repaint(); list.clear(); } private JRadioButton createLinks(final int itemsPerPage, final int current, final int target) { JRadioButton radio = new JRadioButton("" + (target + 1)) { @Override protected void fireStateChanged() { ButtonModel bmodel = getModel(); if (!bmodel.isEnabled()) { setForeground(Color.GRAY); } else if (bmodel.isPressed() &amp;&amp; bmodel.isArmed()) { setForeground(Color.GREEN); } else if (bmodel.isSelected()) { setForeground(Color.RED); } super.fireStateChanged(); } }; radio.setForeground(Color.BLUE); radio.setUI(ui); if (target + 1 == current) { radio.setSelected(true); } radio.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { showPages(itemsPerPage, target + 1); } }); return radio; } private JRadioButton createRadioButtons(final int itemsPerPage, final int target, String title) { JRadioButton radio = new JRadioButton(title); radio.setForeground(Color.BLUE); radio.setUI(ui); radio.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { showPages(itemsPerPage, target); } }); return radio; } @SuppressWarnings("rawtypes") private RowFilter filter(final int itemsPerPage, final int target) { return new RowFilter() { @Override public boolean include( RowFilter.Entry entry) { int ei = (int) entry.getIdentifier(); return (target * itemsPerPage &lt;= ei &amp;&amp; ei &lt; target * itemsPerPage + itemsPerPage); } }; } private static class ForRadioButtonUI extends BasicRadioButtonUI { public ForRadioButtonUI() { } @Override public Icon getDefaultIcon() { return null; } private static Dimension size = new Dimension(); private static final Rectangle rec1 = new Rectangle(); private static final Rectangle rec2 = new Rectangle(); private static final Rectangle rec3 = new Rectangle(); @Override public synchronized void paint(Graphics g, JComponent c) { AbstractButton b = (AbstractButton) c; ButtonModel bmodel = b.getModel(); Font f = c.getFont(); g.setFont(f); FontMetrics fm = c.getFontMetrics(f); Insets i = c.getInsets(); size = b.getSize(size); rec1.x = i.left; rec1.y = i.top; rec1.width = size.width - (i.right + rec1.x); rec1.height = size.height - (i.bottom + rec1.y); rec2.x = rec2.y = rec2.width = rec2.height = 0; rec3.x = rec3.y = rec3.width = rec3.height = 0; String text = SwingUtilities.layoutCompoundLabel(c, fm, b.getText(), null, b.getVerticalAlignment(), b.getHorizontalAlignment(), b .getVerticalTextPosition(), b.getHorizontalTextPosition(), rec1, rec2, rec3, 0); if (c.isOpaque()) { g.setColor(b.getBackground()); g.fillRect(0, 0, size.width, size.height); } if (text == null) return; g.setColor(b.getForeground()); if (!bmodel.isSelected() &amp;&amp; !bmodel.isPressed() &amp;&amp; !bmodel.isArmed() &amp;&amp; b.isRolloverEnabled() &amp;&amp; bmodel.isRollover()) { g.drawLine(rec1.x, rec1.y + rec1.height, rec1.x + rec1.width, rec1.y + rec1.height); } View v = (View) c.getClientProperty(BasicHTML.propertyKey); if (v != null) { v.paint(g, rec3); } else { paintText(g, b, rec3, text); } } } private static class ForRadioButtonUI extends BasicRadioButtonUI { public ForRadioButtonUI() { } @Override public Icon getDefaultIcon() { return null; } private static Dimension size = new Dimension(); private static final Rectangle rec1 = new Rectangle(); private static final Rectangle rec2 = new Rectangle(); private static final Rectangle rec3 = new Rectangle(); @Override public synchronized void paint(Graphics g, JComponent c) { AbstractButton b = (AbstractButton) c; ButtonModel bmodel = b.getModel(); Font f = c.getFont(); g.setFont(f); FontMetrics fm = c.getFontMetrics(f); Insets i = c.getInsets(); size = b.getSize(size); rec1.x = i.left; rec1.y = i.top; rec1.width = size.width - (i.right + rec1.x); rec1.height = size.height - (i.bottom + rec1.y); rec2.x = rec2.y = rec2.width = rec2.height = 0; rec3.x = rec3.y = rec3.width = rec3.height = 0; String text = SwingUtilities.layoutCompoundLabel(c, fm, b.getText(), null, b.getVerticalAlignment(), b.getHorizontalAlignment(), b .getVerticalTextPosition(), b.getHorizontalTextPosition(), rec1, rec2, rec3, 0); if (c.isOpaque()) { g.setColor(b.getBackground()); g.fillRect(0, 0, size.width, size.height); } if (text == null) return; g.setColor(b.getForeground()); if (!bmodel.isSelected() &amp;&amp; !bmodel.isPressed() &amp;&amp; !bmodel.isArmed() &amp;&amp; b.isRolloverEnabled() &amp;&amp; bmodel.isRollover()) { g.drawLine(rec1.x, rec1.y + rec1.height, rec1.x + rec1.width, rec1.y + rec1.height); } View v = (View) c.getClientProperty(BasicHTML.propertyKey); if (v != null) { v.paint(g, rec3); } else { paintText(g, b, rec3, text); } } } </code></pre> <p></p> <p>Please help me to solve this.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload