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

Thread: JTable Updating String Values from User Input

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default JTable Updating String Values from User Input

    I am having issues with my JTable saving values that the user inputs in the cells. At first glance, it would seem that the data has been saved, but when you attempt to retrieve the data or reload the table, the data goes back to its previous value.

    Basically, the user would input data, press enter or move on to a different cell or something, and the data should get saved into the data array. It seems the data is just getting saved visually, but not in any real memory.

    Can anyone give any tips?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JTable Updating String Values from User Input

    My guess is that you aren't actually saving the data (using the TableModel's function setValueAt()), but without a short example that demos the problem its just a guess.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: JTable Updating String Values from User Input

    How do I use the setValueAt() function when I cant see what data the user enters?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JTable Updating String Values from User Input

    Quote Originally Posted by aussiemcgr View Post
    How do I use the setValueAt() function when I cant see what data the user enters?
    The parameters for the setValueAt function include the row, column and an Object. In most cases where the table displays string, this object is simply a String value. In more complex cases you need to cast this object to the type of class the column contains. Either way, this object represents the user entered value, and you can use it to set the data model.

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: JTable Updating String Values from User Input

    Ok, well here is something I didnt know.

    The setValueAt function DOESNT change the value in the data array. Which means I had to manually do it myself. I did this:
        	DefaultCellEditor cellEditor = new DefaultCellEditor(new JTextField());
        	cellEditor.setClickCountToStart(1);
        	cellEditor.addCellEditorListener(new CellEditorListener(){
        		public void editingCanceled(ChangeEvent e)
        		{
        			System.out.println("Editing Canceled: "+e);
        		}
        		public void editingStopped(ChangeEvent e)
        		{
        			patterns.setValueAt(((DefaultCellEditor)e.getSource()).getCellEditorValue(),patterns.getSelectedRow(),0);
        			patternData[patterns.getSelectedRow()][0] = patterns.getValueAt(patterns.getSelectedRow(),0);
        		}
        	});
        	patterns.getColumnModel().getColumn(0).setCellEditor(cellEditor);

    And it works.

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: JTable Updating String Values from User Input

    For those curious or had trouble like I did, here is some code for formatting JTables base on what is in their cell. Might find this useful if you have never done this like I hadnt. Learned alot on this 4 day project. Any thoughts would be appreciated.

    Variables and Setup:
    //Column Names
    Object[] columnNames = new Object[]{"Range","Color 1","Color 2","Type","Single"};
    //JTable Data. For this table, the types are String,Color,Color,JComboBox,JCheckBox
    Object[][] patternData = new Object[1][5]{"E3:AS4",Color.BLUE,Color.RED,new JComboBox(new String[]{"Line","Bar"}),false};
    //Creating the JTable model
    DefaultTableModel model = new DefaultTableModel(patternData,columnNames);
    //Creating the JTable using the model
    JTable patterns = new JTable(model);

    Cell With String Value That User Can Edit
    DefaultCellEditor cellEditor = new DefaultCellEditor(new JTextField());
        cellEditor.setClickCountToStart(1);
        cellEditor.addCellEditorListener(new CellEditorListener(){
        	public void editingCanceled(ChangeEvent e)
        	{
        		System.out.println("Editing Canceled: "+e);
        	}
        	public void editingStopped(ChangeEvent e)
        	{
        		patterns.setValueAt(((DefaultCellEditor)e.getSource()).getCellEditorValue(),patterns.getSelectedRow(),0);
        		patternData[patterns.getSelectedRow()][0] = patterns.getValueAt(patterns.getSelectedRow(),0);
        	}
        });
        patterns.getColumnModel().getColumn(0).setCellEditor(cellEditor);

    Cell That Contains It's Background Color
    patterns.getColumnModel().getColumn(1).setCellRenderer(new TableCellRenderer()
            {
             	public Component getTableCellRendererComponent(
    		JTable table, Object value, boolean isSelected,boolean isFocused, int row, int col)
    		{
    			Color colour = (Color) value;
    			JButton rendererComponent = new JButton();
    			rendererComponent.setBackground(colour);
    			return rendererComponent;
    		}
    	});

    Cell That Contains JComboBox
    		patterns.getColumnModel().getColumn(3).setCellRenderer(new TableCellRenderer()
            	{
                	public Component getTableCellRendererComponent(
    					JTable table, Object value, boolean isSelected,boolean isFocused, int row, int col)
    					{
    						JComboBox rendererComponent = new JComboBox(new String[]{"Line","Bar"});
    						rendererComponent.setSelectedItem(value.toString());
    						return rendererComponent;
    					}
    			});
    		DefaultCellEditor cellEditor2 = new DefaultCellEditor(new JComboBox(new String[]{"Line","Bar"}));
    		cellEditor2.setClickCountToStart(1);
    		cellEditor2.addCellEditorListener(new CellEditorListener(){
        		public void editingCanceled(ChangeEvent e)
        		{
        			System.out.println("Editing Canceled: "+e);
        		}
        		public void editingStopped(ChangeEvent e)
        		{
        			System.out.println("Editing Stopped: "+((DefaultCellEditor)e.getSource()).getComponent());
        			patternData[patterns.getSelectedRow()][3] = patterns.getValueAt(patterns.getSelectedRow(),3);
        			System.out.println(patternData[patterns.getSelectedRow()][3]);
        		}
        	});
        	patterns.getColumnModel().getColumn(3).setCellEditor(cellEditor2);

    Cell With CheckBox
    patterns.getColumnModel().getColumn(4).setCellRenderer(new TableCellRenderer()
            	{
                	public Component getTableCellRendererComponent(
    					JTable table, Object value, boolean isSelected,boolean isFocused, int row, int col)
    					{
    						boolean marked = (Boolean) value;
    						JCheckBox rendererComponent = new JCheckBox();
    						if(marked)
    						{
    							rendererComponent.setSelected(true);
    						}
    						return rendererComponent;
    					}
    			});
    		DefaultCellEditor cellEditor3 = new DefaultCellEditor(new JCheckBox());
    		cellEditor3.setClickCountToStart(1);
    		cellEditor3.addCellEditorListener(new CellEditorListener(){
        		public void editingCanceled(ChangeEvent e)
        		{
        			System.out.println("Editing Canceled: "+e);
        		}
        		public void editingStopped(ChangeEvent e)
        		{
        			System.out.println("Editing Stopped: "+((JCheckBox)((DefaultCellEditor)e.getSource()).getComponent()).isSelected());
        			patternData[patterns.getSelectedRow()][4] = ((JCheckBox)((DefaultCellEditor)e.getSource()).getComponent()).isSelected();
        		}
        	});
        	patterns.getColumnModel().getColumn(4).setCellEditor(cellEditor3);

Similar Threads

  1. Replies: 1
    Last Post: November 2nd, 2012, 02:21 PM
  2. Problem with updating empty JTable
    By byubi in forum AWT / Java Swing
    Replies: 1
    Last Post: May 15th, 2010, 02:06 AM
  3. how do I keep a persistent prompt in JTextArea and allow user input
    By cazmo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 23rd, 2010, 01:14 PM
  4. Values of Input
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 8th, 2009, 03:46 AM
  5. User Input Loop
    By cfmonster in forum Loops & Control Statements
    Replies: 7
    Last Post: August 24th, 2009, 01:52 PM