JTable Component in Java

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.
  1. import javax.swing.*;
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.
  1. JFrame frame = new JFrame("JTable Component");
  2.  
  3.  
  4. Object rows[][] = { { "0001", "Lyndon", "Bermoy" },
  5. { "0001", "Novee Jane", "Dumanig" } };
  6. Object columns[] = { "Student ID", "First name", "Last name" };
  7.  
  8. JTable table = new JTable(rows, columns);
  9. JScrollPane scrollPane = new JScrollPane(table);
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:
  1. frame.getContentPane().add(scrollPane, "Center");
5. Lastly, set the size, visibility, and the close operation of the frame.
  1. frame.setSize(300, 150);
  2. frame.setVisible(true);
  3. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Output: output Here's the full code of this tutorial:
  1. import javax.swing.*;
  2.  
  3.  
  4. public class jTableComponent {
  5. public static void main(String args[]) {
  6. JFrame frame = new JFrame("JTable Component");
  7.  
  8.  
  9. Object rows[][] = { { "0001", "Lyndon", "Bermoy" },
  10. { "0001", "Novee Jane", "Dumanig" } };
  11. Object columns[] = { "Student ID", "First name", "Last name" };
  12.  
  13. JTable table = new JTable(rows, columns);
  14. JScrollPane scrollPane = new JScrollPane(table);
  15.  
  16.  
  17. frame.getContentPane().add(scrollPane, "Center");
  18.  
  19. frame.setSize(300, 150);
  20. frame.setVisible(true);
  21. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22.  
  23. }
  24. }
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

Add new comment