Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Upadating Jtable while entering the data

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    29
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Upadating Jtable while entering the data

    I have this program here which has a table and an add button. By default there are already two data. I want to implement it in such a way that when I add new item by clicking the add button, I want the table to be updated automatically. I have been searching for this thing for last 2 days. I have tried lots of things but none of them work.
    Probably I need to set setValueAt() method as well which I do not know how to implement in this situation. Here is my full code. You can test it.
    Any suggestions or helps are highly appreciated.

    public class TableDemo extends JPanel implements ActionListener{
     
    	private JLabel name, contact;
    	private JTextField nameField, contactField;
    	private static JButton addButton, ok;
     
    	JTable table;
    	JFrame myFrame;
    	MyTableModel myModel;
     
    	public TableDemo() {
    		super(new GridLayout(1, 0));
     
    		myModel = new MyTableModel(new Contact("Jeff", "456"));
    		table = new JTable(myModel);
     
    		table.setFillsViewportHeight(true);
     
    		table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    		table.setFillsViewportHeight(true);
     
    		// Create the scroll pane and add the table to it.
    		JScrollPane scrollPane = new JScrollPane(table);
     
    		// Add the scroll pane to this panel.
    		add(scrollPane);
    	}
     
    	class MyTableModel extends AbstractTableModel {
    		private String[] columnNames = { "Full Name", "Contact Number" };
     
    		ArrayList<Contact> dataList = new ArrayList();
     
    		public MyTableModel(Contact c) {
    			dataList.add(c);
    			dataList.add(new Contact("Mary", "456"));
    		}
     
    		public MyTableModel(ArrayList mylist) {
    			for (int i = 0; i < dataList.size(); i++) {
    				dataList.add((Contact) mylist.get(i));
    			}
    		}
     
    		public MyTableModel() {
    			// TODO Auto-generated constructor stub
    		}
     
    		public int getColumnCount() {
    			return columnNames.length;
    		}
     
    		public int getRowCount() {
    			return dataList.size();
    		}
     
    		public String getColumnName(int col) {
    			return columnNames[col];
    		}
     
    		public void setValueAt(Contact value, int row, int col) {
    			ArrayList object = dataList;
     
    			object.add(value);
     
    			dataList.set(row, value);
     
    			fireTableDataChanged();
    		}
     
    		public Object getValueAt(int row, int col) {
     
    			Contact widget = (Contact) dataList.get(row);
     
    			switch (col) {
    			case 0:
    				return widget.getFullName();
    			case 1:
    				return widget.getPhoneNumber();
    			default:
    				return null;
     
    			}
    		}
     
    		public boolean isCellEditable(int row, int col) {
     
    			return false;
    		}
     
    		public void addContact(Contact c) {
    			dataList.add(c);
    			myModel.fireTableDataChanged();
    			// fireTableRowsInserted(dataList.size() - 1, dataList.size() - 1);
    		}
     
    	}
     
    	private static void createAndShowGUI() {
    		// Create and set up the window.
    		JFrame frame = new JFrame("TableDemo");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		// Create and set up the content pane.
    		TableDemo newContentPane = new TableDemo();
    		newContentPane.setOpaque(true); // content panes must be opaque
    		frame.setContentPane(newContentPane);
    		addButton = new JButton("Add");
    		JPanel myPanel = new JPanel();
    		myPanel.add(addButton);
     
    		addButton.addActionListener(new TableDemo());
     
    		frame.setLayout(new GridLayout(2, 1));
    		frame.setResizable(false);
    		frame.setSize(500, 500);
    		frame.add(myPanel);
    		// Display the window.
    		frame.pack();
    		frame.setVisible(true);
    	}
     
    	public static void main(String[] args) {
    		// Schedule a job for the event-dispatching thread:
    		// creating and showing this application's GUI.
    		javax.swing.SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				createAndShowGUI();
    			}
    		});
    	}
     
    	public void actionPerformed(ActionEvent e) {
    		// TODO Auto-generated method stub
    		newFrame();
    	}
     
    	public void newFrame() {
    		myFrame = new JFrame();
    		JPanel myPanel = new JPanel();
    		name = new JLabel("Full Name");
    		nameField = new JTextField(10);
    		contact = new JLabel("Contact Number");
    		contactField = new JTextField(10);
     
    		ok = new JButton("OK");
     
    		ok.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				myModel.addContact(new Contact(nameField.getText(),
    						contactField.getText()));
    				myFrame.dispose();
    			}
    		});
     
    		myPanel.add(name);
    		myPanel.add(nameField);
    		myPanel.add(contact);
    		myPanel.add(contactField);
    		myPanel.add(ok);
     
    		myFrame.add(myPanel);
     
    		myFrame.setVisible(true);
    		myFrame.setSize(250, 250);
    	}
     
    }

    AND THE CONTACT CLASS IS HERE
    public class Contact {
    String fullName, phoneNumber;
     
    	public Contact(String fullName, String phoneNumber)
    	{
    		this.fullName = fullName;
    		this.phoneNumber = phoneNumber;
    	}
     
    	public String getFullName() {
    		return fullName;
    	}
     
    	public void setFullName(String fullName) {
    		this.fullName = fullName;
    	}
     
    	public String getPhoneNumber() {
    		return phoneNumber;
    	}
     
    	public void setPhoneNumber(String phoneNumber) {
    		this.phoneNumber = phoneNumber;
    	}
    }


  2. #2
    Member
    Join Date
    Apr 2014
    Posts
    93
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Upadating Jtable while entering the data

    Looks like you almost have it.
    myModel.fireTableDataChanged();
    // fireTableRowsInserted(dataList.size() - 1, dataList.size() - 1);
    Should be this:
    fireTableRowsInserted(dataList.size() - 1, dataList.size());
    Also, you're creating a 2nd TableDemo instance here:
    addButton.addActionListener(new TableDemo());
    So you're adding to that TableDemo's table instead of the one you expect. Try this:
    addButton.addActionListener(newContentPane);

  3. The Following User Says Thank You to BinaryDigit09 For This Useful Post:

    kindk12 (April 14th, 2014)

  4. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    29
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: Upadating Jtable while entering the data

    Wow thanks MAN
    I didn't realized it.
    Could you explain the first part.

  5. #4
    Member
    Join Date
    Apr 2014
    Posts
    93
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Upadating Jtable while entering the data

    fireTableRowsInserted needs to know the start and end indices of the row(s) that were inserted.

Similar Threads

  1. Reading Data in JTable
    By frozen java in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 18th, 2013, 08:43 PM
  2. Replies: 21
    Last Post: November 27th, 2012, 10:58 PM
  3. Data from .txt to JTable?
    By skywire_ in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: September 19th, 2011, 04:00 AM

Tags for this Thread