我对 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/