2 Replies Latest reply: Apr 25, 2013 9:58 AM by 993724 RSS

    repeating JButton action with GridBagLayout

    993724
      Pls, I have a problem with this code as following

      I've adjusted the layout using GridBagLayout as I wanted and made the add button to add the new components below the first ones declared
      what I want is how to each time I press this button to add the same components with the same configuration as the previous one but below it.

      here is the code for the implementation class

      /*
      * To change this template, choose Tools | Templates
      * and open the template in the editor.
      */
      package JavaList;

      import java.awt.BorderLayout;
      import java.awt.Component;
      import java.awt.Container;
      import java.awt.FlowLayout;
      import java.awt.GridBagConstraints;
      import java.awt.GridBagLayout;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import javax.swing.JButton;
      import javax.swing.JComboBox;
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.JPanel;
      import javax.swing.JTextField;

      /**
      *
      * @author ep08363
      */
      public class JobList extends JFrame
      {
      //initialize all the variables needed
      private String[] engineerNames = {"Mohamad Samy", "Ahmad El_Namoury",
      "Ahmad Ismail", "Mohamad EL_Gazar",
      "Mohamad Tolba", "Mohamad El_Askary"};

      private String[] plantNames = {"Qar'a", "A.M LPG", "Shipping"};
      private String[] progressJcomboBox = {"In Progress", "Finished", "Waiting SD"};
      private Container container;
      private GridBagLayout layout;
      private JPanel jobComponentsPanel;
      private JPanel namesJpanel;
      private JPanel labelJpanel;
      private JComboBox namesJComboBox;
      private JComboBox plantComboBox;
      private JComboBox progressComboBox;
      private JTextField jobDescriptionJtextField;
      private JTextField jobNoJtextField;
      private JLabel jobDescriptionJlabel;
      private JLabel plantJlabel;
      private JLabel prgressJlabel;
      private JLabel jobNoJlabel;
      private JButton addButton;
      private GridBagConstraints constraints;
      private JPanel mainComponentsPanel;

      private JTextField jobDescriptionJtextFieldSub;
      private JComboBox plantComboBoxSub;
      private JComboBox progressComboBoxSub;
      private JTextField jobNoJtextFieldSub;

      public JobList()
      {
      super("Hand Over Qar'a Office");


      //set the layout for the container
      Container container = getContentPane();
      constraints = new GridBagConstraints();
      layout = new GridBagLayout();
      mainComponentsPanel = new JPanel();
      mainComponentsPanel.setLayout(layout);

      //set the drop list for the engineers names
      namesJComboBox = new JComboBox(engineerNames);
      addButton = new JButton("Add Job");
      jobDescriptionJlabel = new JLabel("Job Description");
      plantJlabel = new JLabel("Plant");
      prgressJlabel = new JLabel("Progress");
      jobNoJlabel = new JLabel("Job No.");
      jobDescriptionJtextField = new JTextField();
      plantComboBox = new JComboBox(plantNames);
      progressComboBox = new JComboBox(progressJcomboBox);
      jobNoJtextField = new JTextField();
      //Define the constraints for the engineer names

      constraints.weightx = 0;
      constraints.anchor = GridBagConstraints.WEST;
      addComponent( namesJComboBox, 0, 0, 1, 1 );
      mainComponentsPanel.add(namesJComboBox);

      //Button won't be modified when resized
      constraints.weightx = 0;
      constraints.anchor = GridBagConstraints.CENTER;
      addComponent( addButton, 0, 0, 1, 1 );
      mainComponentsPanel.add(addButton);

      //Description label won't be modified when resized
      constraints.fill = GridBagConstraints.HORIZONTAL;
      constraints.weightx = 0;
      addComponent( jobDescriptionJlabel, 1, 0, 1, 1 );
      mainComponentsPanel.add(jobDescriptionJlabel);


      constraints.fill = GridBagConstraints.HORIZONTAL;
      constraints.weightx = 0;
      addComponent( plantJlabel, 1, 3, 1, 1 );
      mainComponentsPanel.add(plantJlabel);


      constraints.fill = GridBagConstraints.HORIZONTAL;
      constraints.weightx = 0;
      addComponent( prgressJlabel, 1, 4, 1, 1 );
      mainComponentsPanel.add(prgressJlabel);


      constraints.fill = GridBagConstraints.HORIZONTAL;
      constraints.weightx = 0;
      addComponent( jobNoJlabel, 1, 5, 1, 1 );
      mainComponentsPanel.add(jobNoJlabel);


      constraints.fill = GridBagConstraints.BOTH;
      constraints.weightx = 100;
      addComponent( jobDescriptionJtextField, 2, 0, 1, 3 );
      mainComponentsPanel.add(jobDescriptionJtextField);


      constraints.fill = GridBagConstraints.HORIZONTAL;
      constraints.weightx = 0;
      addComponent( plantComboBox, 2, 3, 1, 1 );
      mainComponentsPanel.add(plantComboBox);


      constraints.fill = GridBagConstraints.HORIZONTAL;
      constraints.weightx = 0;
      addComponent( progressComboBox, 2, 4, 1, 1 );
      mainComponentsPanel.add(progressComboBox);


      constraints.fill = GridBagConstraints.BOTH;
      constraints.weightx = 4;
      addComponent( jobNoJtextField, 2, 5, 0, 1 );
      mainComponentsPanel.add(jobNoJtextField);

      getContentPane().add(mainComponentsPanel, BorderLayout.NORTH);

      //event handling method for add button
      addButton.addActionListener(
      new ActionListener()
      {
      public void actionPerformed(ActionEvent event)
      {
      jobDescriptionJtextFieldSub = new JTextField();
      plantComboBoxSub = new JComboBox(plantNames);
      jobNoJtextFieldSub = new JTextField();
      GridBagConstraints constraints1 = new GridBagConstraints();

      constraints1.fill = GridBagConstraints.BOTH;
      constraints1.weightx = 100;
      addComponent( jobDescriptionJtextFieldSub, 4, 0, 1, 3 );
      mainComponentsPanel.add(jobDescriptionJtextFieldSub);

      constraints1.fill = GridBagConstraints.BOTH;
      constraints1.weightx = 0;
      addComponent( plantComboBoxSub, 4, 3, 1, 1 );
      mainComponentsPanel.add(plantComboBoxSub);

      constraints1.fill = GridBagConstraints.BOTH;
      constraints1.weightx = 0;
      addComponent( new JComboBox(progressJcomboBox), 4, 4, 1, 1 );
      mainComponentsPanel.add(new JComboBox(progressJcomboBox));

      constraints1.fill = GridBagConstraints.BOTH;
      constraints1.weightx = 4;
      addComponent( jobNoJtextFieldSub, 4, 5, 0, 1 );
      mainComponentsPanel.add(jobNoJtextFieldSub);
      add(mainComponentsPanel, BorderLayout.NORTH);
      setVisible(true);
      validate ();
      repaint();


      }
      });
      }




      private void addComponent(Component component, int row, int column, int width, int height)
      {
      constraints.gridx = column; // set gridx
      constraints.gridy = row; // set gridy
      constraints.gridwidth = width; // set gridwidth
      constraints.gridheight = height; // set gridheight
      layout.setConstraints( component, constraints ); // set constraints
      };
      }



      ***and the following class is to the main class**/**





      * To change this template, choose Tools | Templates
      * and open the template in the editor.
      */
      package JavaList;
      import javax.swing.JFrame;

      /**
      *
      * @author ep08363
      */
      public class JobListTest {
      public static void main(String[] args)
      {
      JobList JobListFrame = new JobList();
      JobListFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      JobListFrame.setSize(800, 500);
      JobListFrame.setVisible(true);
      }

      }