0

I have a main JFrame pane using Swing in Java with a JComboBox embedded in it with a number of selections.

Everything initiates well on opening but when I write inside the JTextField, the words in the JComboBox disappear.

I'm initiating my JComboBox in in the following way:

private static String[] options = new String[] {"Search your own pasted text" ,  "Search your own file", 
        "Search website", "Search Lyric Database", "Search Books Database"};
private static JComboBox<String> bookList = new JComboBox<>(options);

Then I initiate the JTextField in this way

private static JTextField textFieldTheme = new JTextField(50); 
private static String theme = textFieldTheme.getText();

Have no idea why they interfere with each other....everything works fine still. In fact, I can stick click the the JComboBox menu, its just that it resizes and the words disappear from it

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Katie Melosto
  • 1,047
  • 2
  • 14
  • 35
  • 2
    For better help sooner post a proper [mre] that demonstrates your issue. – Frakcool Nov 27 '19 at 20:23
  • 2
    All the static variables are in indicator of poor class design. I suggest you read the section from the Swing tutorial on [How to Use Text Fields](https://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html) for a better design. Download the working example and replace the text area with your combo box. That is start with a working example ant make a simple change. Then if it stops working you know what you changed and you can ask a specific question. – camickr Nov 27 '19 at 21:32
  • You don't seem to have accepted any answers. Doing so helps all (including you, when it encourages people not to begin ignoring you). – Andrew Thompson Nov 27 '19 at 22:15
  • @AndrewThompson hi Andrew thanks for the feedback... For some reason the answers are only showing up as comments. Can you advise on how to accept them as answers – Katie Melosto Nov 27 '19 at 22:49
  • See the comment I made on [this answer](https://stackoverflow.com/a/57387012/418556) before commenting here. – Andrew Thompson Nov 28 '19 at 11:48

2 Answers2

0

Per above code you wrote, all looks fine. Maybe better to add also your main frame code to see how you adding the components.

Anyway you can follow instructions How to Use Text Fields

Adir Dayan
  • 1,308
  • 13
  • 21
0

I found that the JTextField was interfering with the JTextArea and somehow that was making it so the JComboBox menu text disappeared if I wrote something into the JTextField.

I wound up turning the JTextField into a custom JTextArea -- while it wasn't ideal, it did allow the JComboBox to work without the text disappearing.

I used this code to turn the JTextArea into a JTextField:

JTextArea textFieldTheme = new JTextArea(textAreaText, 1, 50);
        DefaultCaret caret = (DefaultCaret)textFieldTheme.getCaret();
        caret.setUpdatePolicy(DefaultCaret.OUT_BOTTOM);

ADAPTED FROM THIS HELPFUL POST: Java / Swing : JTextArea in a JScrollPane, how to prevent auto-scroll?

Katie Melosto
  • 1,047
  • 2
  • 14
  • 35