1

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

}
TheCoder24
  • 37
  • 1
  • 1
  • 9
  • Still waiting for you to start "accepting" answers when you get help. All you do is click the check mark beside the answer. – camickr Nov 26 '15 at 16:26
  • I'm sorry, I thought thats what the check mark did, I just went back and "accepted" all the answers that worked for me, after I try this one out I'll do the same with this one! – TheCoder24 Nov 26 '15 at 19:00
  • Try to use WrapFlowLayout, like in this answer https://stackoverflow.com/questions/4699892/how-to-set-the-component-size-with-gridlayout-is-there-a-better-way/66964783#66964783 – Adir Dayan Apr 06 '21 at 07:59

2 Answers2

2

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

Community
  • 1
  • 1
TheBass
  • 64
  • 1
  • 3
  • Thank you, this has worked! But now I dont have a background for the GUI anymore, how could I fix this? I followed the 'swing flow layout break element' that you linked me. – TheCoder24 Nov 26 '15 at 19:21
  • I think your problem was already solved by MadProgrammer in your new [question](http://stackoverflow.com/questions/33948361/swing-adding-more-lines-in-the-gui-and-background-not-showing). If i use your code from there the background shows just fine if I use `frame.setResizable(true);`, it's just hidden behind the buttons. – TheBass Nov 27 '15 at 07:00
0
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