Build a category gui that adds different kinds of categories to arraylist. Then populate this data to JCombobox in another gui frame.
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);
}
}
Comments
Leave a comment