如何设置JTable的宽度

时间:2012-11-07 18:07:24

标签: java swing jtable jscrollpane

我在JScrollPane中有一个JTable。我希望最小宽度大约为600宽表。我尝试在表格,滚动窗格和面板上设置最小尺寸。大小根本没有变化,我错过了什么?很难谷歌这一点,因为所有出现的是如何设置列的宽度。

enter image description here

以下是代码:

class SearchResults extends JPanel {

/**
 * Create the panel.
 */
public SearchResults() {
    setMinimumSize(new Dimension(640, 480));
    String[][] data= new String[][] {
            {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, "VIEW BUTTON"},
            {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, "VIEW BUTTON"}};
    String[] col = new String[] {
            "Last Name", "First Name", "Middle Initial", "Phone Number", "Email", "Project Title", "Project Description", "Amount", "Date Approved", "Date Completed", "College", "Faculty Mentor Name", "Co Grantee", "Major", "Travel Required", "Travel Purpose", "Travel Cost", "Travel Start Date", "Travel End Date", "View"};

      JTable table = new JTable(data,col);
      table.setMinimumSize(new Dimension(600,200));
      JTableHeader header = table.getTableHeader();
      JScrollPane pane = new JScrollPane(table);
      pane.setMinimumSize(new Dimension(600, 23));
      table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
      add(pane);
}

}

以下是我将其添加到JFrame的地方:

public class Test extends JFrame
{

    public static void main(String[] args)
    {
        Test test = new Test();
        test.run();
    }

    public Test()
    {
        super("JAVA TEST!");
    }

    private void run()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        SearchResults resultsPanel = new SearchResults();
        resultsPanel.setMinimumSize(new Dimension(600,200));
        add(resultsPanel);
        setSize(800,600);
        setVisible(true);
    }
}

3 个答案:

答案 0 :(得分:8)

有几个问题:

  • (正如我在评论中已经提到的)内部面板的FlowLayout总是将其子项调整为各自的prefSize
  • 表的min / pref / max宽度是根据各列大小的总和计算的
  • 一个表是一个Scrollable,因此发布了它的preferredScrollableViewportSize(这是围绕JScrollPane用来计算自己的prefSize的大小)
  • prefScrollable的实现是......缺乏(温和地说),其硬编码为400 x 450(或类似)

Consequestly,有几个螺丝要调整(删除所有setXXSize调用后:))

  • 使面板使用BorderLayout:如果调整框架大小,scrollPane将填充整个区域。
  • 扩展JTable,为prefScrollableViewportSize返回合理的内容(f.i.,就可见列/行的pref数而言)

在代码中(并使用SwingX项目的JXTable,因为它已经有第二个api :-))

String[][] data= new String[][] {
        {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, "VIEW BUTTON"},
        {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, "VIEW BUTTON"}};
String[] col = new String[] {
        "Last Name", "First Name", "Middle Initial", "Phone Number", 
        "Email", "Project Title", "Project Description", "Amount", 
        "Date Approved", "Date Completed", "College", "Faculty Mentor Name", 
        "Co Grantee", "Major", "Travel Required", "Travel Purpose", 
        "Travel Cost", "Travel Start Date", "Travel End Date", "View"};

 JXTable table = new JXTable(data,col);
 table.setVisibleColumnCount(10);
 table.setHorizontalScrollEnabled(true);
 JScrollPane pane = new JScrollPane(table);
 JComponent comp = new JPanel(new BorderLayout());
 comp.add(pane);

修改

解决80%的要求(以及MigLayout的小预告:-))

// 80% with a minimum of 600 logical pixel:
MigLayout layout = new MigLayout("wrap 2, debug",
        "[600:pref, fill, grow][20%]");
JComponent comp = new JPanel(layout);
comp.add(pane, "spany");
comp.add(new JLabel("just something"));

答案 1 :(得分:0)

这个东西也有一些问题。也使用setPreferedSize(new Dimension(800,600));。 这可以解决你的问题

答案 2 :(得分:0)

布局很重要。像NetbeansEclipse这样的IDE通过拖放提供免提布局设计。

一旦理解Swing layouts,您甚至可以通过代码实现。我在这里提到的几乎没有最常用的布局

BorderLayout
BoxLayout
FlowLayout
GridLayout
在你的

中,如果添加panel.setLayout( new GridLayout(1,1));,表格将在框架内得到修复。

除非您覆盖某些内容或添加更多内容,否则您无需扩展JPanelJFrame。练习学习。