This is a tutorial in which we will going to create a program that has the JCheckBox Component using Java. The
JTable is used to display tables of data and allows the user to edit the data. This is mainly used to view data from the database or
So, now let's start this tutorial!
1. Open JCreator or NetBeans and make a java program with a file name of
jTableComponent.java.
2. Import the javax.swing package to access the JFrame, JScrollPane, the JTable class.
3. Initialize your variable in your Main, variable
frame for creating JFrame, variable
table for JTable, and variable
scrollPane for JScrollPane. We will also create an object array for the rows and columns that will be put this data to the JTable.
Object rows
[][] = { { "0001",
"Lyndon",
"Bermoy" },
{ "0001", "Novee Jane", "Dumanig" } };
Object columns
[] = { "Student ID",
"First name",
"Last name" };
As you have seen the code above, I created an object rows that is a multidimensional array specifically a two-dimensional one. The object columns has three columns on it as is a one-dimensional array only. The syntax for the JTable is
JTable(row,column). The JTable above is placed inside of a JScrollPane. By default, a JTable will adjust its width such that a horizontal scrollbar is unnecessary.
4. Now, add the JScrollPane variable instead of the table variable of JTable for JTable to the frame using the default BorderLayout of center position of the getContentPane method because the JTable is inside of the JScrollPane component. Have this code below:
frame.getContentPane().add(scrollPane, "Center");
5. Lastly, set the size, visibility, and the close operation of the frame.
frame.setSize(300, 150);
frame.setVisible(true);
frame.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE);
Output:
Here's the full code of this tutorial:
import javax.swing.*;
public class jTableComponent {
public static void main
(String args
[]) {
Object rows
[][] = { { "0001",
"Lyndon",
"Bermoy" },
{ "0001", "Novee Jane", "Dumanig" } };
Object columns
[] = { "Student ID",
"First name",
"Last name" };
frame.getContentPane().add(scrollPane, "Center");
frame.setSize(300, 150);
frame.setVisible(true);
frame.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE);
}
}
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.
Best Regards,
Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
Mobile: 09488225971
Landline: 826-9296
E-mail:
[email protected]
Add and Follow me on Facebook:
https://www.facebook.com/donzzsky
Visit and like my page on Facebook at:
https://www.facebook.com/BermzISware