Davis8988
Davis8988

Reputation: 418

Horizontal scroller bar doesn't show in jtable. How to make the columns stop shrinking?

I am adding a JScrollPane to my JTable along with HORIZONTAL_SCROLLBAR_AS_NEEDED and VERTICAL_SCROLLBAR_AS_NEEDED.
And also setting JTable.AUTO_RESIZE_OFF. But when I add bunch of columns the names of the columns don't show properly, they shrink to fit the table. And the horiztal scroller doesn't show.

here is the code and a picture of how it looks:

tableForDay1 = new JTable();
tableForDay1.setBounds(0, 619, 1337, -619);
tableForDay1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
tableForDay1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

JScrollPane scrollBar1 = new JScrollPane (tableForDay1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollBar1.setVisible(true);
day1.setLayout(new BoxLayout(day1, BoxLayout.X_AXIS));
createColumns(tableForDay1);

here are the columns I add: (I pass the table as a parameter)

private void createColumns(JTable table)
{
    //get table model
    DefaultTableModel tableModel = new DefaultTableModel();
    table.setModel(tableModel);
    tableModel.addColumn("אזור");
    tableModel.addColumn("מוביל");
    tableModel.addColumn("ת.ה");
    tableModel.addColumn("מ.ה");
    tableModel.addColumn("קוד מוצר");
    tableModel.addColumn("יישוב");
    tableModel.addColumn("שעת תאום");
    tableModel.addColumn("תוספות למוצר");
    tableModel.addColumn("כתובת");
    tableModel.addColumn("קומה");
    tableModel.addColumn("דירה");
    tableModel.addColumn("שם הלקוח");
    tableModel.addColumn("תאור מוצר");
    tableModel.addColumn("פלאפון");
    tableModel.addColumn("אישר קשר נוסף");
    tableModel.addColumn("פירוט מחיר");
    table.getTableHeader().setFont(new Font("Ariel", Font.BOLD, 27));

    String[]row = { "something", "something", "something", "something" , 
            "something" , "something" , "something" , "something", "something", 
            "something", "something" , "something" , "something" , "something" };

    tableModel.addRow(row);
}

here is the picture: Names of the columns don't show properly , and columns not alligned

Names of the columns don't show properly , and columns not alligned

how can I fix this?

Upvotes: 1

Views: 33

Answers (1)

Till Hemmerich
Till Hemmerich

Reputation: 168

getColumnModel().getColumn(0).setPreferredWidth(*insert your width here*);

You will have to do this for every Column.

Upvotes: 2

Related Questions