MKL
MKL

Reputation: 5

Return value of filled JComboBox and using the values to fill JTextField

I'm coding a JFrame which opens a JOptionPane through JMenubar. The JOptionPane does have a combobox with a lot of different string values. After the JComboBox there is a JTextfield.

I want the text I select in the JComboBox to be inserted into the next JTextField and to be updated everytime I select a new string value in the JComboBox.

Please help!

class DateiAdapter implements ActionListener {

public void actionPerformed(ActionEvent event) {

    JMenuItem change = (JMenuItem) event.getSource();

    JComboBox alleQuest = new JComboBox(ah.getLine(1)); //this ComboBox gets a lot of different values from a different class

    // Actionlistener
    ActionListener cbActionListener = new ActionListener() {

        public void actionPerformed(ActionEvent e) {

            String s = (String) allQuest.getSelectedItem(); //gets the selected string of the JComboBox
            System.out.println("\n" + s); // I want to use String s outside of here, too
            }
        };

        allQuest.addActionListener(cbActionListener);

        JTextField questions = new JTextField(); //THIS is the TextField I want to insert the different values of the ComboBox above
        JTextField a2 = new JTextField();
        JTextField b2 = new JTextField();
        JTextField c2 = new JTextField();
        JTextField answ2 = new JTextField();

        if (change == itemChange) {

            final JComponent[] input = new JComponent[] {
                    new JLabel("You change your question here:"),
                    allQuest,
                    quest2,
                    a2,
                    b2,
                    c2,
                    answ2,  };

            JOptionPane.showMessageDialog(null, input, "Change Question", JOptionPane.PLAIN_MESSAGE);

Upvotes: 0

Views: 240

Answers (3)

Antoniossss
Antoniossss

Reputation: 32517

You have 2 options regarding

public void actionPerformed(ActionEvent e) {

    String s = (String) allQuest.getSelectedItem(); //gets the selected string of the JComboBox
    System.out.println("\n" + s); // I want to use String s outside of here, too
    }
};
  1. Either store s as a field of enclosing class (DateiAdapter class in your case)
  2. Update every GUI component you want with given s value from the actionPerformed method. To be honest this is the exact place to do this as actionPerformed is invoked by the EDT, where all GUI changes should happen.
  3. In case you want s to use outside DateiAdapter class, follow point 1, then optionally 2, and add public getter for that field.

In your case putting questions.setText(alleQuest.getSelectedItem().toString()); in the actionPerformed would be the most straightforward choice.

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347214

So, this basically, seems to, come down a question of context, the questions field needs to be defined in a context in which the cbActionListener can reference it.

One of the simplest was would be to make questions an instance field of the class, for example...

class DateiAdapter implements ActionListener {

    private JTextField questions = new JTextField(); //THIS is the TextField I want to insert the different values of the ComboBox above

    public void actionPerformed(ActionEvent event) {

        JMenuItem change = (JMenuItem) event.getSource();

        JComboBox alleQuest = new JComboBox(ah.getLine(1)); //this ComboBox gets a lot of different values from a different class

        // Actionlistener
        ActionListener cbActionListener = new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                String s = (String) allQuest.getSelectedItem(); //gets the selected string of the JComboBox
                questions.setText(s);
            }
        };

        allQuest.addActionListener(cbActionListener);

        JTextField a2 = new JTextField();
        JTextField b2 = new JTextField();
        JTextField c2 = new JTextField();
        JTextField answ2 = new JTextField();

        if (change == itemChange) {

            final JComponent[] input = new JComponent[]{
                new JLabel("You change your question here:"),
                allQuest,
                quest2,
                a2,
                b2,
                c2,
                answ2,};

            JOptionPane.showMessageDialog(null, input, "Change Question", JOptionPane.PLAIN_MESSAGE);
        }
    }
}

Upvotes: 1

Heshan
Heshan

Reputation: 172

you can use

questions.setText(alleQuest.getSelectedItem().toString());

inside the ActionListener. But questions textfileld should be defined above the actionPerformed method

Upvotes: 1

Related Questions