简体   繁体   English

将JScrollPane添加到JTable中不会显示

[英]Adding JScrollPane to JTable doesn't show up

I'm pretty new to Javas swings, so sorry if this is something trivial. 我对Javas波动还很陌生,如果这很琐碎,请对不起。 This is the constructor, I excluded unnecessary form items. 这是构造函数,我排除了不必要的表单项。 (I tried running the code as short as this, but the problem still appears) (我尝试运行这样短的代码,但问题仍然出现)

//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);

Populating the table and displaying it isn't the problem, there's enough information inside the table 'table' that the user even needs to scroll down. 填充表并显示它不是问题,表“表”内部有足够的信息,用户甚至需要向下滚动。

But that's where the problem begins, the scroll doesn't show up. 但这是问题开始的地方,滚动条没有出现。 How do I implement it from here. 我如何从这里实现它。 I tried following solutions I found on google, but they pretty much sum up on: 我尝试了以下在Google上找到的解决方案,但它们的总结大致如下:

JScrollPane scroll = new JScrollPane(table);

Which simply doesn't work in my case. 在我看来,这根本行不通。

The problem may be trivial, if it is, I'm sorry, I'm still learning swing. 这个问题可能很简单,如果是这样,对不起,我还在学习挥杆动作。 Also, sorry for my bad english, it's not my native language. 另外,对不起我的英语不好,这不是我的母语。 :) :)

Also, if there's something I forgot to include, please let me know. 另外,如果我忘了包含一些内容,请告诉我。

Thank you! 谢谢!

You add your table to 2 components : 您将table添加到两个组件中:

JScrollPane skrol = new JScrollPane(table);

and

layeredPane.add(table);

Because of Swing component can have just one parent component second statment override first, so your JScrollPane is empty. 由于Swing组件只能具有一个父组件,因此第二个语句首先要被覆盖,因此您的JScrollPane为空。 Seems you need to remove layeredPane.add(table); 似乎您需要删除layeredPane.add(table);

As mentioned here 正如这里提到的

Each GUI component can be contained only once. 每个GUI组件只能包含一次。 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. 如果某个组件已经在容器中,并且您尝试将其添加到另一个容器中,则该组件将从第一个容器中删除,然后添加到第二个容器中。

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

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