我有一个JTable
,每当我选择一个单元格时,我想打印它的行和列索引。出于这个原因,我使用getSelectedRow()
和getSelectedColumn()
方法。运行以下代码:
public class TestTimeTable extends JFrame{
private final int rows = 10;
private final int cols = 8;
private final String daysOfTheWeek[] = {"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY"};
private final JPanel jTablePanel;
private final JScrollPane scrollPane;
private final JTable timeTable;
private final Object[][] rowData;
public TestTimeTable(){
this.rowData = new Object[this.rows][this.cols];
this.timeTable = new JTable(this.rowData,this.daysOfTheWeek){
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
this.timeTable.setRowHeight(200, 200);
this.timeTable.setFillsViewportHeight(true);
this.timeTable.setOpaque(true);
this.timeTable.setColumnSelectionAllowed(true);
this.timeTable.setRowSelectionAllowed(true);
this.timeTable.setCellSelectionEnabled(true);
this.timeTable.setDefaultRenderer(Object.class, new BoardTableCellRenderer());
this.scrollPane = new JScrollPane(this.timeTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
this.jTablePanel = new JPanel();
this.jTablePanel.add(this.scrollPane);
getContentPane().add(new JScrollPane(this.timeTable), BorderLayout.CENTER);
}
public int getColumn(){
return this.timeTable.getSelectedColumn();
}
public int getRow(){
return this.timeTable.getSelectedRow();
}
public JTable getTimeTable(){
return this.timeTable;
}
public void createAndShowUI(){
setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] argas){
TestTimeTable tt = new TestTimeTable();
tt.createAndShowGUI();
}
}
class BoardTableCellRenderer extends DefaultTableCellRenderer {
Component c;
private static final long serialVersionUID = 1L;
private final Color selectionBlue = new Color(131,166,198);
private final MatteBorder border = new MatteBorder(1, 1, 0, 0, Color.BLACK);
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (table.isCellSelected(row, column)){
c.setBackground(this.selectionBlue);
setBorder(this.border);
} else {
c.setBackground(Color.WHITE);
}
if(isSelected){
JOptionPane.showMessageDialog(null, table.getSelectedRow() + ""+table.getSelectedColumn(),null, JOptionPane.INFORMATION_MESSAGE);
}
return c;
}
}
每当我选择一个单元格时,会出现JOptionPane
框架,然后该应用会冻结。所以我必须一直打断它。有人可以告诉我为什么会这样吗?我该怎么做才能解决它?
答案 0 :(得分:5)
boolean isSelected, boolean hasFocus
的{{1}}(built_in方法),而不是引用回JTable - getTableCellRendererComponent
,重置其他语句中其余单元格的Color,例如简单的躯干
table.isCellSelected(row, column)
删除所有这些.Xxx,没用了
不要使用setSize,而是使用JFrame.pack()和/或为JScrollPane设置大小,例如 private class StatusRenderer implements TableCellRenderer {
private static final long serialVersionUID = 1L;
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
setBackground(table.getSelectionBackground());
} else {
setBackground(table.getBackground());
}
return this;
}
}
(对于合理的数字或行和列,否则使用Dimension(x,x))
修改
table.setPreferredScrollableViewportSize(table.getPreferredSize());
答案 1 :(得分:1)
我有一个JTable,每当我选择一个单元格时,我想打印它的行和列索引。
我会覆盖JTable的changeSelection(...)
方法:
JTable table = new JTable(data, columnNames)
{
public void xxxchangeSelection(
int row, int column, boolean toggle, boolean extend)
{
super.changeSelection(row, column, toggle, extend);
System.out.println(row + " : " + column);
}
};