How to prevent resizing columns in a JTable

Java 8Object Oriented ProgrammingProgramming

To prevent resizing columns, use the method setResizingAllowed(). Here, we will set setResizingAllowed() to false for table header to disallow resizing of columns from header −

table.getTableHeader().setResizingAllowed(false);

Let us first see an example wherein we can easily resize columns in a table by resizing the table column header −

Example

package my;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class SwingDemo {
   public static void main(String[] argv) throws Exception {
      DefaultTableModel tableModel = new DefaultTableModel();
      JTable table = new JTable(tableModel);
      tableModel.addColumn("Language/ Technology");
      tableModel.addColumn("Text Tutorial");
      tableModel.addColumn("Video Tutorial");
      tableModel.addColumn("Views");
      tableModel.addRow(new Object[] { "F#", "Yes", "No", "7890"});
      tableModel.addRow(new Object[] { "Blockchain", "Yes", "No", "10600"});
      tableModel.addRow(new Object[] { "SharePoint", "Yes", "Yes", "4900"});
      tableModel.addRow(new Object[] { "AWS", "No", "Yes", "8900"});
      tableModel.addRow(new Object[] { "Python", "Yes", "No", "6789"});
      tableModel.addRow(new Object[] { "Scala", "Yes", "No", "3400"});
      tableModel.addRow(new Object[] { "Swift", "No", "Yes", "9676"});
      tableModel.addRow(new Object[] { "C#", "Yes", "Yes", "1300"});
      tableModel.addRow(new Object[] { "NodeJS", "No", "Yes", "2350"});
      tableModel.addRow(new Object[] { "MVC", "Yes", "No", "1500"});
      tableModel.addRow(new Object[] { "ASP.NET", "Yes", "Yes", "3400"});
      tableModel.addRow(new Object[] { "Java", "Yes", "No", "9686"});
      tableModel.addRow(new Object[] { "jQuery", "Yes", "Yes", "4500"});
      table.setBackground(Color.white);
      table.setForeground(Color.black);
      Font font = new Font("Verdana", Font.PLAIN, 12);
      table.setFont(font);
      JFrame frame = new JFrame();
      frame.setSize(600, 400);
      frame.add(new JScrollPane(table));
      frame.setVisible(true);
   }
}

The output is as follows displaying that we successfully resized a column “Text Tutorial” to give it a different size than the other columns. We resized the column header −

Output

The following is an example wherein we are preventing users to resize columns. We will set FALSE to setResizingAllowed() method −

Example

package my;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class SwingDemo {
   public static void main(String[] argv) throws Exception {
      DefaultTableModel tableModel = new DefaultTableModel();
      JTable table = new JTable(tableModel);
      tableModel.addColumn("Language/ Technology");
      tableModel.addColumn("Text Tutorial");
      tableModel.addColumn("Video Tutorial");
      tableModel.addColumn("Views");
      table.getTableHeader().setResizingAllowed(false);
      tableModel.addRow(new Object[] { "F#", "Yes", "No", "7890"});
      tableModel.addRow(new Object[] { "Blockchain", "Yes", "No", "10600"});
      tableModel.addRow(new Object[] { "SharePoint", "Yes", "Yes", "4900"});
      tableModel.addRow(new Object[] { "AWS", "No", "Yes", "8900"});
      tableModel.addRow(new Object[] { "Python", "Yes", "No", "6789"});
      tableModel.addRow(new Object[] { "Scala", "Yes", "No", "3400"});
      tableModel.addRow(new Object[] { "Swift", "No", "Yes", "9676"});
      tableModel.addRow(new Object[] { "C#", "Yes", "Yes", "1300"});
      tableModel.addRow(new Object[] { "NodeJS", "No", "Yes", "2350"});
      tableModel.addRow(new Object[] { "MVC", "Yes", "No", "1500"});
      tableModel.addRow(new Object[] { "ASP.NET", "Yes", "Yes", "3400"});
      tableModel.addRow(new Object[] { "Java", "Yes", "No", "9686"});
      tableModel.addRow(new Object[] { "jQuery", "Yes", "Yes", "4500"});
      table.setBackground(Color.white);
      table.setForeground(Color.black);
      Font font = new Font("Verdana", Font.PLAIN, 12);
      table.setFont(font);
      JFrame frame = new JFrame();
      frame.setSize(600, 400);
      frame.add(new JScrollPane(table));
      frame.setVisible(true);
   }
}

The output is as follows displaying the default size for all the columns and we cannot resize them since we have set the table resize to FALSE −

OutPut

raja
Published on 31-May-2019 14:24:40
Advertisements