Answer to Question #294577 in Java | JSP | JSF for fsdjsk

Question #294577

Build a category gui that adds different kinds of categories to arraylist. Then populate this data to JCombobox in another gui frame.


1
Expert's answer
2022-02-06T17:17:22-0500
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        JFrame comboBoxFrame = new JFrame("ComboBox");
        JPanel comboBoxPanel = new JPanel();
        comboBoxPanel.setLayout(new BorderLayout());
        JComboBox<String> comboBox = new JComboBox<>();
        JPanel panel = new JPanel();
        panel.add(comboBox);
        comboBoxPanel.add(panel, BorderLayout.CENTER);
        comboBoxFrame.add(comboBoxPanel);

        ArrayList<String> arrayList = new ArrayList<>();
        JFrame inputFrame = new JFrame("Input");
        JPanel inputPanel = new JPanel();
        inputPanel.setLayout(new BorderLayout());
        JTextField textField = new JTextField();
        textField.setColumns(10);
        JButton addJButton = new JButton("Add");
        addJButton.addActionListener(e -> {
            if (textField.getText().length() > 0) {
                arrayList.add(textField.getText());
            }
        });
        JButton comboBoxJButton = new JButton("To ComboBox");
        comboBoxJButton.addActionListener(e -> {
            for (String item : arrayList) {
                comboBox.addItem(item);
            }
        });
        JPanel textPanel = new JPanel();
        textPanel.add(textField);
        JPanel buttonsPanel = new JPanel(new FlowLayout());
        buttonsPanel.add(addJButton);
        buttonsPanel.add(comboBoxJButton);
        inputPanel.add(textPanel, BorderLayout.CENTER);
        inputPanel.add(buttonsPanel, BorderLayout.SOUTH);
        inputFrame.add(inputPanel);


        inputFrame.setSize(200, 250);
        inputFrame.setResizable(false);
        inputFrame.setVisible(true);
        inputFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        comboBoxFrame.setSize(200, 250);
        comboBoxFrame.setResizable(false);
        comboBoxFrame.setVisible(true);
        comboBoxFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS