简体   繁体   English

更改 JComboBox 的字体颜色

[英]Changing font color of JComboBox

Using netbeans, I have this code in my comboBox which generates name suggestions.使用 netbeans,我的 comboBox 中有此代码,它会生成名称建议。

  private void createAndShowGui() {
  if(comboBox.isDisplayable()){
    txtNameID.getDocument().addDocumentListener(new DocumentListener() {
        @Override
        public void insertUpdate(DocumentEvent e) {

            Runnable doAssist = new Runnable() {
                @Override
                public void run() {
                    comboFilter(txtNameID.getText());
                    System.out.println(txtNameID.getText());
                }
            };
            SwingUtilities.invokeLater(doAssist);
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            Runnable doAssist = new Runnable() {
                @Override
                public void run() {
                    comboFilter(txtNameID.getText());
                }
            };
            SwingUtilities.invokeLater(doAssist);
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            // plain text components dont fire this
        }
    });

    comboBox.addActionListener((ActionEvent e) -> {
        txtNameID.setText(comboBox.getSelectedItem().toString());
       
    });
 }
}

//COMBOBOX SUGGEST
public void comboFilter(String enteredText) {
    java.util.List<String> filterArray = new ArrayList<String>();

    String lname = "";
    String fname = "";
    String mi = "";
    String id = "";

    try {
        con = databasePatient.ConnectDb();
        String str = "SELECT * FROM patient_record WHERE firstname  LIKE '" + enteredText + "%' OR lastname  LIKE '" + enteredText + "%' OR patient_id  LIKE '" + enteredText + "%' OR name  LIKE '" + enteredText + "%' OR name  LIKE '%" + enteredText + "%'";
        Statement stmt = con.createStatement();
        ResultSet rs2 = stmt.executeQuery(str);

        if (enteredText.equals("")) {
            String str1 = "";
            filterArray.add(str1);
        } else 
            if (rs2.next()) {
            Statement stmt1 = con.createStatement();
            String str2 = "SELECT * FROM patient_record WHERE firstname  LIKE '" + enteredText + "%' OR lastname  LIKE '" + enteredText + "%' OR patient_id  LIKE '" + enteredText + "%'OR name  LIKE '" + enteredText + "%' OR name  LIKE '%" + enteredText + "%'";
            ResultSet rs = stmt1.executeQuery(str2);

            while (rs.next()) {
                String names = rs.getString("name");
                String str1 = names;
                filterArray.add(str1);
            }
            
        } else {
            String str1 = "";
            filterArray.add(str1);

        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    listOfNames.forEach((item) -> {
        if (item.contains(enteredText)) {
            filterArray.add(item);
        }
    });

    if (filterArray.size() > 0) {
        comboBox.setModel(new DefaultComboBoxModel(filterArray.toArray()));
    }
}

My problem is that I can't change the foreground of the text in the comboBox by simply putting setForeground.我的问题是我无法通过简单地放置 setForeground 来更改 comboBox 中文本的前景。 The default color of the text is black and I need to make it white.文本的默认颜色是黑色,我需要将其设为白色。 Can anyone help me out here?有谁可以帮我离开这里吗? Help would be much appreciated.帮助将不胜感激。 Thank you!谢谢!

I did not try your code.我没有尝试你的代码。

Your problem may stem from the fact that a ComboBox is a combination of items: A field that shows a selected value, a button to open a list and the list itself.您的问题可能源于 ComboBox 是项目的组合:显示所选值的字段、打开列表的按钮和列表本身。 Now if you set the combobox foreground color I guess this would have effect on the field itself but probably not on the button and definitely not on the list.现在,如果您设置 combobox 前景色,我想这会对字段本身产生影响,但可能不会在按钮上,而且绝对不会在列表中。

Am I right until here?到这里为止我是对的吗? In that case you should look how to modify rendering of the current entry, the editing of the current entry and the rendering of the list.在这种情况下,您应该查看如何修改当前条目的呈现、当前条目的编辑和列表的呈现。

If you end up restyling a lot of single components (I guess you want to go towards black background and white foreground), maybe you want to look at Pluggable Look and Feel (PLAF) .如果你最终重新设计了很多单个组件(我猜你想将 go 改成黑色背景和白色前景),也许你想看看Pluggable Look and Feel (PLAF)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM