【问题标题】:how to focus empty combobox如何聚焦空组合框
【发布时间】:2014-03-24 09:05:20
【问题描述】:

在我的项目中,我有一个空的组合框,单击它后我想填充它。

comboCurrent = new JComboBox<String>();     
comboCurrent.setBounds(265, 181, 80, 20);
add(comboCurrent);
comboCurrent.setEditable(true);
comboCurrent.setSelectedItem(null); 
comboCurrent.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        // TODO populate here
        System.out.println(e);
    }
});

但不知何故,动作监听器在这里不起作用。 有没有办法在组合框还是空的时候收听第一次点击?

【问题讨论】:

  • comboCurrent.setBounds(265, 181, 80, 20); Java GUI 可能必须在多个平台、不同的屏幕分辨率和使用不同的 PLAF 上工作。因此,它们不利于组件的精确放置。要为强大的 GUI 组织组件,请改用布局管理器或 combinations of them,以及 white space 的布局填充和边框。 (也许你应该给组合一个Default Text..)

标签: java swing focus jcombobox


【解决方案1】:

ActionListener 仅在您按下 Enter 键时调用。对于第一次点击,我建议您在您的JComboBox 上使用FocusListenerMouseListener

【讨论】:

  • FocusListener 会更好,因为如果用户点击组合,MouseListener 将不会触发。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-23
  • 2016-03-20
  • 2017-10-22
  • 1970-01-01
  • 1970-01-01
  • 2021-01-22
相关资源
最近更新 更多