java - 将 JScrollPane 添加到 JTable 不显示

标签 java swing jtable jscrollpane

我对 Javas swing 还很陌生,如果这是一件微不足道的事情,我很抱歉。这是构造函数,我排除了不必要的表单项。 (我尝试运行这么短的代码,但问题仍然出现)

//This just opens a connection to MySQL server, this doesn't create any problems.
bp = BazaPodataka.getBaza();

//Forming the main frame..
JFrame frame = new JFrame();
{
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setBounds(d.width/2 - sirina/2, d.height/2 - visina/2, sirina, visina);
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout(0, 0));

//Adding a layered pane so I can place items inside the form more 'freely'
JLayeredPane layeredPane = new JLayeredPane();
frame.getContentPane().add(layeredPane, BorderLayout.CENTER);

    //Adding a table
JTable table = new JTable();
String[] rowData = {"Name:", "Price:", "Cathegory:", "Sum:"};
DefaultTableModel model = new DefaultTableModel(rowData, 0);
JScrollPane skrol = new JScrollPane(table);
table.setModel(model);
//The 2 lines below work as intended
ResultSet rs = (ResultSet) bp.query("SELECT * FROM table"); //This calls a query        
popuniTabelu(rs, model); //This populates the table.
table.setBounds(10, 110, 500, 350);
table.setEnabled(false);
table.setShowHorizontalLines(false);
layeredPane.add(table);

填充表格并显示它不是问题,表格“table”内有足够的信息,用户甚至需要向下滚动。

但这就是问题开始的地方,滚动条没有显示。我如何从这里实现它。我尝试了在谷歌上找到的以下解决方案,但它们几乎总结为:

JScrollPane scroll = new JScrollPane(table);

这对我来说根本不起作用。

这个问题可能是微不足道的,如果是的话,对不起,我还在学习swing。另外,抱歉我的英语不好,这不是我的母语。 :)

此外,如果我忘记包含某些内容,请告诉我。

谢谢!

最佳答案

您将添加到2个组件:

JScrollPane skrol = new JScrollPane(table);

layeredPane.add(table);

由于 Swing 组件只能有一个父组件第二条语句首先覆盖,因此您的 JScrollPane 为空。似乎您需要删除 layeredPane.add(table);

如上所述here

Each GUI component can be contained only once. If a component is already in a container and you try to add it to another container, the component will be removed from the first container and then added to the second.

关于java - 将 JScrollPane 添加到 JTable 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30255525/

相关文章:

java - 任务队列Java API

java - Swing 中的计时器任务

java - 将数组打印到 JTextArea 时出现问题

java - 我是否正确构建了这个关键监听器?

java - jtable编辑时将 ""带入数据库

java - 突发 "failed to lazily initialize a collection of role...:no session or session was closed"- 异常

java - Kafka GroupTable 测试使用 ProcessorTopologyTestDriver 时生成额外消息

java - jTable paint() 导致 java.lang.OutOfMemoryError : Java heap space

java - 当滑动到不同的 viewpager 页面时,如何使以前使用 layout_scrollFlags 隐藏的工具栏再次可见

java - 如何在jTable中的某个单元格上获取jComboBox?