1

I'm building a simple beginner app in Java and I need your help with aligning components. What I'm trying to do is to align component(JLabel "name") to the left side of the panel. I've already tried with "new FlowLayout(FlowLayout.LEFT)" but it didn't work so I'm asking you to help me. Here is the picture of the frame and source code below it.

public class firstClass extends JFrame implements ActionListener {


private JFrame frame1;
private JFrame frame2;
private JPanel mainPanelFirst;
private JPanel secondPanel;
private JButton newWindowButton;
private int mulitplyPanels;
private JLabel leftLabel;
private JLabel rightLabel;
private JComboBox leftCB;
private JComboBox rightCB;

First Window:

https://i.stack.imgur.com/DhXXM.png

public JFrame createMainUI(){

   frame1 = new JFrame("Main frame");
   frame1.setSize(600,600);
   frame1.setResizable(false);
   frame1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
   frame1.setVisible(true);

   mainPanelFirst = new JPanel();
   mainPanelFirst.setLayout(new FlowLayout());
   frame1.add(mainPanelFirst);


   newWindowButton = new JButton("Open new window");
   newWindowButton.addActionListener(this);
   mainPanelFirst.add(newWindowButton);


   return frame1;

}

Second Window(include the label I want to align):

https://i.stack.imgur.com/VRIFr.png

 public JFrame createSecondUI() {

    frame2 = new JFrame("Second frame");
    frame2.setSize(600, 600);
    frame2.setResizable(false);
    frame2.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame2.setVisible(true);

    secondPanel = new JPanel();
    secondPanel.setLayout(new FlowLayout());
    secondPanel.setBackground(Color.gray);
    frame2.add(secondPanel);


    JPanel topPanel = new JPanel();
    topPanel.setLayout(new FlowLayout(70,400,20));
    topPanel.setBackground(Color.WHITE);
    secondPanel.add(topPanel);



    leftLabel = new JLabel("Name:");
    topPanel.add(leftLabel);



    return frame2;


}


 @Override
    public void actionPerformed(ActionEvent e) {

        createSecondUI();

    }
}

Thank you for your help :)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jan
  • 55
  • 1
  • 6
  • 1
    There are a number of layout managers available for you to use, I'd highly recommend having a look at [A Visual Guide to Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) and [Laying Out Components Within a Container](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) for more details. Also remember, you are not limited to a single layout manager, you can use a series of compounded containers all using different layout managers to achieve more complex layouts – MadProgrammer May 13 '17 at 22:19
  • 1) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. – Andrew Thompson May 13 '17 at 22:46
  • `I've already tried with "new FlowLayout(FlowLayout.LEFT)" but it didn't work` - It should work, but you don't show in your code what you tried. – camickr May 13 '17 at 22:47
  • If `new FlowLayout(FlowLayout.LEFT)` is going to work at all---and I'm not saying it will---it would need to be applied at all levels of the containment hierarchy. That is, on the `JFrame` itself, as well as `secondPanel` and `topPanel'. – Kevin Anderson May 13 '17 at 22:57
  • Yes, you need it on the secondPanel and the topPanel, but not the frame. – camickr May 13 '17 at 23:21

2 Answers2

0

Warning read this as well: Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?

Since that the JFrame is non-resizable, give the topPanel a defined size:

JPanel topPanel = new JPanel();
topPanel.setPreferredSize(new Dimension(600,100));
topPanel.setLayout(new FlowLayout(FlowLayout.LEFT,400,20));
Community
  • 1
  • 1
Max K
  • 656
  • 1
  • 6
  • 22
  • Won't work; `leftLabel` is being added to a panel with a `FlowLayout` which entirely ignores layout constraints. – Kevin Anderson May 13 '17 at 22:27
  • @KevinAnderson you're right. I've tested the new answer. – Max K May 13 '17 at 22:50
  • Thank you so much! I've changed hgap to 50 and vgap to 20 and that set the Label to the left side. – Jan May 13 '17 at 23:04
  • `topPanel.setPreferredSize(new Dimension(600,100));` See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson May 13 '17 at 23:11
  • @AndrewThompson http://stackoverflow.com/questions/1783793/java-difference-between-the-setpreferredsize-and-setsize-methods-in-compone I'm aware of avoiding the use of those three, however, I'm simply answering the question stated in the simplest manner. I'll add the warning – Max K May 13 '17 at 23:14
0

I would suggest using a Layout Manager for your app. The one you're looking for is most likely BorderLayout, unless you want specific abilities to control where and how your objects are laid out.

Hope this helps

Layout Managers

How to use BorderLayout

user2277872
  • 2,963
  • 1
  • 21
  • 22