TheCoder24
TheCoder24

Reputation: 47

How to go onto the next line on a FlowLayout

I am making a simple GUI with the logo being on the first line, then the rest of the stuff on the next line. Problem is the logo is to small therefore the JComboBox and JTextArea is also on that line, how could I prevent this and ONLY make the logo on the first line? Thank you!

    public class TimerMenu {

    private JFrame frame;
    private JLabel background, logo;
    private JTextArea timeText;
    private JButton startTimerButton;
    private JComboBox timeUnitChoice;

    public TimerMenu(){
        frame = new JFrame("Timer");
        startTimerButton = new JButton("Start Timer");
        startTimerButton.setPreferredSize(new Dimension(135, 30));
        startTimerButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // TODO: CHANGE TO SOMETHING NICER
                JOptionPane.showMessageDialog(null, "This feature hasn't been implemented yet.", "We're sorry!",
                        JOptionPane.ERROR_MESSAGE);
            }
        });
        // Creating drop down menu.
        String[] timeChoices = { "Nanoseconds", "Microseconds", "Milliseconds", "Seconds", "Minutes", "Hours", "Days"};
        // Giving the choices from the array of 'timeChoices'
        timeUnitChoice = new JComboBox(timeChoices);
        // Setting the default option to 'Minutes' (4th choice, starting at 0 as its an array!)
        timeUnitChoice.setSelectedIndex(4);
        try {
            background = new JLabel(new ImageIcon(ImageIO.read(getClass()
                    .getResourceAsStream("/me/devy/alarm/clock/resources/background.jpg"))));
            logo = new JLabel(new ImageIcon(ImageIO.read(getClass()
                    .getResourceAsStream("/me/devy/alarm/clock/resources/timer.png"))));
        } catch (IOException e) {
            e.printStackTrace();
        }
        // Creating simple text
        background.setLayout(new FlowLayout());
        frame.setContentPane(background);
        frame.add(logo);
        frame.add(timeUnitChoice);
        // Creating a text field
        timeText = new JTextArea("Length:");
        timeText.setEditable(false);
        frame.add(timeText);
        frame.add(startTimerButton);
        frame.setVisible(true);
        frame.setSize(550, 250);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

Upvotes: 2

Views: 19841

Answers (2)

Ishmael Mavor Raines
Ishmael Mavor Raines

Reputation: 320

JPanel panel3 = new JPanel(false);    
panel3.setLayout(new FlowLayout(FlowLayout.LEFT));  //using flow layout
JLabel empty_line = new JLabel("");   // <--- empty label to effect next row
empty_line.setPreferredSize(new Dimension(3000,0));
//i used 3000 for x-axis, should over flow most screens but  should not displace 
//the next components under it
panel3.add(component1); 
panel3.add(empty_line); //add empty label
panel3.add(component2); //components 1 and component2 must be declared before

Upvotes: 0

user5271886
user5271886

Reputation:

What you want is to replace this

background.setLayout(new FlowLayout());
frame.add(logo);
frame.add(timeUnitChoice);
frame.add(timeText);
frame.add(startTimerButton);

by this (kind of)

background.setLayout(new FlowLayout(FlowLayout.LEFT));

JPanel logoPnl = new JPanel(new FlowLayout(FlowLayout.LEFT));
logoPnl.add(logo);
JPanel fnctnPnl = new JPanel(new FlowLayout());
fnctnPnl.add(timeUnitChoice);
fnctnPnl.add(timeText);
fnctnPnl.add(startTimerButton);

JPanel borderPnl = new JPanel(new BorderLayout());
borderPnl.add(logoPnl, BorderLayout.NORTH);
borderPnl.add(fnctnPnl, BorderLayout.SOUTH);

JPanel container = new JPanel(new FlowLayout(FlowLayout.LEFT));
container.add(borderPnl);

frame.getContentPane().add(container);

You will generally need to stack different layouts for you to be able to arrange components in a meaningful way.

See also: swing flow layout break element

Upvotes: 2

Related Questions