jctable jcombobox不取消选择
如果我在JTable内有JCombobox,那么它都可以很好地工作,除非您单击ComboBox以触发其下拉列表以显示其下拉,然后再次单击Combobox上的相同位置,或单击JPANEL。
这使Combobox以某些怪异的选择状态突出显示,并且可见下向下箭头。如果您实际上是从Combobox中选择一个值,则将ComboBox出现取消选择,并且向下箭头看不到。
在此选定状态时,它不仅看起来很奇怪,而且如果您尝试以编程方式更改表值,则“选择” Combobox不会更新其值,直到您再次单击它。
下面是一个示例,只需单击ComboBox,然后选择一个值,然后查看表格出现,然后选择一个ComboBox,但是不要选择一个值,请单击红色Jpanel,然后观察该ComboBox现在如何显示。
同样,在这种状态下,它不允许动态更新。我如何获得它,以免它陷入此选定/突出的状态。
public class JTableTest extends JFrame {
public JTableTest() {
String[] columns = {"1", "2", "3"};
DefaultTableModel tableModel = new DefaultTableModel(columns, 1);
JTable table = new JTable(tableModel);
table.setRowSelectionAllowed(false);
table.setCellSelectionEnabled(false);
table.setFocusable(false);
for (int i = 0; i < 3; i++) {
JComboBox cb = new JComboBox();
cb.setFocusable(false);
cb.addItem("a");
cb.addItem("b");
cb.addItem("c");
table.getColumnModel().getColumn(i).setCellEditor(new DefaultCellEditor(cb));
}
JPanel jp = new JPanel();
jp.setPreferredSize(new Dimension(500,500));
jp.setBackground(Color.red);
jp.add(table);
this.add(jp);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JTableTest jt = new JTableTest();
jt.setVisible(true);
}
}
If I have a JComboBox within a JTable then it all works great except if you click on the combobox to trigger it's dropdown to appear and then either click on the same location on the combobox again or click on the JPanel.
This leaves the combobox in some weird selected state with it highlighted and the down arrow visible. If you were to actually select a value from the combobox instead then the combobox appeared deselected and the down arrow is not visible.
When in this selected state it not only looks weird but if you attempt to change the table values programmatically the "selected" combobox does not update it's value until you click it again.
Below is an example simply click a combobox and then select a value and see how the table appears and then select a combobox but instead of selecting a value click on the red JPanel and observe how that combobox appears now.
Again when in this state it does not allow dynamic updates. How do I get it so it doesn't get stuck in this selected/highlighted state.
public class JTableTest extends JFrame {
public JTableTest() {
String[] columns = {"1", "2", "3"};
DefaultTableModel tableModel = new DefaultTableModel(columns, 1);
JTable table = new JTable(tableModel);
table.setRowSelectionAllowed(false);
table.setCellSelectionEnabled(false);
table.setFocusable(false);
for (int i = 0; i < 3; i++) {
JComboBox cb = new JComboBox();
cb.setFocusable(false);
cb.addItem("a");
cb.addItem("b");
cb.addItem("c");
table.getColumnModel().getColumn(i).setCellEditor(new DefaultCellEditor(cb));
}
JPanel jp = new JPanel();
jp.setPreferredSize(new Dimension(500,500));
jp.setBackground(Color.red);
jp.add(table);
this.add(jp);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JTableTest jt = new JTableTest();
jt.setVisible(true);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论