
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we prevent the re-ordering columns of a JTable in Java?
JTable component in Swing provides an effective method for showing and editing tabular information. JTable automatically supports re-ordering columns through dragging of column headers by default. In this article, we will learn to prevent the re-ordering of columns of a JTable in Java.
JTable
A JTable is a subclass of the JComponent class, and it can be used to create a table with information displayed in multiple rows and columns.
Syntax
The following is the syntax for JTable initialization:
JTable table = new JTable(data, columnNames);
When a value is selected from a JTable, a TableModelEvent is generated, which is handled by implementing a TableModelListener interface.
Preventing the Reordering of Columns
By default, we can re-order columns in a JTable. We cannot allow the user to reorder columns by using the table.getTableHeader().setReorderingAllowed ()method and setting the value to false.
The following are the steps to prevent the reordering of columns of a JTable in Java:
Class Declaration & Imports
The javax.swing.* provides Swing components (JScrollPane, JFrame, etc.) while the java.awt.* provides AWT classes (size, layout managers), and the class extends JFrame to create a window.
import java.awt.*; import javax.swing.*; public final class JTableColumnReorderingTest extends JFrame {
Instance Variable & Constructor Setup
The table variable of the JTable component shows the tabular data, the scrollPane is the JScrollPane that contains the table to provide scrolling. The constructor constructs the JFrame window with the header "JTableColumnReordering Test".
JTable table; JScrollPane scrollPane; public JTableColumnReorderingTest() { setTitle("JTableColumnReordering Test");
Table Data Setup
Defined columnNames as a string array for column headers, and created a 2D array containing the table's rows and columns.
String[] columnNames = {"Name", "Mobile Number", "Course"}; Object[][] data = {{"Raja", "123456789", "Java"},{"Adithya", "456123789", ".Net"},{"Vineet", "789456123", "Java Script"},{"Archana", "987456321", "Python"},{"Krishna", "321456987", "Scala"},{"Jai", "456321789", "ServiceNow"}};
Disabling Column Reordering & Resizing
The setReorderingAllowed(false) stops users from dragging columns to re-order them, and the setResizingAllowed(false) prevents users from adjusting column widths. Also adds the scrollable table to the JFrame.
table.getTableHeader().setReorderingAllowed(false); table.getTableHeader().setResizingAllowed(false); add(scrollPane);
Main Method
The main method launches the application by creating an object of JTableColumnReorderingTest.
public static void main(String[] args) { new JTableColumnReorderingTest(); }
Example
Below is an example to prevent the reordering of columns of a JTable in Java:
import java.awt.*; import javax.swing.*; public final class JTableColumnReorderingTest extends JFrame { JTable table; JScrollPane scrollPane; public JTableColumnReorderingTest() { setTitle("JTableColumnReordering Test"); String[] columnNames = {"Name", "Mobile Number", "Course"}; Object[][] data = {{"Raja", "123456789", "Java"}, {"Adithya", "456123789", ".Net"}, {"Vineet, "789456123", "Java Script"}, {"Archana", "987456321", "Python"}, {"Krishna", "321456987, "Scala"}, {"Jai", "456321789", "ServiceNow"}}; table = new JTable(data, columnNames); scrollPane= new JScrollPane(table); table.getTableHeader().setReorderingAllowed(false); // not allow re-ordering of columns table.getTableHeader().setResizingAllowed(false); add(scrollPane); setSize(375, 250); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new JTableColumnReorderingTest(); } }