The trouble portion is where I declare and assign :
String emotionText = (String) emotions.getSelectedItem();
I am trying to use it later down the line to compare to one of the array items from the JcomboBox. Where I say
if (emotionText.equals("Happy") {
JLabel newText = new JLabel(verse);
panel2.add(newText);
}
ie. if the result is this.. then do this, but it is not working properly. Any insight would be much appreciated. Thanks in advance.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUI extends JComboBox {
public static void main(String[] args) {
JFrame frame = new JFrame("Mood Food");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(900, 800);
frame.setLocation(430, 100);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); // added code
frame.add(panel);
var name = javax.swing.JOptionPane.showInputDialog("What is your name?");
JLabel lblName = new JLabel(name + " ,welcome to the Mood Food Program");
lblName.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(lblName);
JLabel lbl = new JLabel("How are you feeling today?");
lbl.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(lbl);
String[] choices = {"Select..", "Happy", "Content", "Grateful", "Excited", "Sad",
"Angry", "Lonely", "Heartbroken", "Worried"};
JComboBox<String> emotions = new JComboBox<String>(choices);
emotions.setMaximumSize(panel.getPreferredSize());
emotions.setAlignmentX(Component.CENTER_ALIGNMENT);
emotions.setAlignmentY(Component.TOP_ALIGNMENT);
String emotionText = (String) emotions.getSelectedItem();
panel.add(emotions);
String verse = Happy.Verses();
JPanel panel2 = new JPanel();
panel2.setVisible(false);
frame.add(panel2);
if (emotionText.equals("Happy") {
JLabel newText = new JLabel(verse);
panel2.add(newText);
}
JButton btn = new JButton("OK");
btn.setAlignmentX(Component.CENTER_ALIGNMENT); // added code
panel.add(btn);
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
boolean visible = panel.isVisible();
panel.setVisible(!visible);
panel2.setVisible(visible);
}});
frame.setVisible(true); // added code
}
}
Swing is an event driven environment, that is, something happens and you respond to it. It also means that your code doesn't run in a linear or sequential flow.
For example, you can't do this...
JComboBox<String> emotions = new JComboBox<String>(choices);
emotions.setMaximumSize(panel.getPreferredSize());
emotions.setAlignmentX(Component.CENTER_ALIGNMENT);
emotions.setAlignmentY(Component.TOP_ALIGNMENT);
String emotionText = (String) emotions.getSelectedItem();
panel.add(emotions);
String verse = Happy.Verses();
if (emotionText.equals("Happy") {
JLabel newText = new JLabel(verse);
panel2.add(newText);
}
When you call getSelectedItem
, the user has not actually had a chance to select anything (nothing's been presented to them).
Instead, you need to add a ActionListener
to the JComboBox
and update emotionText
from within it.
Because of the delayed nature of this workflow, you should also update the ActionListener
on the button to update the label based on the value of emotionText
.
Something more like...
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Main extends JComboBox {
public static void main(String[] args) {
new Main();
}
private String emotionText = "Empty inside";
private CardLayout cardLayout;
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Mood Food");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(900, 800);
frame.setLocation(430, 100);
cardLayout = new CardLayout();
frame.setLayout(cardLayout);
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
frame.add(panel, "first");
var name = javax.swing.JOptionPane.showInputDialog("What is your name?");
JLabel lblName = new JLabel(name + " ,welcome to the Mood Food Program");
lblName.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(lblName, gbc);
JLabel lbl = new JLabel("How are you feeling today?");
lbl.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(lbl, gbc);
String[] choices = {"Select..", "Happy", "Content", "Grateful", "Excited", "Sad",
"Angry", "Lonely", "Heartbroken", "Worried"};
JComboBox<String> emotions = new JComboBox<String>(choices);
emotions.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
emotionText = (String)emotions.getSelectedItem();
}
});
panel.add(emotions, gbc);
JPanel panel2 = new JPanel();
JLabel newText = new JLabel(emotionText);
panel2.add(newText);
frame.add(panel2, "second");
JButton btn = new JButton("OK");
btn.setAlignmentX(Component.CENTER_ALIGNMENT); // added code
panel.add(btn, gbc);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
newText.setText(emotionText);
cardLayout.show(frame.getContentPane(), "second");
}
});
frame.setVisible(true); // added code
}
});
}
}