JTable里的JComboBox在按空格键时不能显示下拉框

zaghost zaghost 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
通常的JComboBox在获得焦点时按空格键会显示下拉框,
但是JTable里的JComboBox,按空格键没有反应
这是为什么
有没有办法实现JTable里的JComboBox也能在按空格键会显示下拉框

附实例代码


public class T1 extends JComboBox implements TableCellRenderer {
public T1() {
setBorder(BorderFactory.createEmptyBorder());

addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
// TODO 自動生成されたメソッド・スタブ
System.out.println("gggg");
}

});

}

public static void main(String[] args) {

JFrame f = new JFrame();

// try {
// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
// } catch (Exception e) {
// e.printStackTrace();
// }

JComboBox comboBox = new JComboBox();
comboBox.setBorder(BorderFactory.createEmptyBorder());
comboBox.addItem("Item-1");
comboBox.addItem("Item-2");
comboBox.addItem("Item-3");
comboBox.addItem("Item-4");
// comboBox.addItem("Item-5");
comboBox.setFocusable(true);

DefaultCellEditor editor = new DefaultCellEditor(comboBox);


JTable table = new JTable(5, 5);
table.getColumnModel().getColumn(0).setCellRenderer(new T1());
table.getColumnModel().getColumn(0).setCellEditor(editor);

JScrollPane sp = new JScrollPane(table);

f.getContentPane().add(sp, BorderLayout.CENTER);

JComboBox cmb1 = new JComboBox();
cmb1.setBorder(BorderFactory.createEmptyBorder());
cmb1.addItem("Item-1");
cmb1.addItem("Item-2");
cmb1.addItem("Item-3");
cmb1.addItem("Item-4");
cmb1.addItem("Item-5");
// cmb1.setFocusable(true);
f.getContentPane().add(cmb1, BorderLayout.SOUTH);

JComboBox cmb2 = new JComboBox();
cmb2.setBorder(BorderFactory.createEmptyBorder());
cmb2.addItem("Item-1");
cmb2.addItem("Item-2");
cmb2.addItem("Item-3");
cmb2.addItem("Item-4");
cmb2.addItem("Item-5");
cmb2.setFocusable(true);
f.getContentPane().add(cmb2, BorderLayout.NORTH);


f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
this.removeAllItems();
this.addItem(value);

return this;
}
}
...全文
评论
收藏 已收藏
分享
返回顶部
zaghost
Java SE 62,621 成员 307,251 内容