将 JScrollPane 添加到 JTable 不显示
Adding JScrollPane to JTable doesn't show up
我对 Javas swings 很陌生,很抱歉,如果这是微不足道的事情。这是构造函数,我排除了不必要的表单项。 (试过运行这么短的代码,还是出现问题)
//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 并显示它不是问题,table 'table' 中有足够的信息,用户甚至需要向下滚动。
但这就是问题开始的地方,卷轴没有出现。我如何从这里实施它。我尝试了以下在 google 上找到的解决方案,但它们几乎总结为:
JScrollPane scroll = new JScrollPane(table);
这对我来说根本行不通。
这个问题可能是微不足道的,如果是的话,对不起,我还在学习swing。另外,抱歉我的英语不好,这不是我的母语。 :)
此外,如果有什么我忘记包括的,请告诉我。
谢谢!
您将 table
添加到 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.
我对 Javas swings 很陌生,很抱歉,如果这是微不足道的事情。这是构造函数,我排除了不必要的表单项。 (试过运行这么短的代码,还是出现问题)
//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 并显示它不是问题,table 'table' 中有足够的信息,用户甚至需要向下滚动。
但这就是问题开始的地方,卷轴没有出现。我如何从这里实施它。我尝试了以下在 google 上找到的解决方案,但它们几乎总结为:
JScrollPane scroll = new JScrollPane(table);
这对我来说根本行不通。
这个问题可能是微不足道的,如果是的话,对不起,我还在学习swing。另外,抱歉我的英语不好,这不是我的母语。 :)
此外,如果有什么我忘记包括的,请告诉我。
谢谢!
您将 table
添加到 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.