bijiDango
bijiDango

Reputation: 1596

Weighty doesn't work in GridBagLayout

enter image description here

Above is what I want to achieve:

  1. on the east of the JFrame is a fixed 400 width JPanel.
  2. the panel uses GridBagLayout.
  3. components in this panel all stretches horizontally
  4. the last component fills the extra y space
  5. the last component is a JscrollPane wrapping a JTable.

I have had many trials, all failed:

  1. I tried to setPreferredSize of the panel, then the inner components won't stretch horizontally. (I know how use a GridBagConstraints, or I think I know)
  2. I tried to setSize of the panel, then size is actually determined by the JscrollPane.
  3. I tried to warp the JscrollPane in another JPanel (because I do not trust JScrollPane that much), no use.
  4. The GridBagContraints.weighty never functions in this project, however functions well elsewhere.

So I think the problem is with the JTable, it always make a prox. 300*300 rectangular as shown within the red line. enter image description here

Today is another bad day with swing's setPreferredSize and setSize and all these size problems. Hope anyone could help, thanks!

    private JPanel buildInfoPanel() {
        JPanel panel = new JPanel();
        panel.setVisible(false);

        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.gridx = 0;
        panel.setLayout(gbl);

        label_player = new JLabel("label_player");
        label_player.setBackground(Color.RED);
        constraints.gridy = 0;
        constraints.anchor = GridBagConstraints.NORTH;
        gbl.setConstraints(label_player, constraints);
        panel.add(label_player);

        label_deck = new JLabel("label_deck");
        label_deck.setBackground(Color.BLUE);
        constraints.gridy++;
        gbl.setConstraints(label_deck, constraints);
        panel.add(label_deck);

        JTable table = new JTable(new ShipTableModel());
        table.setAutoCreateRowSorter(true);
        table.setFillsViewportHeight(true);
        table.getRowSorter().toggleSortOrder(2);

        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setBorder(new LineBorder(Color.red));
        constraints.gridy++;
        constraints.weighty = 1;
        gbl.setConstraints(scrollPane, constraints);
        panel.add(scrollPane);
        return panel;
    }

    class ShipTableModel extends AbstractTableModel {

        private Ship[] ships = new Ship[0];
        private String[] columnNames = {"id", "name", "lv", "hp", "舰队", "舰种", "mId"};

        @Override
        public String getColumnName(int col) {
            return columnNames[col];
        }

        @Override
        public int getRowCount() { 
            return ships.length; 
        }

        @Override
        public int getColumnCount() { 
            return columnNames.length; 
        }
    }

Upvotes: 0

Views: 297

Answers (1)

nitegazer2003
nitegazer2003

Reputation: 1193

Just set constraints.fill = GridBagConstraints.VERTICAL for the JScrollPane.

Upvotes: 0

Related Questions