3

This may be a silly question, and I'd rather not use the NetBeans inbuilt GUI editor, but I've been asked to.

The problem I'm having is I'm attempting to add a JPanel (itself containing a few labels) to an already existing JScrollPane that is on a JFrame. The JScrollPane and JFrame have been created using the NetBeans GUI editor.

I have an ArrayList (of undetermined size) of orders, and for each order in the ArrayList I'm attempting to create a JPanel and put it in the JScrollPane.

I'm having trouble with adding a JPanel to a JScrollPane and then adding labels to the JPanel. I've looked at tutorials online but all of them need reference to the JFrame... but because I'm using the inbuilt GUI editor that comes with NetBeans I can't reference the JFrame in the code.

The code I'm currently using:

    FlowLayout experimentLayout = new FlowLayout(FlowLayout.CENTER);
    JPanel panel = new JPanel();
    panel.setLayout(experimentLayout);
    JPanel panel2 = new JPanel();
    JLabel label2 = new JLabel("Hello");
    scrollPane.add(panel); //ScrollPane is already on form (put there by GUI editor)
    panel.add(panel2);
    panel2.add(label2);

Any help on how to add these JPanel to the JScrollPane would be very much appreciated!

4

2 回答 2

4

我想通了!我使用了 scrollPane.getViewport().add(panel); 而不仅仅是 .add :)

谢谢你的帮助 :)

于 2012-11-15T10:21:31.327 回答
4

如果没有代码,就不可能 100% 确定,但您会像自己构建 UI 那样做。

jScrollPane1.setViewportView(yourPanel);

如果您无法直接访问框架的内部,那么您需要提供某种访问方法来允许您这样做。

于 2012-11-15T00:01:27.627 回答