关于JavaSwing,JTable的问题。
JTable如何监听单元格选中状态变化的事件,我的JTable已经被设置为只能选中单元格,不能选中整行和整列。我希望在单元格选中状态变化后,进行一些处理,我知道JTabl...
JTable如何监听单元格选中状态变化的事件,我的JTable已经被设置为只能选中单元格,不能选中整行和整列。我希望在单元格选中状态变化后,进行一些处理,我知道JTable可以用getSelectionModel().addListSelectionListener监听行选中状态变化,但是倘若选中同一行的其他单元格,就不会发出这个事件。麻烦高手指点迷津,可以追加积分。
多谢2楼,真的挺好,我回去试了,好用
但是总是感觉这样稍微有点取巧,有更好的解决办法吗?比如用事件模型什么的。 展开
多谢2楼,真的挺好,我回去试了,好用
但是总是感觉这样稍微有点取巧,有更好的解决办法吗?比如用事件模型什么的。 展开
2个回答
展开全部
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.util.*;
/**
* @author Hardneedl
*/
class CellSelectionDemo extends JFrame {
private static final Dimension minSize = new Dimension(300, 200);
private static final Dimension maxSize = new Dimension(1024, 768);
private static final Dimension preferredSize = new Dimension(600, 400);
public Dimension getMaximumSize() {return maxSize;}
public Dimension getMinimumSize() {return minSize;}
public Dimension getPreferredSize() {return preferredSize;}
public String getTitle() {return "Frame Title";}
private class DemoTableModel extends AbstractTableModel{
public int getRowCount() {return 3;}
public int getColumnCount() {return 3;}
public Object getValueAt(int rowIndex, int columnIndex) {return rowIndex + " " + columnIndex;}
}
private class FocusCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component c=super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
if (hasFocus) System.out.printf("row=%d,column=%d\r\n",row,column);
return c;
}
}
private JTable table;;
CellSelectionDemo() throws HeadlessException {
doLay();
attachListeners();
}
private void doLay() {
Container container = getContentPane();
container.add(new JScrollPane(table=new JTable(new DemoTableModel())), BorderLayout.CENTER);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
pack();
}
private void attachListeners() {
Enumeration<TableColumn>em = table.getColumnModel().getColumns();
while (em.hasMoreElements()) {
TableColumn col = em.nextElement();
col.setCellRenderer(new FocusCellRenderer());
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {new CellSelectionDemo().setVisible(true);}
}
import javax.swing.table.*;
import java.awt.*;
import java.util.*;
/**
* @author Hardneedl
*/
class CellSelectionDemo extends JFrame {
private static final Dimension minSize = new Dimension(300, 200);
private static final Dimension maxSize = new Dimension(1024, 768);
private static final Dimension preferredSize = new Dimension(600, 400);
public Dimension getMaximumSize() {return maxSize;}
public Dimension getMinimumSize() {return minSize;}
public Dimension getPreferredSize() {return preferredSize;}
public String getTitle() {return "Frame Title";}
private class DemoTableModel extends AbstractTableModel{
public int getRowCount() {return 3;}
public int getColumnCount() {return 3;}
public Object getValueAt(int rowIndex, int columnIndex) {return rowIndex + " " + columnIndex;}
}
private class FocusCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component c=super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
if (hasFocus) System.out.printf("row=%d,column=%d\r\n",row,column);
return c;
}
}
private JTable table;;
CellSelectionDemo() throws HeadlessException {
doLay();
attachListeners();
}
private void doLay() {
Container container = getContentPane();
container.add(new JScrollPane(table=new JTable(new DemoTableModel())), BorderLayout.CENTER);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
pack();
}
private void attachListeners() {
Enumeration<TableColumn>em = table.getColumnModel().getColumns();
while (em.hasMoreElements()) {
TableColumn col = em.nextElement();
col.setCellRenderer(new FocusCellRenderer());
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {new CellSelectionDemo().setVisible(true);}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询