简体   繁体   English

桌上的Java Swing滚动条我看过很多不同的指南,似乎没有用

[英]Java Swing Scroll Bars on table I've looked at a lot of different guides doesn't seem to be working

I've been writing a constructor. 我一直在写一个构造函数。 To create a table. 创建一个表。 However I want it to have scroll bars on the table. 但是我希望它在桌子上有滚动条。 I've been doing a lot of searching on how to do it correctly. 我一直在做很多关于如何正确做的搜索。 However it doesn't seem to be working. 但是,它似乎不起作用。 I am unsure as to where I am going wrong. 我不确定我要去哪里。

public ApplicationPanel(String[][] data, String[] colNames)
{   
   JTable table = new JTable(data, colNames);

   table.setPreferredScrollableViewportSize(new Dimension(450, 65));
   table.setFillsViewportHeight(true);
   TableColumn column;

   JScrollPane scrollPane = new JScrollPane(table);

   scrollPane.setPreferredSize(new Dimension(900,800));

 //  column.setResizable(true);
   for (int i = 0; i<4; i++)
   {
       column = table.getColumnModel().getColumn(i);
       if (i == 2)
       {
           column.setPreferredWidth(400);     
       }
       else
       {
           column.setPreferredWidth(50);
       }
   }
   this.add(scrollPane);
}

Filled the area as @alex2410 suggested with the following code added to the constructor. 按照@ alex2410的建议填充该区域,并将以下代码添加到构造函数中。

 this.setLayout(new GridLayout());

After which table scroll bars are now viewable. 在此之后,表格滚动条现在可见。 Combine this with the code above (minus the code below and you should have a working table constructor with scroll bars. 将其与上面的代码 (减去下面的代码)结合使用 ,您应该有一个带滚动条的工作表构造函数。

The following code was removed and at some point was in the code. 以下代码已删除,并且在代码中的某个位置。 So for reference these do not appear to have any effect of which I can tell. 因此,仅供参考,这些似乎没有任何作用。

   table.setPreferredScrollableViewportSize(new Dimension(450, 65));
   table.setFillsViewportHeight(true);
   table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
   scrollPane = new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_S‌​CROLLBAR_ALWAYS);
   ApplicationFrame.getFrames().getContentPane().add( new JScrollPane( table ), BorderLayout.CENTER );
   column.setResizable(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM