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 2 of 2

Thread: Reading Data in JTable

  1. #1
    Member
    Join Date
    Jan 2011
    Posts
    88
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Reading Data in JTable

    As the title suggests I am trying to read the data that is dynamically entered by the user. This is my first time using JTables.

    This is how I am reading the data:
    public void readData() {
    		for(int i=0; i < rows.length; i++) {
    			for (int j=0; j < 7; j++) {
    				System.out.println(rows[i][j]);
    			}
    		}
     
    	}


    I think what my problem is here is that the rows array is not being updated as a user inputs information. I've tried implementing TableModelListener but when I do it returns an error when I add more rows to the table.

    Here is the code
    package sycorp.iface;
     
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Vector;
     
    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;
     
     
    public class Gui extends JPanel implements ActionListener {
     
    	// Declare JFrame
    	private JFrame f = new JFrame("Table Test");
     
    	// Declare Menus
    	private JMenuBar mb = new JMenuBar();
    	private JMenu muFile = new JMenu("File");
    	private JMenuItem muItemClose = new JMenuItem("Close");
     
    	// Declare Buttons
    	private JButton addNew = new JButton("Add New");
    	private JButton read = new JButton("Read Data");
     
    	// Table
    	private String columns[] = {"Make", "Model", "Year", "Miles", "Condition", "Price", "Notes"};
    	private String rows[][] = {{}};
    	private JTable jt = new JTable(rows, columns);
     
    	// Declare Panels
    	private JScrollPane dataPane = new JScrollPane(jt);
     
    	public Gui() {
     
    		f.setJMenuBar(mb); // Add the menu bar to the JFrame
    		jt.setPreferredScrollableViewportSize(new Dimension(865, 600)); // Set the Deminsions of the table
    		jt.setFillsViewportHeight(true); // Set it to the correct height
    		jt.setModel(new DefaultTableModel(rows, columns));
     
    		// File Menu
    		mb.add(muFile); // Add the file menu to the menu bar
    		muFile.add(muItemClose); // Add the close options to the file menu
     
    		// Button
    		mb.add(addNew);
    		mb.add(read);
     
    		// Add Action Listener
    		muItemClose.addActionListener(this); // Add action listener to muItemClose
    		addNew.addActionListener(this); // Add action listener to addNew button
    		read.addActionListener(this); // Add action listener to remove button
     
    		// Set Action Command
    		muItemClose.setActionCommand("close"); // Set command for muItemClose
    		addNew.setActionCommand("addNew"); // Set command for addNew button
    		read.setActionCommand("read"); // set command for remove button
     
    	}
     
    	public void launchFrame() {
    		try {
    			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); // Set the theme
    			Container pane = f.getContentPane(); // Create the frame
    			pane.add(dataPane);  // Add the pane
    			f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set close operation
    			f.setVisible(true); // Make it visible
    			f.setResizable(true); // Make it resizable
    			f.setSize(865, 600); // Set the default size
    		} catch (ClassNotFoundException e) {
    			e.printStackTrace();
    		} catch (InstantiationException e) {
    			e.printStackTrace();
    		} catch (IllegalAccessException e) {
    			e.printStackTrace();
    		} catch (UnsupportedLookAndFeelException e) {
    			e.printStackTrace();
    		}
     
    	}
     
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		String cmd = e.getActionCommand();
     
    		switch(cmd) {
    			case "close":
    				System.exit(0);
    				break;
     
    			case "addNew":
    				newRow();
    				break;
     
    			case "read":
    				readData();
     
    		}
     
    	}
     
    	public void newRow() {
    		((DefaultTableModel) jt.getModel()).addRow(new Vector(6));
    	}
     
    	public void readData() {
    		for(int i=0; i < rows.length; i++) {
    			for (int j=0; j < 7; j++) {
    				System.out.println(rows[i][j]);
    			}
    		}
     
    	}
     
    	public void messageBox(String msg) {
    		JOptionPane.showMessageDialog(f, msg);
    	}
     
    	public String inputBox(String msg) {
    		return null;
    	}
     
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Reading Data in JTable

    What error are you seeing when you try to use a TableModelListener? I don't see any code posted above where you try to use this. Can you post this attempt, and the errors that you see?

Similar Threads

  1. data won't show up in jtable
    By takamine in forum Object Oriented Programming
    Replies: 10
    Last Post: February 6th, 2013, 07:18 AM
  2. Replies: 21
    Last Post: November 27th, 2012, 09: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