Martin
Martin

Reputation: 3615

Trying to wrap JLabels to JPanel inside JScrollPane

EDIT: As in the example (now written by me) what I'm trying to achieve is packing JLabel (+JTextFields, not in the example) into JPanel with FlowLayout and sorting these panels with BoxLayout one under another but limitting it with JScrollPane so I can specify how high the view area is and if those JPanels (packed JLabels) exceed the height the user has to scroll but only vertically.

public class Example2 extends JFrame {

    JScrollPane scrollPane = new JScrollPane();
    JPanel viewPanel = new JPanel();

    public Example2() {
        setSize(400,300);
        buildGUI();
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void buildGUI() {
        // SCROLLPANE PLACEMENT
        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(scrollPane, GroupLayout.PREFERRED_SIZE, 350, GroupLayout.PREFERRED_SIZE)
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(scrollPane, GroupLayout.PREFERRED_SIZE, 223, GroupLayout.PREFERRED_SIZE)
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        // REST
        scrollPane.setViewportView(viewPanel);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        viewPanel.setLayout(new BoxLayout(viewPanel, BoxLayout.Y_AXIS));


        for(int i=0; i<3; i++) {
            JPanel panel = new JPanel();
            panel.setLayout(new FlowLayout(FlowLayout.LEADING));
            panel.setBackground(new Color(200,i*100,100*i));
            for(int j=0;j<20;j++) {
                JLabel label = new JLabel("label "+j);
                panel.add(label);
            }
            viewPanel.add(panel);
        }

    }

    public static void main(String[] args) {
        new Example2();
    }

}

Upvotes: 0

Views: 3010

Answers (2)

Geoffrey Zheng
Geoffrey Zheng

Reputation: 6640

Instead of using a wrapping layout manager, a cheasy alternative that may work in a very limited context is to use JList with a wrapping orientation:

JList list = new JList(new String[]{"lllllllllllllll", "abc", "ajdfkalfjlsjfjsakljflkajsd"});
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
list.setVisibleRowCount(-1); // see setLayoutOrientation javadoc for why
JScrollPane listScroller = new JScrollPane(list);
listScroller.setPreferredSize(new Dimension(250, 20));
getContentPane().add(listScroller);

Default cell renderer for JList is JLabel, and you can style the list such that it doesn't look like a list (no border, no background).

Upvotes: 0

camickr
camickr

Reputation: 324098

Not sure but the Wrap Layout might work for you.

Upvotes: 2

Related Questions