Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java swing layout of two components

    +--------------------------------------------+
    |                 +-------+      +----------+|
    |                 | +---+ |      |  +-----+ ||
    |                 | | A | |      |  |  B  | ||
    |                 | +---+ |      |  +-----+ ||
    |                 +-------+      +----------+|
    +--------------------------------------------+
                          ^
                          |
                          |
                        Center

Background: I have

  • a JButton ("A"), size 50x25, within a JPanel (FlowLayout.CENTER)
  • a JLabel ("B"), size 100x25, within a JPanel (FlowLayout.RIGHT)
  • the two JPanels are in a JPanel

Desired Result: I want

  • The JButton "A" to always be centered horizontally,
  • The JLabel "B" to always be flush right.

Thing I've tried: These didn't work for me

  • BorderLayout is not working for me because JButton "A" is shifted LEFT:
  • I'd prefer not to put an invisible component WEST to undo the shift

    +--------------------------------------------+
    |            +-------+           +----------+|
    |            | +---+ |           |  +-----+ ||
    |            | | A | |           |  |  B  | ||
    |            | +---+ |           |  +-----+ ||
    |            +-------+           +----------+|
    +--------------------------------------------+
                     ^    ^
                     |    |
                     |    |
                     |  Center
                     |
                   Shifted Left
    
  • GridLayout won't work because I don't want the "A" and "B" to be expanded

Appreciate any suggestions!

p.s.

The JButton/JLabels are each inside of their own JPanels because WITHOUT the Jpanel, BorderLayout.CENTER expands the JButton across the entire width of the major panel (up to the left edge of the JLabel). The JPanels are not needed/critical to the statement of the problem

Conclusion:

  • I went with "Hovercraft Full Of Eels" answer posted below. Thanks!
like image 452
casio car Avatar asked Oct 21 '11 15:10

casio car


People also ask

What is the difference between FlowLayout and BorderLayout?

I know the difference between the two layouts in the title, that FlowLayout adds onjects onto the canvas one after each other in a line from left to right, then taking a new line when the edge of the canvas is reached. Also that BorderLayout can be set to the NORTH, SOUTH, EAST, WEST and CENTER of the canvas.


1 Answers

You should nest JPanels and use a combination of layouts. Placing the panels holding the JButtons into another JPanel that uses GridLayout(1, 0) (1 row, variable number of columns) could work, and placing that JPanel into the BorderLayout.NORTH position of a BorderLayout-using JPanel could work.

For example

import java.awt.*;
import javax.swing.*;

public class Foo003 {

   private static void createAndShowGui() {
      JButton btnA = new JButton("A");
      JButton btnB = new JButton("B");

      btnA.setPreferredSize(new Dimension(50, 25));
      btnB.setPreferredSize(new Dimension(100, 25));

      JPanel btnAPanel = new JPanel(); // uses default FlowLayout.CENTER
      JPanel btnBPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
      btnAPanel.add(btnA);
      btnBPanel.add(btnB);

      JPanel topPanel = new JPanel(new GridLayout(1, 0));
      topPanel.add(new JLabel("")); // empty placeholder label
      topPanel.add(btnAPanel);
      topPanel.add(btnBPanel);

      JPanel mainPanel = new JPanel(new BorderLayout());
      mainPanel.add(topPanel, BorderLayout.NORTH);

      mainPanel.setPreferredSize(new Dimension(400, 300));

      JFrame frame = new JFrame("Foo003");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
like image 55
Hovercraft Full Of Eels Avatar answered Oct 30 '22 04:10

Hovercraft Full Of Eels