Edelweiss Monville
Edelweiss Monville

Reputation: 1

How to set the width of a JTable columns

I read all the posted messages concerned with setting the column width of a JTable. I literally copied the posted answers just changing the JTable name but kept getting error messages for each statement that tries to set the current column width.

I simply define a JTable with 7 fixed columns whose names are passed to the DefaultTableModel. I must be missing something but... what?

I am posting just the few relevant lines. Thank you in advance for any suggestion.

       import javax.swing.table.TableColumn;
       import javax.swing.table.TableColumnModel;


    JString[] columnNames = {"New Line Name", "Point1", "Point2", "Angle", "Parallel Line Name",      
                                 "Distance From Line", "Group"};
        DefaultTableModel linesTableModel = new DefaultTableModel(null,columnNames);
        JTable tblLines = new JTable(linesTableModel);
    TableColumnModel colModel = tblLines.getColumnModel(); 
    colModel.getColumn(0).setPreferredWidth(100);
    colModel.getColumn(1).setPreferredWidth(50);
    colModel.getColumn(2).setPreferredWidth(80);
    colModel.getColumn(3).setPreferredWidth(150);
    colModel.getColumn(4).setPreferredWidth(200);
    colModel.getColumn(5).setPreferredWidth(100);
    colModel.getColumn(6).setPreferredWidth(90);

Upvotes: -1

Views: 53

Answers (1)

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51565

Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section.

I went to the trouble of creating the rest of the code. Here's the GUI.

Example

Other than the columns not wide enough for the column headers, I don't see a problem.

Here's the complete runnable code, which is what you should be including with your question.

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;

public class JTableExample implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new JTableExample());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("JTable Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        frame.add(createTablePanel(), BorderLayout.CENTER);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JScrollPane createTablePanel() {
        String[] columnNames = { "New Line Name", "Point1", "Point2", "Angle",
                "Parallel Line Name", "Distance From Line", "Group" };
        DefaultTableModel linesTableModel = new DefaultTableModel(null,
                columnNames);
        JTable tblLines = new JTable(linesTableModel);
        TableColumnModel colModel = tblLines.getColumnModel();
        colModel.getColumn(0).setPreferredWidth(100);
        colModel.getColumn(1).setPreferredWidth(50);
        colModel.getColumn(2).setPreferredWidth(80);
        colModel.getColumn(3).setPreferredWidth(150);
        colModel.getColumn(4).setPreferredWidth(200);
        colModel.getColumn(5).setPreferredWidth(100);
        colModel.getColumn(6).setPreferredWidth(90);
        
        return new JScrollPane(tblLines);
    }

}

Upvotes: 0

Related Questions