标签: jcombobox

如何让JComboBox中的某些项目无法选择?

如何让我的一些JComboBox项目无法选择?我试过这个:

@Override
public Component getListCellRendererComponent(JList list, Object value,
    int index. boolean isSelected, boolean cellHasFocus) {

    Component comp = super.getListCellRendererComponent(list, value, index,
        isSelected, cellHasFocus);

    if (not selectable conditions) {
        comp.setEnabled(false);
        comp.setFocusable(false);
    } else {
        comp.setEnabled(true);
        comp.setFocusable(true);
    }

    return comp;
}
Run Code Online (Sandbox Code Playgroud)

项目变为灰色,但仍可由用户选择.

java swing jcombobox

4
推荐指数
1
解决办法
3209
查看次数

如何在填充 JComboBox 时保持它的弹出菜单打开?

我的面板上有一个 JComboBox。弹出菜单项之一是“更多”,当我单击它时,我会获取更多菜单项并将它们添加到现有列表中。在此之后,我希望保持弹出菜单打开,以便用户意识到已获取更多项目,但是弹出菜单关闭。我使用的事件处理程序代码如下

public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == myCombo) {
            JComboBox selectedBox = (JComboBox) e.getSource();
            String item = (String) selectedBox.getSelectedItem();
            if (item.toLowerCase().equals("more")) {
                fetchItems(selectedBox);
            }
            selectedBox.showPopup();
            selectedBox.setPopupVisible(true);
        }
    }



private void fetchItems(JComboBox box)
    {
        box.removeAllItems();
        /* code to fetch items and store them in the Set<String> items */
        for (String s : items) {
            box.addItem(s);
        }
    }
Run Code Online (Sandbox Code Playgroud)

我不明白为什么 showPopup() 和 setPopupVisible() 方法没有按预期运行。

java applet swing combobox jcombobox

4
推荐指数
1
解决办法
1万
查看次数

如何检测JComboBox是否为空?

如何检测JComboBox是否为空?是这样的:

combobox.isEmpty()

java swing jcombobox

4
推荐指数
1
解决办法
1万
查看次数

JComboBox的值

是否可以定义与JComboBox中的实际内容不同的值?
在HTML中,它看起来如下:

<select>
  <option value="value1">Content1</option>
  <option value="value2">Content2</option>
  <option value="value3">Content3</option>
</select>
Run Code Online (Sandbox Code Playgroud)

无论内容有多长,这里都可以获得一个简短的值.

在Java中我只知道以下解决方案:

// Creating new JComboBox with predefined values
   String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" }; 
   private JComboBox combo = new JComboBox(petStrings);

// Retrieving selected value
   System.out.println(combo.getSelectedItem());
Run Code Online (Sandbox Code Playgroud)

但在这里我只会得到"Cat","Dog"等

.问题是,我想将数据库中的所有客户名称加载到JComboBox中,然后从所选客户中检索ID.它应该如下所示:

<select>
  <option value="104">Peter Smith</option>
  <option value="121">Jake Moore</option>
  <option value="143">Adam Leonard</option>
</select>
Run Code Online (Sandbox Code Playgroud)

java swing jcombobox

4
推荐指数
1
解决办法
7147
查看次数

更改背景颜色可编辑JComboBox

我正在编写JFrame表单中的可编辑组合框,但我想改变背景颜色.

程序如何工作:如果我点击"按"按钮,那么组合框的背景需要变成黑色.

我试过了:

1.

cbo.setBackground(Color.BLACK);
Run Code Online (Sandbox Code Playgroud)

但它没有做任何事情

2

cbo.getEditor().getEditorComponent().setBackground(Color.BLACK);

((JTextField) cbo.getEditor().getEditorComponent()).setOpaque(true);
Run Code Online (Sandbox Code Playgroud)

做这个:

图片

代码示例:

public class NewJFrame extends javax.swing.JFrame {

    private JComboBox cboCategorie;

    public NewJFrame() {
        initComponents();

        cboCategorie = new JComboBox();
        cboCategorie.setBounds(10, 10, 250, 26);
        cboCategorie.setVisible(true);
        cboCategorie.setEditable(true);
        this.add(cboCategorie);

    }

private void pressActionPerformed(java.awt.event.ActionEvent evt) {
        cboCategorie.getEditor().getEditorComponent().setBackground(Color.BLACK);
        ((JTextField) cboCategorie.getEditor().getEditorComponent()).setOpaque(true);
}
Run Code Online (Sandbox Code Playgroud)

我正在使用Java JDK7

任何sugestions?

java swing jcombobox java-7 setbackground

4
推荐指数
1
解决办法
2万
查看次数

GridBagLayout中的JComboBox宽度问题

当我将JComboBox放入带有GridBagLayout的JPanel时,我发现最长的JComboBox项目不会完全显示,而是显示部分带有'...'

例如,它显示'医疗成分'的'医疗保健成分'.

请看这里的截图:

JComboBox宽度问题

代码是这样的:

JPanel infoPanel = JPanel(new GridBagLayout());            
dataTypeCB = new JComboBox(dataTypeList.toArray());
infoPanel.add(dataTypeCB, new GridBagConstraints(1, 1, 1, 1, 1, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5,5,5,5), 0, 0));
Run Code Online (Sandbox Code Playgroud)

我已经找到了两个解决方案:

  1. 覆盖JComboBox的getPreferenceSize,但有点烦人.
  2. 使用JPanel包装JComboBox,如下所示,但是经过扭曲的JPanel会引入一些意图并导致对齐问题,但仍然不完美:

第二个解决方案代码:

dataTypeCB = new JComboBox(dataTypeList.toArray());
JPanel dataTypeCBPanel = new JPanel();//make item can be fully show up.
dataTypeCBPanel.add(dataTypeCB, BorderLayout.CENTER);
infoPanel.add(dataTypeCBPanel, new GridBagConstraints(1, 1, 1, 1, 1, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5,5,5,5), 0, 0));
Run Code Online (Sandbox Code Playgroud)

我的问题是:还有其他简单方法可以解决这个问题吗?

环境:Windows 7与JDK 1.6_25(64位版本)

非常感谢

java swing jcombobox gridbaglayout

4
推荐指数
1
解决办法
1535
查看次数

如何将 JButton 放入 JComboBox

我想在 JComboBox 中放置一个 JButton。此按钮允许用户浏览文件。用户选择的文件被添加到 JComboBox 列表中。我该怎么做呢?我使用某种渲染器吗?谢谢你。

编辑: 在阅读有关 ListCellRenderer 的更多信息后,我尝试了以下代码:

JComboBox comboBox = new JComboBox(new String[]{"", "Item1", "Item2"});
ComboBoxRenderer renderer = new ComboBoxRenderer();
comboBox.setRenderer(renderer);

class ComboBoxRenderer implements ListCellRenderer {

    public Component getListCellRendererComponent(
            JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean cellHasFocus) {

        JButton jbutton = new JButton("Browse");

        return jbutton;
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的问题是按钮“浏览”将被添加 3 次,我希望它只显示一次并在它下方显示 Item1 和 Item2 作为普通/常规组合框选择对象。

java swing render jbutton jcombobox

4
推荐指数
1
解决办法
6134
查看次数

为 JComboBox 显示不可选择的默认值

我有一个JComboBox包含三个 Items {"Personel", "Magasinier", "Fournisseur"}

我希望它JComboBox显示 value "Choisir une option :",这是一个不可选择的值。

我在以下之后尝试了此代码initComponents();

this.jComboBox1.setSelectedItem("Choisir une option :");
Run Code Online (Sandbox Code Playgroud)

但它不起作用。

我怎样才能做到这一点 ?

java swing prompt jcombobox listcellrenderer

4
推荐指数
1
解决办法
2万
查看次数

jcombobox作为单元格编辑器java.awt.IllegalComponentStateException:必须在屏幕上显示组件以确定其位置

我使用自定义JComboBox作为JTable中的单元格编辑器.当用户使用键盘控件进入单元格时,它会尝试打开弹出窗口.这会导致以下错误:

java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location
    at java.awt.Component.getLocationOnScreen_NoTreeLock(Component.java:1964)
    at java.awt.Component.getLocationOnScreen(Component.java:1938)
    at javax.swing.JPopupMenu.show(JPopupMenu.java:887)
    at javax.swing.plaf.basic.BasicComboPopup.show(BasicComboPopup.java:191)
    at javax.swing.plaf.basic.BasicComboBoxUI.setPopupVisible(BasicComboBoxUI.java:859)
    at javax.swing.JComboBox.setPopupVisible(JComboBox.java:796)
Run Code Online (Sandbox Code Playgroud)

我看过一些文章说这是一个已知问题,解决方法是设置:

    comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
Run Code Online (Sandbox Code Playgroud)

但这并没有帮助.无论如何这应该做什么?

我读过的关于这个的所有主题和文章对于问题的本质都非常模糊.

有没有人对这个问题出现的原因有什么了解?我的组合框是非常自定义的,所以它有助于理解问题的基础,所以我可以修复代码.

这是在捕获的组合框上的焦点获取事件上触发并调用setPopupVisible(true);

 public void focusGained(java.awt.event.FocusEvent e)
 {
        //if focus is gained then make sure we show the popup if it is suppose to be visible
            setPopupVisible(true);
        //and highlight the selected text if any
        comboTextEditor.setCaretPosition(comboTextEditor.getText().length());
        comboTextEditor.moveCaretPosition(0);
 }
Run Code Online (Sandbox Code Playgroud)

顺便说一句,我在Java 1.7_40中获得与Java 1.6_45相同的结果

完整堆栈跟踪:

Exception in thread "AWT-EventQueue-1" java.awt.IllegalComponentStateException: component must be showing on …
Run Code Online (Sandbox Code Playgroud)

java swing jtable jcombobox illegalstateexception

4
推荐指数
1
解决办法
4657
查看次数

有没有办法将对象添加到 JComboBox 并分配一个要显示的字符串?

我想将对象添加到 JComboBox 但在 JComboBox 上为每个对象显示一个字符串。

例如,在下面的html代码中

<select>
  <option value="1">Item 1</option>
  <option value="2">Item 2</option>
  <option value="3">Item 3</option>
  <option value="4">Item 4</option>
</select>
Run Code Online (Sandbox Code Playgroud)

在第一项中,显示的字符串是“Item 1”,但该项的值为“1”。

有没有一种表格可以用 JComboBox 做类似的事情?

java swing combobox jcombobox

4
推荐指数
1
解决办法
1万
查看次数