Reputation:
I just wanna know how to use jtable on netbeans because I'm scruffing along and I could'nt do anything good. please, help me
Upvotes: 0
Views: 1776
Reputation:
You can rather search for tutorials on google, especially on YouTube. It all depends on what you would like to do with the table.
If you want to connect your Jtable to the database such as MySQL then here is the coding. I have added comments so that you can easily understand it.
public final void loaddbtable() {
DefaultTableModel model = (DefaultTableModel) t_View.getModel();
//declared sql below used for database selection of specific information
String sql = "Select * from tableName";
try
{
Class.forName("com.mysql.jdbc.Driver");
//connection for database
Connection conn = (Connection)
//root and username and password for access to the database
DriverManager.getConnection("jdbc:mysql://localhost:3306/DatabaseNameAsOnMySQL","root","password");
//create the statement that will be used
Statement stmt=conn.createStatement();
//executes the statement
ResultSet rs = stmt.executeQuery(sql);
//table to view data
t_View.setModel(DbUtils.resultSetToTableModel(rs));
}
catch (Exception e)
{
//exception handled for connection to database problem or data retrieval problem
JOptionPane.showMessageDialog(null, "Error message if can't load", "Datatbase connection error", JOptionPane.ERROR_MESSAGE);
}
}
Remember that you will need to have imports such as
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import net.proteanit.sql.DbUtils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
You can place the method in your constructor if want to load immediately when the interface is run or using a button.
BUT for more information and anything else, feel free to reply and I will help you out.
you can also use this guys channel as he is very good and he explains exactly how to do it.
Hope this helps you out.
Upvotes: 1
Reputation: 3103
The following will help you, it is a Netbeans Project.
http://download.oracle.com/javase/tutorial/uiswing/examples/zipfiles/components-SimpleTableDemoProject.zip
Upvotes: 0