Results 1 to 7 of 7
  1. #1
    Maraxus is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default JTable. make it read what's in the box

    Hi. I ran across an annoying problem I had before and still I can't figure it out...

    I am using a JTable, where the user writes stuff into.
    Then there is a button below it, he clicks and then the calculation happens.

    However, the last cell changed is not read correctly, first I have to switch the highlighted cell, only then will the button-triggered calculations (table.getValueAt(x, y), to be precise) read the last change correctly.

    Am I just to stupid or is there no way to get the value in the cell without the user switching to another first?
    Last edited by Maraxus; 09-10-2010 at 07:45 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You may wish to create a small compilable program that tests just this and nothing else and see for yourself. If it still doesn't work, then you can post this test program here, and we can work with you on fixing it. This is called creating a short, self contained, correct (compilable), example, or SSCCE. Check out the link for more on this.

    Best of luck on solving your problem and welcome to the Java-Forum!

  3. #3
    Maraxus is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanks for the welcome.

    Okay, first of all, I'm sorry for my bad java-style. I guess for most of the stuff I do, there is a better way, but well, this is mine:
    Java Code:
    import javax.swing.*;
    import javax.swing.event.TableModelEvent;
    import javax.swing.event.TableModelListener;
    import javax.swing.table.JTableHeader;
    import javax.swing.table.TableColumn;
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.event.*;
    
    public class Start extends JPanel implements ActionListener, TableModelListener {
    	
    	private static final long serialVersionUID = 1L;
    	private JTable table1;
    	private JScrollPane scrollpane;
    	
    	private JButton button;
    	
    	private static JFrame frame = new JFrame("Testa");
    
    	public Start() {
    		super();
    
    		BorderLayout blo = new BorderLayout();
    		setLayout(blo);
    		
    		table1 = new JTable(2, 2);
    		table1.getModel().addTableModelListener(this);
    		
    		scrollpane = new JScrollPane(table1);
    		scrollpane.setPreferredSize(new Dimension(180, 130));
    		add(scrollpane, BorderLayout.NORTH);
    		
    		table1.setCellSelectionEnabled(true);
    		JTableHeader header = table1.getTableHeader();
    		header.setReorderingAllowed(false);
    		
    		TableColumn column = table1.getColumnModel().getColumn(0);
    		column.setHeaderValue("A");
    		column.setPreferredWidth(100);
    		column = table1.getColumnModel().getColumn(1);
    		column.setHeaderValue("B");
    		column.setPreferredWidth(100);
    		
    		table1.setValueAt(0, 0, 0);
    		table1.setValueAt(0, 1, 0);
    
    		button = new JButton("Calc");
    		button.addActionListener(this);
    		button.setActionCommand("calc");
    		add(button, BorderLayout.CENTER);
    	}    
    
    	public void calc(){
    		try {
    			table1.setValueAt(table1.getValueAt(0, 0), 0, 1);
    		}
    		catch (Exception e){}
    		try {
    			table1.setValueAt(table1.getValueAt(1, 0), 1, 1);
    		}
    		catch (Exception e){}
    	}
    	
    	public void actionPerformed(ActionEvent ae) {
    		Object source = ae.getSource();
    		if ( source == button ) {
    			calc();
    		}
    	}
    	
    	public void tableChanged(TableModelEvent e) {
    		;
    	}
        
    	public static void main(String[] args) {
    		String laf = UIManager.getSystemLookAndFeelClassName();
    		try {
    			UIManager.setLookAndFeel(laf);
    		}
    		catch(Exception e) {
    			e.printStackTrace();
    			UIManager.put("swing.boldMetal", Boolean.FALSE);
    		}
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.add(new Start(), BorderLayout.CENTER);
    		frame.pack();
    		frame.setLocation(200, 100);
    		frame.setVisible(true);
    	}
    }
    Now what does it roughly do:
    Create the GUI with a 2x2 table and a button.
    By pressing the button (function calc) the stuff from the left collum is copied into the right collum.

    What I exactly want it to do but it does not (by example):
    1. I click on the cell A1.
    2. I hit "5" on the keybord (the value in the cell is now "05")
    3. I click on the button
    4. The cell B1 reads "05"

    What happens:
    4. The cell B1 reads "0", just like A1 did before I added a "5"

    Of course, I can simply accept that I have to click on another cell, or press an arrow key or enter, after I have entered the 5 before I press the button but I hope there is a better way.
    Last edited by Maraxus; 09-10-2010 at 10:12 PM.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    Check out camickr's Table Stop Editing.
    Table Stop Editing Java Tips Weblog

    db

  5. #5
    Maraxus is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    I searched the whole API but could not find
    table.putClientProperty("terminateEditOnFocusLost" , Boolean.TRUE);
    Thanks that was exactly what I was looking for.

  6. #6
    madcommu is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    you have to put table.stopCellEditing() in try-catch in that buttons actionperformed method

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    Quote Originally Posted by madcommu View Post
    you have to put table.stopCellEditing() in try-catch in that buttons actionperformed method
    No you don't.

    db

Similar Threads

  1. How to make the column of Jtable editable
    By man4ish in forum AWT / Swing
    Replies: 1
    Last Post: 01-06-2010, 03:53 PM
  2. How to make a scanner read an int?
    By Dieter in forum New To Java
    Replies: 3
    Last Post: 11-08-2009, 01:11 AM
  3. How to make Scanner read the same line
    By mcollins in forum New To Java
    Replies: 2
    Last Post: 03-03-2009, 06:41 AM
  4. Replies: 5
    Last Post: 10-17-2008, 02:13 PM
  5. Can't make JTable work -- please help!!
    By cagalli83 in forum Advanced Java
    Replies: 0
    Last Post: 02-13-2008, 09:31 AM

Tags for this Thread

Posting Permissions

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