我有JTable如下。它显示一个页面的30条记录。如果我点击第二页的第二行。 myTabel.getSeletedRow()传递值'2',而不是'34'。怎么实现呢?
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<MyTableModel> 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<MyTableModel> (tm);
myTable.setRowSorter(sorter);
showPages(30, 1); ------------------> // 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);
} </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 <= 0)
startPageIndex = 1;
int maxPageIndex = (model.getRowCount() / itemsPerPage) + 1;
int endPageIndex = currentPageIndex + pageSize - 1;
if (endPageIndex > maxPageIndex)
endPageIndex = maxPageIndex;
if (currentPageIndex > 1) list.add(createRadioButtons(itemsPerPage, currentPageIndex - 1, "<<< Prev"));
for (int i = startPageIndex; i <= endPageIndex; i++) list.add(createLinks(itemsPerPage, currentPageIndex, i - 1));
if (currentPageIndex < maxPageIndex) list.add(createRadioButtons(itemsPerPage, currentPageIndex + 1, "Next >>>"));
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() && 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 <= ei && ei < 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() && !bmodel.isPressed() && !bmodel.isArmed() && b.isRolloverEnabled() && 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() && !bmodel.isPressed() && !bmodel.isArmed() && b.isRolloverEnabled() && 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);
}
}
}
请帮我解决这个问题。
答案 0 :(得分:3)
您应该使用如下方法获取支持JTable的数据模型中的确切行。
myTable.convertRowIndexToModel(myTable.getSelectedRow());