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

Reply

Join Date: Sep 2004
Posts: 55
Reputation: apcxpc is an unknown quantity at this point 
Solved Threads: 0
apcxpc's Avatar
apcxpc apcxpc is offline Offline
Junior Poster in Training

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

 
0
  #1
Oct 25th, 2005
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;



  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 55
Reputation: apcxpc is an unknown quantity at this point 
Solved Threads: 0
apcxpc's Avatar
apcxpc apcxpc is offline Offline
Junior Poster in Training

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

 
0
  #2
Oct 25th, 2005
kk nevermind, I figured it out.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 46
Reputation: Gargol is an unknown quantity at this point 
Solved Threads: 1
Gargol's Avatar
Gargol Gargol is offline Offline
Light Poster

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

 
0
  #3
Oct 31st, 2005
From your last post I assume that you have now implemented an AbstractTableModel.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 56
Reputation: staneja is an unknown quantity at this point 
Solved Threads: 1
staneja's Avatar
staneja staneja is offline Offline
Junior Poster in Training

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

 
0
  #4
Feb 16th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 822
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

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

 
0
  #5
Feb 16th, 2009
@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.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC