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

    Default How do i get my data from a 2d array and add it to a JTable

    hi,

    I have some textboxes that users will be able to enter information into. When they click ADD, the information is supposed to go into a JTable. I am having difficulty figuring out how to add this info from the textboxes to the 2d array and how to then add the info to the JTable. Here is my code:

    Java Code:
    public class Buttons extends JPanel 
    {
    	JPanel panel = new JPanel();
    	JFrame frame = new JFrame();
    	private 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);
    		
    		
    		 
    	}
    
    	
    	
    }
    Java Code:
    public class CustomerInformation extends JPanel
    {
    	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;
    	private JTextField text6;
    	
    	//Properties
    	public void setText1(JTextField _text1)
    	{
    		text1 = _text1;
    	}
    	
    	public JTextField getText1()
    	{
    		return text1;
    	}
    	
    	public void setText2(JTextField _text2)
    	{
    		text2 = _text2;
    	}
    	
    	public JTextField getText2()
    	{
    		return text2;
    	}
    	
    	public void setText3(JTextField _text3)
    	{
    		text3 = _text3;
    	}
    	
    	public JTextField getText3()
    	{
    		return text3;
    	}
    	
    	public void setText4(JTextField _text4)
    	{
    		text4 = _text4;
    	}
    	
    	public JTextField getText4()
    	{
    		return text4;
    	}
    	
    	public void setText5(JTextField _text5)
    	{
    		text5 = _text5;
    	}
    	
    	public JTextField getText5()
    	{
    		return 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");
    		
    		// Instantiate text boxes
    		text1 = new JTextField(10);
    		text2 = new JTextField(10);
    		text3 = new JTextField(10);
    		text4 = new JTextField(10);
    		text5 = new JTextField(10);
    		
    		// 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);
    	}
    	
    	
    }
    Java Code:
    public class CustomerDisplay extends JPanel
    {
    	JFrame frame = new JFrame();
    	JPanel panel = new JPanel();
    	JTextField text1 = new JTextField();
    	
    	//}
    public CustomerDisplay()
    {
    	MyTableModel myModel = new MyTableModel();
    	JTable table = new JTable(myModel);
    	//table.setPreferredScrollableViewportSize(new Dimension(500,700));
    	
    	JScrollPane scrollPane = new JScrollPane(table);
    	panel.add(scrollPane);
    	add(panel);
    	
    	
    }
    
    private class MyTableModel extends AbstractTableModel {
    	
        private String[] columnNames = {"First Name", "Last Name", "Customer Status", "Dependent Age", "Customer ID"};
        private Object[][] data = {{"","","","",""}};
    
        public int getColumnCount1() {
            return columnNames.length;
        }
        
        public int getRowCount1() {
            return data.length;
        }
    
        public String getColumnName(int col) {
            return columnNames[col];
        }
    
        public Object getValueAt1(int row, int col) {
            return data[row][col];
        }
    
        public Class getColumnClass(int c)
        {
            return getValueAt1(0, c).getClass();
        }
    
        /*
         * Don't need to implement this method unless your table's
         * editable.
         */
        public boolean isCellEditable(int row, int col) 
        {
            //Note that the data/cell address is constant,
            //no matter where the cell appears onscreen.
            return false;
        }
    
    	@Override
    	public int getColumnCount() {
    		// TODO Auto-generated method stub
    		return columnNames.length;
    	}
    
    	public int getRowCount(int row, int col) {
    		// TODO Auto-generated method stub
    		  return (Integer) data[row][col];
    	}
    
    	@Override
    	public Object getValueAt(int row, int col) {
    		// TODO Auto-generated method stub
    		return data[row][col];
    	}
    
    	@Override
    	public int getRowCount() {
    		// TODO Auto-generated method stub
    		  return data.length;
    	}
    
    	
    	
    	
    }
    
    
    }
    Java Code:
    public class Customer extends JPanel
    { 
    	
    	JPanel panel = new JPanel();
    	
    	public Customer()
    	{
    				
    		panel.setLayout(new BorderLayout());
    		panel.add(new Buttons(), BorderLayout.NORTH);
    		panel.add(new CustomerInformation(), BorderLayout.CENTER);
    		panel.add(new CustomerDisplay(), BorderLayout.SOUTH);
    		
    		add(panel);
    		
    		
    	}
    	
    
    
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    I am having difficulty figuring out how to add this info from the textboxes to the 2d array and how to then add the info to the JTable.
    There is not need to create a custom TableModel. Just use the DefaultTableModel. Then you can use the addRow(...) method to dynamically add rows to the table.

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

    Default

    Thank You!

    Here is my new code if anyone else is having the same issue:

    Java Code:
    public class CustomerDisplay extends JPanel
    {
    	JFrame frame = new JFrame();
    	JPanel panel = new JPanel();
    	private DefaultTableModel model;
    	private 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 [] first = {"Jane", "Doe","Active","31","2"};	
    	model.addRow(first);
    	
    	table = new JTable(model);
    	JScrollPane scrollPane = new JScrollPane(table);
    	panel.add(scrollPane);
    	add(panel);
    	
    	
    }
    
    
    }
    Also, I wanted to use an ArrayList instead of a regular array, but I wasn't able to.

Similar Threads

  1. Update JTable data
    By DC200 in forum AWT / Swing
    Replies: 8
    Last Post: 03-30-2010, 10:10 PM
  2. how to retrieve data from other jtable..
    By javanewbie2010 in forum AWT / Swing
    Replies: 1
    Last Post: 02-12-2010, 08:32 AM
  3. Save JTable Data
    By anilkumar_vist in forum New To Java
    Replies: 0
    Last Post: 02-08-2010, 12:11 PM
  4. how to refresh data of the JTable
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-17-2008, 12:01 PM
  5. How to change data in jtable?
    By chandu.v in forum AWT / Swing
    Replies: 2
    Last Post: 07-01-2008, 09:46 AM

Posting Permissions

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