How to change a JTextField based on a JComboBox selection

Refresh

December 2018

Views

2.8k time

1

This is just a silly program I am working on for school but I am having some trouble. I have a JComboBox and based on what the user selects I want to change the text field. I'm having some trouble with that however, as it stands now the program compiles and runs but the text field doesn't change. I've found plenty of examples of people doing far more complicated things but I just need a simple solution. Here is the code. Thanks!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class emocon extends JFrame implements ItemListener {
JPanel row1 = new JPanel();
JComboBox choose = new JComboBox();
JPanel row2 = new JPanel();
JTextField text = new JTextField(10);
//image will be displayed here
JPanel row3 = new JPanel();
JLabel pic = new JLabel();

//Images
ImageIcon happy = new ImageIcon("images/happy.gif");
ImageIcon lol = new ImageIcon("images/lol.gif");
ImageIcon winky = new ImageIcon("images/winky.gif");
ImageIcon sad = new ImageIcon("images/sad.gif");
ImageIcon worried = new ImageIcon("images/worried.gif");
ImageIcon angry = new ImageIcon("images/angry.gif");
ImageIcon shock = new ImageIcon("images/shock.gif");
ImageIcon uninpressed = new ImageIcon("images/uninpressed.gif");
ImageIcon yawn = new ImageIcon("images/yawn.gif");
ImageIcon evil = new ImageIcon("images/evil.gif");

public emocon(){
    setTitle("Emoticon Converter");
    setSize(350,350);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

GridLayout two = new GridLayout(3,1);
setLayout(two);
 choose.addItem("Happy");
 choose.addItem("LOL");
 choose.addItem("Winky");
 choose.addItem("Sad");
 choose.addItem("Worried");
 choose.addItem("Angry");
 choose.addItem("Shock");
 choose.addItem("Uninpressed");
 choose.addItem("Yawn");
 choose.addItem("Evil");
choose.addItemListener(this);
row1.add(choose);
row2.add(text);
row3.add(pic); 
add(row1);
add(row2);
add(row3);

}
@Override
public void itemStateChanged(ItemEvent item) {
Object source = item.getSource();
String emo = source.toString();
if (emo == "Sad"){
    text.setText("hjgjhg");
}
}   
public static void main(String[] args) {
    emocon emo = new emocon();
}
}

9 answers

1

Получить выбранный элемент, как это:

String item = (String)jComboBox.getSelectedItem();
if(item.equals("Sad")){
    text.setText("hjgjhg");
}
1

Get the selected item like this:

String item = (String)jComboBox.getSelectedItem();
if(item.equals("Sad")){
    text.setText("hjgjhg");
}
1

Get the selected item like this:

String item = (String)jComboBox.getSelectedItem();
if(item.equals("Sad")){
    text.setText("hjgjhg");
}
3

You've a problem here:

if (emo == "Sad"){
    text.setText("hjgjhg");
}

Don't compare Strings using ==. Use the equals(...) or the equalsIgnoreCase(...) method instead. Understand that == checks if the two objects are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. So instead of

if (fu == "bar") {
  // do something
}

do,

if ("bar".equals(fu)) {
  // do something
}

or,

if ("bar".equalsIgnoreCase(fu)) {
  // do something
}

EDIT

Your other problem is that you're just getting the source from the ItemEvent. The source is the JComboBox, not what you want. You need to get the selected item!

Try using a different method available in your ItemEvent object, not getSource().

1

Get the selected item like this:

String item = (String)jComboBox.getSelectedItem();
if(item.equals("Sad")){
    text.setText("hjgjhg");
}
1

Get the selected item like this:

String item = (String)jComboBox.getSelectedItem();
if(item.equals("Sad")){
    text.setText("hjgjhg");
}
1

Получить выбранный элемент, как это:

String item = (String)jComboBox.getSelectedItem();
if(item.equals("Sad")){
    text.setText("hjgjhg");
}
1

Получить выбранный элемент, как это:

String item = (String)jComboBox.getSelectedItem();
if(item.equals("Sad")){
    text.setText("hjgjhg");
}
1

Get the selected item like this:

String item = (String)jComboBox.getSelectedItem();
if(item.equals("Sad")){
    text.setText("hjgjhg");
}