调整miglayou中的表格

时间:2014-11-24 16:00:25

标签: java swing layout layout-manager miglayout

我创建了一个MigLayout,看起来像这样:

enter image description here

正如您所看到的,表格没有正确调整大小。

我正在创建我的布局:

public JScrollPane createLayout() {
    JPanel panel = new JPanel(new MigLayout("debug 400"));
    JScrollPane sp;

    JLabel lab = new JLabel(labelValue);
    lab.setFont(new Font("Tahoma", Font.BOLD, 15));

    panel.add(lab, "wrap");
    panel.add(resultsTable(), "growx, wrap");
    panel.add(resultsButtons(), "wrap");

    //set table properties
    tableProperties(resultTable);
    updateResultsTable();

    sp = new JScrollPane(panel);
    sp.repaint();
    sp.validate();

    return sp;
}

我的表是这样创建的:

private JPanel resultsTable() {
    JPanel panel = new JPanel(new MigLayout(""));
    JScrollPane scrollTablePane;
    rtm = new ResultTableModel(resultList);
    resultTable = new JTable(rtm);
    scrollTablePane = new JScrollPane(resultTable);
    sorter = new TableRowSorter<TableModel>(resultTable.getModel());
    resultTable.setRowSorter(sorter);

    scrollTablePane.repaint();
    scrollTablePane.validate();

    //add to panel
    panel.add(scrollTablePane);
    panel.repaint();
    panel.validate();

    return panel;
}

此外,我使用以下方法设置表属性:

public void tableProperties(JTable table) {
    table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
}

为什么我的桌子栏不伸展的任何建议?

感谢您的回答!

1 个答案:

答案 0 :(得分:1)

如果您希望自动调整表的大小,请尝试在createLayout方法内使用以下构造函数JPanel panel = new JPanel(new MigLayout("debug 400,wrap 1","[grow,fill]","[grow,fill]"))resultsTable}方法中的以下构造函数JPanel panel = new JPanel(new MigLayout("","[grow,fill]","[grow,fill]"))