Grow table larger than growx

2019-01-27 06:22发布

问题:

I have configured my layout like that:

    panel.add(addButtons(), "wrap");    
    panel.add(showTable(), "growx, wrap");

So at first I am adding a button group and then I would like to grow my table as large as it can and then wrap the next component in the next "line".

However, my gui looks like that:

Here you clearly cannot see any values from the table. Therefore, how to grow the table so that each value can be seen?

I appreciate your answer!

回答1:

Here you clearly cannot see any values from the table. Therefore, how to grow the table so that each value can be seen?

As I've said in my comment, I don't think your problem is about columns (preferred | min | max) sizes but the default behavior of your layout manager: MigLayout. As stated in this answer by default rows in MigLayout doesn't fill all available width but just the necessary to display the longest row (based on components width). You can see this fact if you enable "debug" feature when you instantiate your layout:

MigLayout layout = new MigLayout("debug");

As I understand your question you need a combination of both growx and fillx constraint. The first one is a component constraint and the other one is a layout constraint.

That being said, pelase consider the following progression.

1. Adding scroll pane without "growx" constraint

Snippet

MigLayout layout = new MigLayout("debug");
JPanel panel = new JPanel(layout);
panel.add(buttonsPanel, "wrap");
panel.add(scrollPane);

Screenshot

2. Adding scroll pane with "growx" constraint

Snippet

MigLayout layout = new MigLayout("debug");
JPanel panel = new JPanel(layout);
panel.add(buttonsPanel, "wrap");
panel.add(scrollPane, "growx"); // Note "growx" here

Screenshot

3. Adding scroll pane with "growx" and "fillx" contraints

Snippet

MigLayout layout = new MigLayout("debug, fillx"); // Note "fillx" here
JPanel panel = new JPanel(layout);
panel.add(buttonsPanel, "wrap");
panel.add(scrollPane, "growx"); // Note "growx" here

Screenshot



回答2:

You can use javax.swing.table.TableColumn;

TableColumn custom_column = yourTable.getColumnModel().getColumn(1); // 1 means column 1 
custom_column.setPreferredWidth(500); 

As you want to widen all columns you can use loop :

         TableColumn custom_column ;
         int numberOfColumns  = yourTable.getColumnModel().getColumnCount();

         for(int y= 0;y<numberOfColumns;y++){
            custom_column=table.getColumnModel().getColumn(y);
            custom_column.setMinWidth(500);
         }