Results 1 to 4 of 4
  1. #1
    kulangotski is offline Senior Member
    Join Date
    Feb 2009
    Posts
    117
    Rep Power
    0

    Default Dynamically changing the contents of a jTable

    is it possible to reload the contents of a jTable without instantiating the class where the table itself resides?.

    I have a jtable that can hold table data.

    at startup it has the data from 'TABLE 1', what I would wish to do is to change the content of the jtable to 'TABLE 2'.

    I have a method that I use to populate the tables and it works fine on the first instantiation. but when I try to call it again to change the content, the contents does not seem to move.

    are there other methods that I can use to change to jtable contents upon a certain action?

    thanks for reading. :D

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    Look into using a TableModel. The JTable tutorial shows you how.

    If you want help with code, you'll have to post the code in SSCCE form.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    kulangotski is offline Senior Member
    Join Date
    Feb 2009
    Posts
    117
    Rep Power
    0

    Default

    hello sirs, this is my humble code. hope this is readable enough.

    by the way all the action here is on the populateTable() method(which is not working), the rest is just for the GUI code auto generated with the help of eclipse

    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import java.awt.Rectangle;
    import java.util.ArrayList;
    
    import javax.swing.JButton;
    import javax.swing.table.DefaultTableModel;
    
    //using eclipse helios 
    
    public class DynamicTable {
    
    	private JFrame jFrame = null;  //  @jve:decl-index=0:visual-constraint="36,7"
    	private JPanel jContentPane = null;
    	private JScrollPane jScrollPane = null;  //  @jve:decl-index=0:visual-constraint="711,89"
    	private JScrollPane jScrollPane1 = null;
    	private JTable jTable = null;
    	private JButton jButton = null;
    	private JButton jButton1 = null;
    	public int opt = 1;
    	/**
    	 * This method initializes jButton1	
    	 * 	
    	 * @return javax.swing.JButton	
    	 */
    
    
    	/**
    	 * This method initializes jFrame	
    	 * 	
    	 * @return javax.swing.JFrame	
    	 */
    	public static void main (String[] args) {
    			DynamicTable dynamicToBe = new DynamicTable();
    			dynamicToBe.getJFrame().setVisible(true);
    			dynamicToBe.populateTable(1); // use 1 as the first value upon first start up
    	}
    	
    	private JFrame getJFrame() {
    		if (jFrame == null) {
    			jFrame = new JFrame();
    			jFrame.setSize(new Dimension(593, 296));
    			jFrame.setContentPane(getJContentPane());
    		}
    		return jFrame;
    	}
    
    	/**
    	 * This method initializes jContentPane	
    	 * 	
    	 * @return javax.swing.JPanel	
    	 */
    	private JPanel getJContentPane() {
    		if (jContentPane == null) {
    			jContentPane = new JPanel();
    			jContentPane.setLayout(null);
    			jContentPane.add(getJScrollPane1(), null);
    			jContentPane.add(getJButton(), null);
    			jContentPane.add(getJButton1(), null);
    		}
    		return jContentPane;
    	}
    
    	/**
    	 * This method initializes jScrollPane	
    	 * 	
    	 * @return javax.swing.JScrollPane	
    	 */
    	private JScrollPane getJScrollPane() {
    		if (jScrollPane == null) {
    			jScrollPane = new JScrollPane();
    		}
    		return jScrollPane;
    	}
    
    	/**
    	 * This method initializes jScrollPane1	
    	 * 	
    	 * @return javax.swing.JScrollPane	
    	 */
    	private JScrollPane getJScrollPane1() {
    		if (jScrollPane1 == null) {
    			jScrollPane1 = new JScrollPane();
    			jScrollPane1.setBounds(new Rectangle(35, 22, 505, 133));
    			jScrollPane1.setViewportView(getJTable());
    		}
    		return jScrollPane1;
    	}
    
    	/**
    	 * This method initializes jTable	
    	 * 	
    	 * @return javax.swing.JTable	
    	 */
    	private JTable getJTable() {
    		
    		
    		if (jTable == null) {
    			jTable = new JTable();
    		}
    		
    		
    		return jTable;
    	}
    
    	/**
    	 * This method initializes jButton	
    	 * 	
    	 * @return javax.swing.JButton	
    	 */
    	private JButton getJButton1() {
    		if (jButton1 == null) {
    			jButton1 = new JButton();
    			jButton1.setBounds(new Rectangle(405, 195, 153, 41));
    			jButton1.setText("big");
    			populateTable(1);
    		}
    		return jButton1;
    	}
    	
    	private JButton getJButton() {
    		if (jButton == null) {
    			jButton = new JButton();
    			jButton.setBounds(new Rectangle(191, 193, 194, 43));
    			jButton.setText("small");
    			populateTable(2);
    		}
    		return jButton;
    	}
    	
    	
    	public void populateTable(int opt) {
    		ArrayList<letters> data = null;
    		
    		
    		if (opt == 1) { //print small letters for the first option
    		data = new ArrayList<letters>();
    		data.add(new letters("a"));
    		data.add(new letters("b"));
    		data.add(new letters("c"));
    		}
    		////////////////////////////////////////////////////////
    		//
    		// print ucase letters for the second option 
    		else if (opt == 2) {
    			data = new ArrayList<letters>();
    			data.add(new letters("A"));
    			data.add(new letters("B"));
    			data.add(new letters("C"));
    		}
    		DefaultTableModel model = new DefaultTableModel();
    		jTable.setModel(model);
    		model.setColumnIdentifiers(new String[] {"value"});
    		
    		for (letters l : data) {
    			model.addRow(new String[] {l.first});
    		
    		}
    		
    	}
    
    }
    
    class letters {
    	String first;
    	
    	public letters(String first) {
    		super();
    		this.first = first;
    
    	}
    	
    	public String getFirst() {
    		return first;
    	}
    
    }

  4. #4
    kulangotski is offline Senior Member
    Join Date
    Feb 2009
    Posts
    117
    Rep Power
    0

    Default

    got it working. just reinstantiated the whole class so it will be fetching the new data everytime it loads up. thanks anyway :D

Similar Threads

  1. Replies: 6
    Last Post: 08-12-2010, 01:54 PM
  2. Dynamically changing the title of the JFrame??
    By Y. Progammer in forum New To Java
    Replies: 2
    Last Post: 03-15-2010, 09:20 AM
  3. Dynamically changing the TextArea.forground !
    By Y. Progammer in forum New To Java
    Replies: 6
    Last Post: 03-01-2010, 10:08 AM
  4. Replies: 3
    Last Post: 02-06-2010, 03:28 PM
  5. Dynamically changing the display
    By abhiN in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 01-22-2008, 11:19 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
  •