- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Disallow resizing a column by dragging between headers in a JTable Component with Java
Let us first create a table with rows and columns −
String data[][] = { {"Australia","5","1"}, {"US","10","2"}, {"Canada","9","3"}, {"India","7","4"}, {"Poland","2","5"}, {"SriLanka","5","6"} }; String col [] = {"Team","Selected Players","Rank"}; DefaultTableModel tableModel = new DefaultTableModel(data,col); JTable table = new JTable(tableModel);
Now, we will disable resizing of columns by dragging headers −
table.getTableHeader().setResizingAllowed(false);
The following is an example to disallow resizing a column by dragging between headers in a JTable: −
Example
package my; import java.awt.Dimension; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JRootPane; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class SwingDemo { public static void main(String[] argv) throws Exception { JFrame frame = new JFrame("Demo"); JPanel panel = new JPanel(); String data[][] = { {"Australia","5","1"}, {"US","10","2"}, {"Canada","9","3"}, {"India","7","4"}, {"Poland","2","5"}, {"SriLanka","5","6"} }; String col [] = {"Team","Selected Players","Rank"}; DefaultTableModel tableModel = new DefaultTableModel(data,col); JTable table = new JTable(tableModel); table.getTableHeader().setResizingAllowed(false); Dimension dim = new Dimension(50,2); table.setIntercellSpacing(new Dimension(dim)); JScrollPane scrollPane = new JScrollPane(table); panel.add(scrollPane); frame.add(panel); frame.setSize(600,400); frame.setUndecorated(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getRootPane().setWindowDecorationStyle(JRootPane.COLOR_CHOOSER_DIALOG); frame.setVisible(true); } }
The output is as follows. You won’t be able to resize the columns by dragging between table header −
Output
- Related Articles
- How to disallow resizing component with GridBagLayout in Java
- How to disable auto resizing for a JTable in Java?
- How to prevent resizing columns in a JTable
- Disable auto resizing to make the JTable horizontal scrollable in Java
- How to select the first column in a JTable with Java?
- Java Program to select a column in JTable?
- Java Program to enable column selection in a JTable
- How can we sort a JTable on a particular column in Java?
- How to change each column width of a JTable in Java?
- How to extract the column headers in a table in Selenium with python?
- Python - How to rename multiple column headers in a Pandas DataFrame with Dictionary?
- How to set magins between cells of a JTable in Java?
- What are the Selection Modes in a JTable with Java?
- How to enable row selection in a JTable with Java
- Python – Center align column headers of a Pandas DataFrame

Advertisements