2014-01-07 47 views
0

你好我在JScrollPane中显示我的JTable时出现问题。Java jTable在jScrollPane中不可见

这是我的代码:

table = new JTable(); 
    table.setBounds(16, 203, 362, 16); 
    //getContentPane().add(table); 
    table.setShowHorizontalLines(true); 
    table.setShowVerticalLines(true); 
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
    table.setModel(new DefaultTableModel(
     new Object[][] { 
      {"2", "DN Korina", "200"}, 
     }, 
     new String[] { 
      "QTY", "Item Code", "Amount" 
     } 
    )); 
    table.getColumnModel().getColumn(0).setPreferredWidth(105); 
    table.getColumnModel().getColumn(1).setPreferredWidth(244); 
    table.getColumnModel().getColumn(2).setPreferredWidth(220); 
    table.setBackground(Color.PINK); 
    JTableHeader header = table.getTableHeader(); 
    header.setBackground(Color.yellow); 
    JScrollPane scrollPane = new JScrollPane(table); 
    scrollPane.setBounds(16, 480, 359, -240); 
    getContentPane().add(scrollPane); 

我使用windowbuilder亲,我无法看到JScrollPane的设计视图。如果我删除JScrollPane并让JTable单独运行,则JTable是可见的。但我看不到标题。我读过这个来查看JTable的头文件,JTable必须放在JScrollPane中。任何想法为什么我看不到我的JScrollPane和JTable?谢谢。

+1

使用布局管理器,你的问题将消失 – Reimeus

+1

@Reimeus我使用AbsoluteLayout。这有什么问题吗? – Dunkey

+0

不要设置边界。一个布局_other_比AbsoluteLayout –

回答

1

最后,我得到它的工作!

我为JTable和JScrollPane设置了相同的边界。

table = new JTable(); 
table.setBounds(16, 203, 362, 16); 


scrollPane.setBounds(16, 203, 362, 266); 
+1

将容器的布局设置为BorderLayout,没有更多问题 – MadProgrammer

+0

@MadProgrammer对于此应用程序,我使用的是AbsoluteLayout。 – Dunkey

+1

'LayoutManager'旨在克服开发GUI应用程序时出现的问题。您无法控制字体,也无法控制这些字体在不同系统上的显示效果,其中最明显的就是这些字体。虽然绝对布局经理“看起来”很好开始,准备花费你的余生调整它... – MadProgrammer