0

I have an JPanel that uses by default a FlowLayout manager. I like the advantage of the document style FlowLayout in which I add components one at a time with automatic wrapping but would like a component to force selection of a new line.

I read if I used a BoxLayout I could insert a sort of component return key and force the components to start on a new line. I need guidance regarding my decision and which is a better approach.

I have a JLabel and JTextField on one line and would like to place a JTextArea wrapped inside a JScrollPane below.

Mushy
  • 2,535
  • 10
  • 33
  • 54

1 Answers1

2
  • Use a combination of FlowLayout and BorderLayout. It's a good idea to nest layouts to get your desired result.
  • The JLabel and the JTextField would go in one JPanel with FlowLayout
  • Then another JPanel with BorderLayout will hold the previous panel at the NORTH position, and the JTextArea with JScrollPane at the CENTER position.

    JPanel topPanel = new JPanel();
    JLabel label = new JLabel("Text Field Label");
    JTextField jtf = new JTextField(20);
    topPanel.add(label);
    topPanel.add(jtf);
    
    JPanel bothPanel = new JPanel(new BorderLayout());
    JTextArea jta = new JTextArea(20, 40);
    bothPanel.add(topPanel, BorderLayout.NORTH);
    bothPanel.add(new JScrollPane(jta));
    

Have a look at Laying Out Components Within a Container

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class FlowBorderDemo {

    public FlowBorderDemo() {
        JPanel topPanel = new JPanel();
        JLabel label = new JLabel("Text Field Label");
        label.setForeground(Color.white);
        JTextField jtf = new JTextField(20);
        topPanel.add(label);
        topPanel.add(jtf);
        topPanel.setBackground(Color.black);


        JPanel bothPanel = new JPanel(new BorderLayout());
        JTextArea jta = new JTextArea(20, 40);
        JScrollPane scrollPane = new JScrollPane(jta);
        scrollPane.setBorder(BorderFactory.createMatteBorder(3, 0, 0, 0, Color.GRAY));
        bothPanel.add(topPanel, BorderLayout.NORTH);
        bothPanel.add(scrollPane);
        bothPanel.setBorder(BorderFactory.createMatteBorder(3, 8, 3, 8, Color.GRAY));

        JLabel copyLabel = new JLabel("<html>&copy;2014 peeskillet</html>");
        copyLabel.setBackground(Color.LIGHT_GRAY);
        copyLabel.setHorizontalAlignment(JLabel.CENTER);
        bothPanel.add(copyLabel, BorderLayout.PAGE_END);


        JFrame frame = new JFrame();
        frame.add(bothPanel);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager
                            .getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException
                        | IllegalAccessException
                        | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                new FlowBorderDemo();
            }
        });
    }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Thank you @peeskillet. I never considered just multiplying the number of panels and playing with the layout managers. However, how would I force wrap a component within the same layout manager? – Mushy Jan 11 '14 at 12:55
  • @Mushy what exactly are you trying to _wrap_? – Paul Samsotha Jan 11 '14 at 13:02
  • I want to remain in a `FlowLayout` and wrap the `JScrollPane` beneath the `JLabel` and the `JTextField` without playing with extra panels and layout managers. – Mushy Jan 11 '14 at 13:08
  • @Mushy you can nest an many panels and layouts as you want to get your desired result. Wrapping may not give you your desired result all the time. For example if you wanted some components below the panel with the text field, you could create another panel with a `BoxLayout` and add the text field panel and another panel to the `BoxLayout` then put that `BoxLayout` panel at the `NORTH` position in the code above. Just keep nesting panels to get your desired result. – Paul Samsotha Jan 11 '14 at 13:08
  • I see and thank you. [This is saying the same thing you are so thank you](http://stackoverflow.com/questions/6238309/swing-flow-layout-break-element") – Mushy Jan 11 '14 at 13:10
  • @Mushy that won't give you a desired result at all. if the user widens the screen then the text area will appear to the right of the label and text field. And if you make the screen unresizable, that defeats the purpose of what you're attempting. – Paul Samsotha Jan 11 '14 at 13:11