This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes regarding JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "regarding JTable" Watch "regarding JTable" New topic
Author

regarding JTable

ravic johnson
Greenhorn

Joined: Oct 25, 2008
Posts: 9
how to Populate a JTable with MySQL Data after clicking JButton

datails:
data is present in mysql database in tabular form and i want to fetch this data from the database in the form of JTable after clicking JButton
:
please help
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37950
    
  22
You get a ResultSet which has rows, each row with several columns in. You can get the details from the ResultSetMetaData object associated with the ResultSet, eg how many rows, name of column, etc.
You can get the contents of the ResultSet with its next() and getXXX() methods, put them into a List, or an Object[][], and populate the JTable.
Or you can extend DefaultTableModel.
Go through the Java Tutorials and see whether that helps.

And welcome to JavaRanch
ravic johnson
Greenhorn

Joined: Oct 25, 2008
Posts: 9
i am new to java
i am preparing a project called supply chain system
i took help on it through tutorials on the internet
but i could not exactly understand what you have written
can you please explain it with the help of example
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18


1) You could also use other methods, like getString for only string representations. Ideally you should use meta.getColumnType(i) for that. For instance, you can have the following mapping:

Check the rest of java.sql.Types for what it could return, and be creative in the mapping.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
ravic johnson
Greenhorn

Joined: Oct 25, 2008
Posts: 9
i have tried it but it is not resulting that i wants
here is the coding of java file which is for opening contents present in mysql database in the form of JTable after clicking JButton
/*
*/
please help

[Edit]Sorry, but even though it is a private URL I think it's still wiser to remove the password. Rob[/Edit]
[ October 26, 2008: Message edited by: Rob Prime ]
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Originally posted by ravic johnson:
i have tried it but it is not resulting that i wants


You will probably get better help if you go into greater detail here. What is this code doing that is wrong? What is it not doing that it should be doing? etc...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Originally posted by ravic johnson:
JPanel panel = new JPanel();
panel.add(new JScrollPane(table),BorderLayout.CENTER);
panel.setForeground(java.awt.Color.lightGray);

This is probably not the main problem, but it is still a problem.

JPanel has a FlowLayout by default, so your code will not put in the center of your panel and resize it when your user interface resizes. You need to explicitly give the JPanel a new BorderLayout() for that to work.
ravic johnson
Greenhorn

Joined: Oct 25, 2008
Posts: 9
according to your advice i went into greater details that is trying to understand the defaulttablemodel concept
i worked on the coding part
made some changes in the coding part
but again i could not to achieve what i want
i am putting that coding part here
please help
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

What is it doing what you don't want, or not doing what you want?

I've tried your code (just changed the database details of course) and it worked just fine.
ravic johnson
Greenhorn

Joined: Oct 25, 2008
Posts: 9
i want to say to populate a jTable with mysql data after clicking a JButton
this coding i have given will run quit good without clicking a JButton
that is to run :java student
but i want to populate a JTable with mysql database data after clicking a Jbutton
here JButton implements a actionlistener on clicking a new interface should open
that i want to say
please help
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Well, you know how to fill a DefaultTableModel with your data know. So all you need to do is put that part of the code in your action listener, and when it's done call table.setModel(tableModel).

You will notice that your program seems to be hanging while your table model is being filled. For the reason and a solution check JProgressBat Doesn't Update. In short, all the filling code should be put in a different thread, and when it's done the "table.setmodel(tableModel)" call should be on the Event Dispatcher Thread.

An easy way is using javax.swing.SwingWorker:

You could have the loading seem more dynamic using publish and process but let's keep it simple for now.
ravic johnson
Greenhorn

Joined: Oct 25, 2008
Posts: 9
yes i am to able to know how to fill the defaulttablemodel with data
but i have tried putting that part of coding into java file that implements actionlistener but it yields errors
i am putting code here unchanged of that part includes JButton that after clicking i expects must proceed to show JTable with mysql data



can you explain it how to implement it with this coding
please help
[ October 29, 2008: Message edited by: ravic johnson ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Originally posted by ravic johnson:
but it yields errors

What errors?
ravic johnson
Greenhorn

Joined: Oct 25, 2008
Posts: 9
in class Supplier which coding i previously posted that implements actionlistener if i put that portion how to fill defaulttablemodel with data with coding changed is as follows


when compiled it results following errors
C:\jdk\bin>javac Supplier.java
Supplier.java:99: <identifier> expected
tableModel.setRowCount(0);
^
Supplier.java:100: <identifier> expected
tableModel.setColumnCount(0);
^
Supplier.java:107: illegal start of type
for(int i=1; i<=columnCount; i++)
^
Supplier.java:112: <identifier> expected
Vector data = new Vector();
^
Supplier.java:116: illegal start of type
while(result.next())
^
Supplier.java:151: <identifier> expected
}


please help
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Just before "TableModel model = new TableModel(data,columns);" you close your method, so the rest is just dangling inside the class definition. And inside the class definition you cannot have statements.
ravic johnson
Greenhorn

Joined: Oct 25, 2008
Posts: 9
i have put the lines of code inside the method that is under initComponents method but it even it results error
/*
C:\jdk\bin>javac Supplier.java
Supplier.java:95: cannot find symbol
symbol : class ResultSetMetaData
location: class Supplier
ResultSetMetaData md = result.getMetaData();
^
Supplier.java:95: cannot find symbol
symbol : variable result
location: class Supplier
ResultSetMetaData md = result.getMetaData();
^
Supplier.java:98: javax.swing.table.TableModel is abstract; cannot be instantiat
ed
TableModel model = new TableModel(data,columns);
^
Supplier.java:103: cannot find symbol
symbol : class ResultSetMetaData
location: class Supplier
ResultSetMetaData md = result.getMetaData();
^
Supplier.java:103: cannot find symbol
symbol : variable result
location: class Supplier
ResultSetMetaData md = result.getMetaData();
^
Supplier.java:104: columnCount is already defined in initComponents()
int columnCount = md.getColumnCount();
^
Supplier.java:106: columns is already defined in initComponents()
Vector columns = new Vector(columnCount);
^
Supplier.java:114: data is already defined in initComponents()
Vector data = new Vector();
^
Supplier.java:115: row is already defined in initComponents()
Vector row;
^
Supplier.java:118: cannot find symbol
symbol : variable result
location: class Supplier
while(result.next())
^
Supplier.java:121: row is already defined in initComponents()
Object[] row = new Object[columns];
^
Supplier.java:121: incompatible types
found : java.util.Vector
required: int
Object[] row = new Object[columns];
^
Supplier.java:123: cannot find symbol
symbol : variable result
location: class Supplier
{row[i - 1] = result.getObject(i);
^
13 errors
*/
coding of supplier is as follows

i again want to say that i want to populate a JTable with mysql data after clicking JButton
can you please explain what you have advised
Well, you know how to fill a DefaultTableModel with your data know. So all you need to do is put that part of the code in your action listener, and when it's done call table.setModel(tableModel).

i am quite new to swing and AWT concepts

please help i want to submit this project in the first week of November
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Those errors are quite basic. You should start by importing the right classes. Also, you should create a new DefaultTableModel. TableModel itself is an interface.
ravic johnson
Greenhorn

Joined: Oct 25, 2008
Posts: 9
i want to ask what is the concept behind putting defaulttablemodel with data in the part (supplier java file) which implements action listener
please explain
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regarding JTable
 
Similar Threads
JTable
JTable(!!!)
Color in JTable
JTable date edit problem
Issue with JTable Cell