tags:

views:

49

answers:

2

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();
    }

}
+1  A: 

Not sure but the Wrap Layout might work for you.

camickr
It did actually :-) thanks
Martin S.
A: 

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).

Geoffrey Zheng
but as I said above I don't to wrap only JLabels I also want to wrap JTextField like JLabel with few letters then JTextField as missing letter and continue the end of the word with JLabel again that's why I wrapped it into JPanel with FlowLayout which contains for example JLabel + JTextField + JLabel + JTextField + JLabel
Martin S.
May I ask what your application actually does? Why do you need to use individual components for each word?
Geoffrey Zheng
Well, I see where you're getting. And I'm most likely going to use a different approach although I didn't excpect it to be so difficult.
Martin S.
The application loads some sentences and splits them into pieces so that letters y,i are separated from the rest of the sentence and replace each y and i letter with a small JTextField
Martin S.
I guess I'm asking too much, but again why do you need to use a full `JTextField` for one letter? Can't you use `JTextPane`, or `JEditorPane` if you want to style the letters?
Geoffrey Zheng
I need JTextField for the user to input the letter into it but if JTextPane or JEditorPane can handle it I can use one of them thought I am not familiar with them so far.
Martin S.
So is your app some kind of language learning thing that teach people to make out confusing letters? I don't think you can limit input on only part of the string in JTextPane or JEditorPane, so you can consider using an editable list (e.g. http://www.jroller.com/santhosh/date/20050607) with the wrapping I mentioned.
Geoffrey Zheng