| | |
Problem changing contents of JTable when button is pressed...
![]() |
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;
Any help would be much appreciated.
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;
•
•
•
•
Java Syntax (Toggle Plain Text)
public class TestClass extends JFrame{ JPanel panel; JTable table; JScrollPane scrollPane; Container C; JButton testButton; ActionListener changeListener; String[] columnNames1 = {"First Name","Last Name","Age"}; String[] columnNames2 = {"First Name","Last Name","Height"}; Object[][] data1 = { {"Mary", "Campione", "Snowboarding", "5"}, {"Alison", "Huml", "Rowing", "3"}, {"Kathy", "Walrath", "Knitting", "2"}, {"Sharon", "Zakhour", "Speed reading", "20"}, {"Philip", "Milne", "Pool", "10"}}; Object[][] data2 = { {"James", "Keenan", "Hiking", "65"}, {"Josh", "Huml", "Running", "37"}, {"Billy", "Walrath", "Swimming", "21"}, {"Robert", "Zakhour", "Golfing", "88"}, {"Mike", "Milne", "Soccer", "1"}}; public TestClass(){ testButton = new JButton("Change Data"); changeListener = new ChangeListener(); testButton.addActionListener(changeListener); panel = new JPanel(); Container C = getContentPane(); C = getContentPane(); C.add(panel); table = new JTable(data1, columnNames1); scrollPane = new JScrollPane(table); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); panel.add(scrollPane); panel.add(testButton); } class ChangeListener implements ActionListener{ public void actionPerformed(ActionEvent evt){ System.out.println("Change Button pressed"); //change contents of JTable } } public static void main(String[] args){ Frame f = new TestClass(); f.setSize(600,600); f.setVisible(true); f.setTitle("University Database Application"); } }
Any help would be much appreciated.
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
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
@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 !!!
![]() |
Similar Threads
- URGENT :: Odd Windows Booting Problem (Windows NT / 2000 / XP)
- Laptop Opening / Hinge (Monitors, Displays and Video Cards)
- Recognizing when a button is pressed. (Java)
- JTables (Java)
- Ginormous Problem With XP (Windows NT / 2000 / XP)
- Windows Does Not Start - New Mobo (Motherboards, CPUs and RAM)
Other Threads in the Java Forum
- Previous Thread: method calls with scanner in classes using classes
- Next Thread: Update Jtable content issue
| Thread Tools | Search this Thread |
account android api applet application array arrays automation bidirectional binary birt bluetooth class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield jtree julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source oracle plazmic print problem program project property recursion ria scanner search server set sharepoint smart sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree unlimited webservices windows





