'How to use Object from other class in a JCombobox
I'm new at Java and trying to learn so my apologies if it seems obvious to you :(
I'm trying to import an ArrayList from another class to my JCombobox. When I import the arraylist in the Combobox from his own class it works but when I call it in the other class it doesn't work I don't know why. I had extended the class btw and the list isn't private.
My arraylist is empty at first so maybe this is the problem? Is it possible to save the new arraylist at the end of the execution of my frame? This is the code where the Arraylist is located :
public class MainForm {
private JTextField textField1;
private JButton resetButton;
private JButton saveButton;
private JTable tablePort;
public JSpinner spinner1;
private JButton updateButton;
private JPanel jpan;
private JScrollPane porttabl;
private JSplitPane rootPanel;
private JComboBox comboBox1;
public List<Port> ports;
private tablePort model;
private SpinnerModel limit;
private Port selectport;
private int selectedIndex;
public List<Port> getPorts() {
return ports;
}
public List<Port> setPorts(List<Port> a){
this.ports = a;
return ports;
}
public MainForm() {
ports = new ArrayList<Port>();
model = new tablePort(ports);
tablePort.setModel(model);
limit = new SpinnerNumberModel(0,0,10,1);
spinner1.setModel(limit);
saveButton.addActionListener(e-> {
if(textField1.getText().equals("")){
JOptionPane.showMessageDialog(null,"Il faut rentrer un nom pour votre bâteau","Erreur",0);
}else {
int p = (int) spinner1.getValue();
Port a = new Port(textField1, 0, 0, spinner1);
ports.add(a);
model.fireTableDataChanged();
comboBox1.setModel(new DefaultComboBoxModel(ports.toArray()));
setPorts(ports);
System.out.println(ports);
System.out.println(model);
System.out.println(a.getNom());
System.out.println(a.quais.nbQuai);
clear();
}
});
And here where i want my list :
public class TablBateau extends MainForm {
private JPanel panel1;
private JTextField textField1;
private JComboBox comboBox1;
private JCheckBox enMerCheckBox;
private JSplitPane rootPanel;
private JComboBox comboBox2;
private JTable table1;
private JButton button1;
private JButton resetButton;
private JButton button2;
private JButton button3;
public List<Bateau> bateaux;
public TablBateau() {
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
comboBox1.setModel(new DefaultComboBoxModel(ports.toArray()));
}
Solution 1:[1]
The key to your problem is to pass the information to where it is needed by using composition. Objects that need to extract information from another object hold variables of those objects and then call methods on them:
public class TablBateau extends MainForm {
private JPanel panel1;
private JTextField textField1;
private JComboBox comboBox1;
private JCheckBox enMerCheckBox;
private JSplitPane rootPanel;
private JComboBox comboBox2;
private JTable table1;
private JButton button1;
private JButton resetButton;
private JButton button2;
private JButton button3;
public List<Bateau> bateaux;
private MainForm mainForm; // add this
// pass the MainForm into this clas
public TablBateau(MainForm mainForm) {
this.mainForm = mainForm;
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
comboBox1.setModel(new DefaultComboBoxModel(mainForm.getPorts().toArray()));
}
You pass the currently displayed MainForm instance into your TablBateau instance when you create the latter, by passing it in as a parameter:
public class MainForm {
someListener() {
TablBateau tablBateau = new TablBateau(this);
}
}
For example,
class PassingInfo:
- Has the main method that starts the Swing GUI
- Creates a MainForm object, puts it into a JFrame, and displays it
class Port
- Dummy class to represent a Port object.
- This is obviously not the Port class that you are using (but I don't have your code, so I have to use this placeholder class instead)
class MainForm
- Extends JPanel so that it can be placed into a JFrame or JDialog, or another JPanel, wherever it is needed
- Holds a
List<Port> portsvariable that is filled with data from a dummy String array - JButton that displays a modal JDialog that holds the TablBateau JPanel
- Passes the ports list into TablBateau whenever the latter is displayed (not done in constructor in case the ports Port list is changed)
- Displays the above in a modal JDialog so that we will know when a selection is made.
- After a selection is made (code resumes from past the JDialog being made visible), extract the selection from the TablBateau instance and display in this object's JTextField.
class TablBateau
- Extends JPanel so it can be placed into a JFrame or JDialog or other JPanel,...
- Displays a JComboBox of Ports
- Has a
setPorts(...)method that fills the JComboBox. - ActionListener added to combobox that will dispose of the window that contains this JPanel.
import java.awt.Dialog.ModalityType;
import java.awt.Window;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class PassingInfo {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
MainForm mainPanel = new MainForm();
JFrame frame = new JFrame("GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
});
}
}
class MainForm extends JPanel {
private static final String[] PORTS = {"Here", "There", "Everywhere", "Yes", "No", "You", "Know", "What", "I", "Mean"};
private List<Port> ports;
private Port selectedPort = null;
private TablBateau tablBateau;
private JDialog tablBateauDlg;
private JTextField selectedPortField = new JTextField(20);
public MainForm() {
ports = Arrays.stream(PORTS).map(Port::new).collect(Collectors.toList());
JButton showTablBateauButton = new JButton("Show Tabl Bateau");
showTablBateauButton.addActionListener(e -> showTablBateau());
add(showTablBateauButton);
add(new JLabel("Selected Port:"));
add(selectedPortField);
int eb = 40;
setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb));
}
public List<Port> getPorts() {
return ports;
}
private void showTablBateau() {
if (tablBateau == null) {
tablBateau = new TablBateau();
Window windowOwner = SwingUtilities.getWindowAncestor(this);
tablBateauDlg = new JDialog(windowOwner, "Tabl Bateau", ModalityType.APPLICATION_MODAL);
tablBateauDlg.add(tablBateau);
tablBateauDlg.pack();
tablBateauDlg.setLocationByPlatform(true);
}
tablBateau.setPorts(ports);
tablBateauDlg.setVisible(true);
selectedPortField.setFocusable(false);
selectedPort = tablBateau.getSelectedPort();
selectedPortField.setText(selectedPort.getName());
}
}
class TablBateau extends JPanel {
private JComboBox<Port> comboBox1 = new JComboBox<>();
private List<Port> ports;
public TablBateau() {
add(comboBox1);
comboBox1.setPrototypeDisplayValue(new Port("ABCDEFGHIJKLMNOP"));
comboBox1.addActionListener(e -> itemSelected());
int eb = 20;
setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb));
}
private void itemSelected() {
Window windowOwner = SwingUtilities.getWindowAncestor(this);
windowOwner.dispose();
}
public Port getSelectedPort() {
return (Port) comboBox1.getSelectedItem();
}
public void setPorts(List<Port> ports) {
this.ports = ports;
DefaultComboBoxModel<Port> comboModel = new DefaultComboBoxModel<Port>(ports.toArray(new Port[] {}));
comboBox1.setModel(comboModel);
comboBox1.setSelectedIndex(-1);
}
}
class Port {
String name;
public Port(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Port: " + getName();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
