Results 1 to 4 of 4
  1. #1
    geekchick is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default How to insert a row into JTable when clicking Add Button

    hi,

    i need some help with inserting rows into my JTable after a user enters information into textfields and clicks the add button. Right now, nothing is happening.


    Java Code:
    public class CustomerDisplay extends JPanel implements ActionListener
    {
      
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        static DefaultTableModel model;
        static JTable table;
        
         private String[] columnNames = {"First Name", "Last Name", "Customer Status", "Dependent Age", "Customer ID"};
        
    public CustomerDisplay()
    {
        super();
        model = new DefaultTableModel();
        model.addColumn("First Name");
        model.addColumn("Last Name");
        model.addColumn("Customer Status");
        model.addColumn("Dependent Age");
        model.addColumn("Customer ID");
        
      
       
         
       // String [] customer = {stringText1, stringText2, stringText3, stringText4, stringText5}; 
        //model.addRow(customer);
        
        table = new JTable(model);
        JScrollPane scrollPane = new JScrollPane(table);
        panel.add(scrollPane);
        add(panel);
        
        
    }
    
        
        public void actionPerformed(ActionEvent e)
        {
            //CustomerInformation cust3 = Customer.cust;
            //String [] first = {CustomerInformation.text1.getText()};  
            //model.addRow(first);
            //String stringText1 = cust3.text1.getText();
            //System.out.println(stringText1);
            
        }
            
        
        
    
    }
    Java Code:
    class Buttons extends JPanel implements ActionListener
    {
        JPanel panel = new JPanel();
        JFrame frame = new JFrame();
        protected JButton addButton;
        private JButton modifyButton;
        private JButton deleteButton;
     
    
        public Buttons()
        {  
          
            super();
            addButton = new JButton("Add");
            modifyButton = new JButton("Modify");
            deleteButton = new JButton("Delete");
      
            
            panel.setLayout(new FlowLayout());
            panel.add(addButton);
            panel.add(modifyButton);
            panel.add(deleteButton);
            add(panel);
            
            CustomerInformation cust1 = CustomerInformation.cust;
            
            addButton.addActionListener(cust1);
    
    
    
        }
    
          public void actionPerformed(ActionEvent e)
        {
            
              
        }
            
          
      
    }
    
     class Customer extends JPanel
    { 
     
        JPanel panel = new JPanel();
        
        public Customer()
        {
                    
            panel.setLayout(new BorderLayout());
            panel.add(new Buttons(), BorderLayout.NORTH);
            panel.add(CustomerInformation.cust, BorderLayout.CENTER);
            panel.add(new CustomerDisplay(), BorderLayout.SOUTH);
            
            add(panel);
            
            
        }
        
    
    
    }
    
      class CustomerInformation extends JPanel implements ActionListener
    {
      static CustomerInformation cust = new CustomerInformation();
        JPanel panel = new JPanel();
        // Declare JLabels
        private JLabel label1;
        private JLabel label2;
        private JLabel label3;
        private JLabel label4;
        private JLabel label5;
        
        // Declare JTextFields
        private JTextField text1;
        private JTextField text2; 
        private JTextField text3; 
        private JTextField text4; 
        private JTextField text5;
        
        
        
        public CustomerInformation()
        { 
            // Instantiate labels
            label1 = new JLabel("First Name");
            label2 = new JLabel("Last Name");
            label3 = new JLabel("Customer Status");
            label4 = new JLabel("Dependendt Age");
            label5 = new JLabel("Customer ID");
     
            text1 = new JTextField(10);
            text2 = new JTextField(10);
            text3 = new JTextField(10);
            text4 = new JTextField(10);
            text5 = new JTextField(10);
            
           text1.addActionListener(this);
           text2.addActionListener(this);
           text3.addActionListener(this);
           text4.addActionListener(this);
           text5.addActionListener(this);
           
           
            
            // Set the Grid Layout
            panel.setLayout( new GridLayout(5, 2));
            
            // Add items to grid
            panel.add(label1);
            panel.add(text1);
            panel.add(label2);
            panel.add(text2);
            panel.add(label3);
            panel.add(text3);
            panel.add(label4);
            panel.add(text4);
            panel.add(label5);
            panel.add(text5);
            
            
            add(panel);
            
       
        }
        
        
          public void actionPerformed(ActionEvent e)
        {
               String stringText1 = text1.getText();
               String stringText2 = text2.getText();
               String stringText3 = text3.getText();
               String stringText4 = text4.getText();
               String stringText5 = text5.getText();
              
              
               String [] first = {stringText1, stringText2, stringText3, stringText4, stringText5};
              CustomerDisplay.model.addRow(first);
               CustomerDisplay.model = (DefaultTableModel)CustomerDisplay.table.getModel();
              //System.out.println(stringText1);
              
              
             //for(int i = 0; i < Object.length; i++)
              //{
              //    System.out.println(Object[i]);
              //}
              
              
              
        }
            
         
        
        
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    1) Get rid of everything static except for your main method and associated methods (such as createAndShowUI).

    2) Give your classes public methods to allow them to interact with other classes.

    3) Solve your problem in a simplified fashion before extending it to your complex GUI.

    For instance, say I have three classes:

    i) FuDisplayPanel which displays a JTable showing first and last names (again, simplifly
    ii) FuButtonPanel which holds a single addInfoButton which when pressed adds a row to the JTable above, and
    iii) FuInformationPanel which holds two JTextFields, one for last name and one for first name, whose content is that which is added to the JTable's, I'd give them public methods like so:

    FuDisplayPanel would have a public addRow method which would accept Strings which would be used to make a row and insert it into the JTable's model.
    FuButtonPanel whould have an addInfoBtnAddActionListener method which would accept an ActionListener object which would then be added to the addInfoButton. This would be another wasy to allow outside classes to act on this class's button press.
    FuInformationPanel would have public getLastName() and getFirstName() methods that would extract the Strings from the JTextFields and return them so that outside classes can get and use these Strings.

    I'd then tie it all together in a main gui class (that also acts as the control class here), where I'd create and add the ActionListener to the button panel class.

    A working demo could look like so:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;
    
    public class FuMain {
       private static void createAndShowUI() {
    
          JFrame frame = new JFrame("Customer Main");
          frame.getContentPane().add(new FuGui(), BorderLayout.CENTER);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    
    class FuGui extends JPanel {
       FuDisplayPanel displayPanel = new FuDisplayPanel();
       FuButtonPanel buttonPanel = new FuButtonPanel(); 
       FuInformationPanel informationPanel = new FuInformationPanel();
    
       public FuGui() {
          JPanel bottomPanel = new JPanel();
          bottomPanel.add(buttonPanel);
          bottomPanel.add(Box.createHorizontalStrut(10));
          bottomPanel.add(informationPanel);
          
          setLayout(new BorderLayout());
          add(displayPanel, BorderLayout.CENTER);
          add(bottomPanel, BorderLayout.SOUTH);
          
          buttonPanel.addInfoBtnAddActionListener(new ActionListener() {
             
             public void actionPerformed(ActionEvent e) {
                String lastName = informationPanel.getLastName();
                String firstName = informationPanel.getFirstName();
                
                displayPanel.addRow(lastName, firstName);
             }
          });
       }
    }
    
    class FuDisplayPanel extends JPanel {
       private String[] COLUMNS = {"Last Name", "First Name"};
       private DefaultTableModel model = new DefaultTableModel(COLUMNS, 0);
       private JTable table = new JTable(model);
       
       public FuDisplayPanel() {
          setLayout(new BorderLayout());
          add(new JScrollPane(table));
       }
       
       public void addRow(String lastName, String firstName) {
          Object[] row = new Object[2];
          row[0] = lastName;
          row[1] = firstName;
          model.addRow(row);
       }
    }
    
    class FuButtonPanel extends JPanel {
       private JButton addInfoButton = new JButton("Add Information");
       
       public FuButtonPanel() {
          add(addInfoButton);
       }
       
       public void addInfoBtnAddActionListener(ActionListener listener) {
          addInfoButton.addActionListener(listener);
       }
    }
    
    class FuInformationPanel extends JPanel {
       private JTextField lastNameField = new JTextField(10);
       private JTextField firstNameField = new JTextField(10);
       
       public FuInformationPanel() {
          add(new JLabel("Last Name:"));
          add(lastNameField);
          add(Box.createHorizontalStrut(10));
          add(new JLabel("First Name:"));
          add(firstNameField);
       }
       
       public String getLastName() {
          return lastNameField.getText();
       }
       
       public String getFirstName() {
          return firstNameField.getText();
       }
    }

  3. #3
    geekchick is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default Solved

    Thank you! It worked. Here is my code for anyone else having a similiar issue:

    Java Code:
    import java.awt.Component;
    import java.awt.FlowLayout;
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.JTable;
    import javax.swing.JScrollPane;
    import java.awt.event.*;
    import java.awt.*;
    
    
    class Buttons extends JPanel
    {
        JPanel panel = new JPanel();
        JFrame frame = new JFrame();
        private JButton addButton = new JButton("Add");
        private JButton modifyButton = new JButton("Modify");
        private JButton deleteButton = new JButton("Delete");
     
    
        public Buttons()
        {  
          
           
            panel.setLayout(new FlowLayout());
            panel.add(addButton);
            panel.add(modifyButton);
            panel.add(deleteButton);
            add(panel);
            
            
            
        }
    
         public void addInfoBtnAddActionListener(ActionListener listener)
        {
            addButton.addActionListener(listener);
              
        }
        
         public void modifyInfoBtnAddActionListener(ActionListener listener)
        {
            modifyButton.addActionListener(listener);
              
        }
            
          
      
    }
    
     class Customer extends JPanel
    { 
     
        Buttons buttons = new Buttons();
        CustomerInformation customer = new CustomerInformation();
        CustomerDisplay display = new CustomerDisplay();
        
        public Customer()
        {
            JPanel panel = new JPanel();       
            panel.setLayout(new BorderLayout());
            panel.add(buttons, BorderLayout.NORTH);
            panel.add(customer, BorderLayout.CENTER);
            panel.add(display, BorderLayout.SOUTH);
            
            add(panel);
            
            buttons.addInfoBtnAddActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    String firstName = customer.getLastName();
                    String lastName = customer.getFirstName();
                    String status = customer.getStatus();
                    String age = customer.getAge();
                    String id = customer.getID();
                    
                    display.addRow(firstName, lastName, status, age, id);
                }
            });
            
            buttons.modifyInfoBtnAddActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                     String firstName = customer.getLastName();
                     String lastName = customer.getFirstName();
                     String status = customer.getStatus();
                     String age = customer.getAge();
                     String id = customer.getID();
                     
                     display.setData(firstName, 1, 0);
                    }
                });
               
        }
        
    
    
    }
    
    
      class CustomerInformation extends JPanel
    {
      
        JPanel panel = new JPanel();
        // Declare JLabels
        private JLabel label1 = new JLabel("First Name");
        private JLabel label2 = new JLabel("Last Name");
        private JLabel label3 = new JLabel("Customer Status");
        private JLabel label4 = new JLabel("Dependent Age");
        private JLabel label5  = new JLabel("Customer ID");
        
        // Declare JTextFields
        private JTextField text1 = new JTextField(10);
        private JTextField text2 = new JTextField(10);
        private JTextField text3 = new JTextField(10);
        private JTextField text4 = new JTextField(10);
        private JTextField text5 = new JTextField(10);
        
        
        
        public CustomerInformation()
        { 
            
            // Set the Grid Layout
            panel.setLayout( new GridLayout(5, 2));
            
            // Add items to grid
            panel.add(label1);
            panel.add(text1);
            panel.add(label2);
            panel.add(text2);
            panel.add(label3);
            panel.add(text3);
            panel.add(label4);
            panel.add(text4);
            panel.add(label5);
            panel.add(text5);
            
            
            add(panel);     
       
        }
        
        public String getLastName()
        {
            return text1.getText();
        }
        
        public String getFirstName()
        {
            return text2.getText();
        }
        
        public String getStatus()
        {
            return text3.getText();
        }
        
        public String getAge()
        {
            return text4.getText();
        }
        
         public String getID()
        {
            return text5.getText();
        }
        
        
        
    }
    Java Code:
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JTextField;
    import javax.swing.table.AbstractTableModel;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.JButton;
    
    
    public class CustomerDisplay extends JPanel
    {
      
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        private String[] columnNames = {"First Name", "Last Name", "Customer Status", "Dependent Age", "Customer ID"};
        private DefaultTableModel model = new DefaultTableModel(columnNames, 0);
        private JTable table = new JTable(model);
        
        
    public CustomerDisplay()
    {
        JScrollPane scrollPane = new JScrollPane(table);
        panel.add(scrollPane);
        add(panel);
        
        
    }
    
    public void addRow(String firstName, String lastName, String custStatus, String custAge, String custID)
    {
        Object [] row = new Object [5];
        row[0] = firstName;
        row[1] = lastName;
        row[2] = custStatus;
        row[3] = custAge;
        row[4] = custID;
        model.addRow(row);
    }
    
    public void setData(Object obj, int row_index, int col_index)
    {
        table.getModel().setValueAt(obj, row_index, col_index);
    }    
           
    
    }
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class AirlineReservationDemo {
    
     private static void createAndShowUI()
     {
         
    		 JPanel panel = new JPanel();
    
            Customer cust = new Customer();
            JTabbedPane tab = new JTabbedPane();
            JFrame frame = new JFrame("Airline");
            tab.add("Customer", cust);
            tab.add("Flight", null);
            tab.add("Reservations", null);
            frame.add(tab, BorderLayout.CENTER);
            frame.setSize(800,500);
            frame.setVisible(true);
      }
    	public static void main(String[] args) 
    	{
            
           java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
    
    	}
    
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. right clicking JTable row
    By Stephen Douglas in forum New To Java
    Replies: 2
    Last Post: 06-27-2010, 11:41 PM
  2. Replies: 3
    Last Post: 06-27-2010, 02:57 AM
  3. Get source web page after clicking next Button
    By noob in forum New To Java
    Replies: 1
    Last Post: 03-05-2010, 09:01 PM
  4. Sorting in JTable without clicking on column headers
    By ProgrammingPup in forum Advanced Java
    Replies: 6
    Last Post: 11-04-2009, 05:26 AM
  5. how to insert button in jfreechart
    By santhosh_el in forum AWT / Swing
    Replies: 3
    Last Post: 03-14-2009, 02:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •