Search code examples
javaswingarraylistjcheckbox

How to Add JTextField & JCheckBox to ArrayList Object


I'm working on my very first GUI app.

I have an ArrayList of the Parent Class Plant of which I have four child classes.

I have some JTextFields with some information and some JCheckBoxs with other information...

The JTextFields should be converted into Strings and the JCheckBoxs into boolean.

I need to add all of these components to an ArrayList and then display the list of the Plants.

How can I do that?

Example:

public class Flower extends Plant{

   private boolean thorns;
   private boolean smell;   

   public Flower(String name, String id, String color, boolean blnThorns, boolean blnSmell){
      super(name, id, color);
      thorns = blnThorns;
      smell = blnSmell;

   }

   public boolean isThorns(){

      return thorns;
   }

   public void setThorns(boolean blnThorns){
      thorns = blnThorns;  
   }

   public boolean isSmell(){

      return smell;
   }

   public void setSmell(boolean blnSmell){
      smell = blnSmell;  
   }


}

This is my ArrayList

ArrayList<Plant> plantList = new ArrayList<Plant>();

Here I try to add the parameters for the Flower:

private void addFlower(ArrayList<Plant> plantList){

    //Adding window
    JFrame addingFrame = new JFrame();
    addingFrame.setTitle("Plant Database Interface");
    addingFrame.setSize(600, 200);
    addingFrame.setLocationRelativeTo(null);
    addingFrame.setVisible(true);

    //ADDING FRAME LAYOUT
    addingFrame.setLayout(new BorderLayout());

    //TRAITS
    JPanel fields = new JPanel();
    addingFrame.add(fields, BorderLayout.NORTH);

    JLabel nameLabel = new JLabel("Name:");      
    JTextField  nameField = new JTextField(15);
    String name = nameField.getText();


    JLabel idLabel = new JLabel("ID:");      
    JTextField  idField  = new JTextField(10);
    String id = idField.getText();

    JLabel colorLabel = new JLabel("Color:");      
    JTextField  colorField = new JTextField(10);
    String color = colorField.getText();

    fields.add(nameLabel);
    fields.add(nameField);

    fields.add(idLabel);
    fields.add(idField);

    fields.add(colorLabel);
    fields.add(colorField);

    JPanel traits = new JPanel();
    addingFrame.add(traits , BorderLayout.CENTER );

    JCheckBox thornsBox = new JCheckBox("Thorns Present");
    thornsBox.setSelected(false);
    traits.add(thornsBox);

    JCheckBox smellBox = new JCheckBox("Smell Present");
    smellBox.setSelected(false);
    traits.add(smellBox);

    JPanel southPanel = new JPanel();
    addingFrame.add(southPanel, BorderLayout.SOUTH);

    JButton addPlantBtn = new JButton("Add Plant");
    southPanel.add(addPlantBtn, BorderLayout.EAST);

    JButton cancelBtn = new JButton("Cancel");
    southPanel.add(cancelBtn, BorderLayout.WEST);

    boolean blnThorns;
    boolean blnSmell;

    //I REALLY DON'T KNOW HOW TO DO THIS PART
    thornsBox.addItemListener(new ItemListener(){
        public void itemStateChanged(ItemEvent e){
            if(e.getStateChange() == ItemEvent.SELECTED){
                blnThorns =true;
            }
            else{
                blnThorns = false;
            }
        }
    });

    smellBox.addItemListener(new ItemListener(){
        public void itemStateChanged(ItemEvent e){
            if(e.getStateChange() == ItemEvent.SELECTED){
                blnSmell =true;
            }
            else{
                blnSmell = false;
            }
        }
    });               

    addPlantBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //plantList.add(new Flower(name, id, color, blnThorns, blnSmell)); //HOW TO DO????................
        }
    });


    cancelBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            addingFrame.dispatchEvent(new WindowEvent(addingFrame, WindowEvent.WINDOW_CLOSING));
        }
    });
}

Solution

  • Use your addPlantBtn ActionListener to gather the information you need when it's called

    addPlantBtn.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            Flower flower = new Flower(nameField.getText(), idField.getText(), colorField.getText(), thornsBox.isSelected(), smellBox.isSelected());
            plantList.add(flower);
    

    You would, also, find it easier, if you created a dedicated JPanel, designed to gather the user details and generate a Flower when you requested it to (from the values of the fields). You could then use this panel on some kind of dialog when ever you needed it.

    Have a look at How to Make Dialogs for more details

    For example...

    public class FlowerPane extends JPanel {
    
        JTextField nameField = new JTextField(15);
        JTextField idField = new JTextField(10);
        JTextField colorField = new JTextField(10);
        JCheckBox smellBox = new JCheckBox("Smell Present");
        JCheckBox thornsBox = new JCheckBox("Thorns Present");
    
        public FlowerPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.WEST;
            gbc.insets = new Insets(2, 2, 2, 2);
    
            JLabel nameLabel = new JLabel("Name:");
            JLabel idLabel = new JLabel("ID:");
            JLabel colorLabel = new JLabel("Color:");
    
            add(nameLabel, gbc);
            gbc.gridy++;
            add(idLabel, gbc);
            gbc.gridy++;
            add(idLabel, gbc);
    
            gbc.gridx++;
            gbc.gridy = 0;
            add(nameField, gbc);
            gbc.gridy++;
            add(idField, gbc);
            gbc.gridy++;
            add(colorField, gbc);
    
            gbc.gridx = 0;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
    
            add(thornsBox, gbc);
            gbc.gridy++;
            add(smellBox, gbc);
        }
    
        public Flower create() {
            return new Flower(nameField.getText(), idField.getText(), colorField.getText(), thornsBox.isSelected(), smellBox.isSelected());
        }
    
    }
    

    Which you could then use by doing something like...

    FlowerPane flowerPane = new FlowerPane();
    switch (JOptionPane.showConfirmDialog(null, flowerPane, "Flower", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE)) {
        case JOptionPane.OK_OPTION:
            Flower flower = flowerPane.create();
            plantList.add(flower);
            break;
    }
    

    Or add it to some other container