Results 1 to 7 of 7
- 09-10-2010, 07:43 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 3
- Rep Power
- 0
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.
-
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!
- 09-10-2010, 10:10 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 3
- Rep Power
- 0
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:
Now what does it roughly do: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); } }
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.
- 09-10-2010, 10:19 PM #4
Check out camickr's Table Stop Editing.
Table Stop Editing « Java Tips Weblog
db
- 09-11-2010, 01:05 AM #5
Member
- Join Date
- Sep 2010
- Posts
- 3
- Rep Power
- 0
I searched the whole API but could not find
table.putClientProperty("terminateEditOnFocusLost" , Boolean.TRUE);
Thanks that was exactly what I was looking for.
- 09-18-2010, 10:17 AM #6
Member
- Join Date
- Sep 2010
- Posts
- 5
- Rep Power
- 0
you have to put table.stopCellEditing() in try-catch in that buttons actionperformed method
- 09-18-2010, 01:41 PM #7
Similar Threads
-
How to make the column of Jtable editable
By man4ish in forum AWT / SwingReplies: 1Last Post: 01-06-2010, 03:53 PM -
How to make a scanner read an int?
By Dieter in forum New To JavaReplies: 3Last Post: 11-08-2009, 01:11 AM -
How to make Scanner read the same line
By mcollins in forum New To JavaReplies: 2Last Post: 03-03-2009, 06:41 AM -
java.io.IOException: Unable to read entire block; 493 bytes read before EOF; expected
By kushagra in forum New To JavaReplies: 5Last Post: 10-17-2008, 02:13 PM -
Can't make JTable work -- please help!!
By cagalli83 in forum Advanced JavaReplies: 0Last Post: 02-13-2008, 09:31 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks