943,852 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 7043
  • Java RSS
Oct 25th, 2005
0

Problem changing contents of JTable when button is pressed...

Expand Post »
Hi all

I am programming a database-related application in Java. I have to facilitate several different types of queries, but I'd like to use the same JTable to display the results of each query (one at a time, of course).

The problem is that when I'm using one JTable, I can't change the contents of it. I'm stuck with the data that I used to initialize the JTable.

I could not find any methods to update the contents of the JTable. I tried creating a new JTable object and adding that to the panel, then tried repainting/updating the panel & container, but nothing worked.

So I created this very stripped down version of the problem. Currently I am initializing the JTable with column names columnNames1 and rows data1.
When the button is clicked, I want to change the JTable to have column names columnNames2 and rows data2;



Quote ...
Java Syntax (Toggle Plain Text)
  1. public class TestClass extends JFrame{
  2.  
  3. JPanel panel;
  4. JTable table;
  5. JScrollPane scrollPane;
  6. Container C;
  7. JButton testButton;
  8. ActionListener changeListener;
  9.  
  10. String[] columnNames1 = {"First Name","Last Name","Age"};
  11. String[] columnNames2 = {"First Name","Last Name","Height"};
  12.  
  13. Object[][] data1 = {
  14. {"Mary", "Campione", "Snowboarding", "5"},
  15. {"Alison", "Huml", "Rowing", "3"},
  16. {"Kathy", "Walrath", "Knitting", "2"},
  17. {"Sharon", "Zakhour", "Speed reading", "20"},
  18. {"Philip", "Milne", "Pool", "10"}};
  19.  
  20. Object[][] data2 = {
  21. {"James", "Keenan", "Hiking", "65"},
  22. {"Josh", "Huml", "Running", "37"},
  23. {"Billy", "Walrath", "Swimming", "21"},
  24. {"Robert", "Zakhour", "Golfing", "88"},
  25. {"Mike", "Milne", "Soccer", "1"}};
  26.  
  27. public TestClass(){
  28. testButton = new JButton("Change Data");
  29. changeListener = new ChangeListener();
  30. testButton.addActionListener(changeListener);
  31.  
  32. panel = new JPanel();
  33. Container C = getContentPane();
  34. C = getContentPane();
  35. C.add(panel);
  36.  
  37. table = new JTable(data1, columnNames1);
  38. scrollPane = new JScrollPane(table);
  39. table.setPreferredScrollableViewportSize(new Dimension(500, 70));
  40. panel.add(scrollPane);
  41. panel.add(testButton);
  42. }
  43.  
  44. class ChangeListener implements ActionListener{
  45. public void actionPerformed(ActionEvent evt){
  46. System.out.println("Change Button pressed");
  47. //change contents of JTable
  48. }
  49. }
  50.  
  51. public static void main(String[] args){
  52. Frame f = new TestClass();
  53. f.setSize(600,600);
  54. f.setVisible(true);
  55. f.setTitle("University Database Application");
  56. }
  57. }

Any help would be much appreciated.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
apcxpc is offline Offline
55 posts
since Sep 2004
Oct 25th, 2005
0

Re: Problem changing contents of JTable when button is pressed...

kk nevermind, I figured it out.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
apcxpc is offline Offline
55 posts
since Sep 2004
Oct 31st, 2005
0

Re: Problem changing contents of JTable when button is pressed...

From your last post I assume that you have now implemented an AbstractTableModel.
Reputation Points: 10
Solved Threads: 1
Light Poster
Gargol is offline Offline
46 posts
since Oct 2005
Feb 16th, 2009
0

Re: Problem changing contents of JTable when button is pressed...

Can you please tell me how you solved this issue. Its occurring for me also.

I am running a query , sometime it results in 8 rows and sometime it gives 3 rows.

My first quesry resulted in 8 rows and i am getting 8 rows now but my 2nd query resultd 3 nows , Still i ma able to see 8 rows..

Help me out
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
staneja is offline Offline
63 posts
since Dec 2006
Feb 16th, 2009
0

Re: Problem changing contents of JTable when button is pressed...

@staneja : Atleast care to see the post date, before posting question to him. It's better to start a new thread than to revive a four year old thread.
Last edited by verruckt24; Feb 16th, 2009 at 2:52 am.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: method calls with scanner in classes using classes
Next Thread in Java Forum Timeline: Update Jtable content issue





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC