- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
How to change each column width of a JTable in Java?
JTable
- A JTable is a subclass of JComponent for displaying complex data structures.
- A JTable can follow the Model View Controller (MVC) design pattern for displaying the data in rows and columns.
- The DefaultTableModel class can extend AbstractTableModel and it can be used to add the rows and columns to a JTable dynamically.
- The DefaultTableCellRenderer class can extend JLabel class and it can be used to add images, colored text and etc. inside the JTable cell.
- A JTable can generate TableModelListener, TableColumnModelListener, ListSelectionListener, CellEditorListener, RowSorterListener interfaces.
- By default the width of a JTable is fixed, we can also change the width of each column by using table.getColumnModel().getColumn().setPreferredWidth() method of JTable class.
Example
import java.awt.*; import javax.swing.*; import javax.swing.table.*; public class JTableTest extends JFrame { private JTable table; private JScrollPane scrollPane; private DefaultTableModel model; private DefaultTableCellRenderer cellRenderer; public JTableTest() { setTitle("JTable Test"); setLayout(new FlowLayout()); scrollPane = new JScrollPane(); JTable table = new JTable(); scrollPane.setViewportView(table); model = (DefaultTableModel)table.getModel(); model.addColumn("S.No"); model.addColumn("First Name"); model.addColumn("Last Name"); model.addColumn("Email"); model.addColumn("Contact"); for(int i = 0;i < 4; i++) { model.addRow(new Object[0]); model.setValueAt(i+1, i, 0); model.setValueAt("Tutorials", i, 1); model.setValueAt("Point", i, 2); model.setValueAt("@tutorialspoint.com", i, 3); model.setValueAt("123456789", i, 4); } // set the column width for each column table.getColumnModel().getColumn(0).setPreferredWidth(5); table.getColumnModel().getColumn(3).setPreferredWidth(100); cellRenderer = new DefaultTableCellRenderer(); cellRenderer.setHorizontalAlignment(JLabel.CENTER); table.getColumnModel().getColumn(0).setCellRenderer(cellRenderer); add(scrollPane); setSize(475, 250); setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new JTableTest(); } }
Output
- Related Articles
- How to apply adjustments to the next column of a JTable only, when the width of a column is changed in Java Swing?
- How to apply adjustments to the last column of a JTable only, when the width of any column is changed in Java Swing?
- Java Program to select a column in JTable?
- How to select the first column in a JTable with Java?
- How to change JTable's header font in Java
- Java Program to enable column selection in a JTable
- Java Program to change the background color of rows in a JTable
- How to change ttk.Treeview column width and weight in Python 3.3?
- How can we sort a JTable on a particular column in Java?
- Change column-width property with CSS Animations
- Define the width of each column in CSS Grid Layout
- How to Copy a Column Width in Excel?
- How to set a tooltip to each column of a JTableHeader in Java?
- How can we set the background/foreground color for individual column of a JTable in Java?
- Change column-rule-width property with CSS Animations

Advertisements