1

I am new to java swing, I wrote a startup program to formart text, but i am confused with the layout, the result is below: enter image description here

I want the combobox and the button are placed middle of the ctrlPanel, and the combobox should not be stretched

 public class MainFrame extends JFrame {
private static final long serialVersionUID = 7553142908344084288L;

private static String[] formats = new String[] {
    "JSON",
    "XML",
    "YAML"
};

public MainFrame() {
    super("jValidator");
    Panel mainPanel = new Panel();
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
    setContentPane(mainPanel);

    JTextArea fromTextArea = new JTextArea(20, 40);
    JScrollPane fromTextAreaScrollPanel = new JScrollPane(fromTextArea);
    fromTextAreaScrollPanel.setPreferredSize(new Dimension(300, 300));
    fromTextAreaScrollPanel.setBorder(BorderFactory.createEmptyBorder(15, 5, 15, 5));
    mainPanel.add(fromTextAreaScrollPanel);

    JButton fmtButton = new JButton("Format >>");
    JComboBox jComboBox = new JComboBox(formats);
    jComboBox.setBorder(BorderFactory.createTitledBorder("Text Format"));

    JPanel ctrPanel = new JPanel();
    ctrPanel.setLayout(new BoxLayout(ctrPanel, BoxLayout.Y_AXIS));
    ctrPanel.setAlignmentY(Component.CENTER_ALIGNMENT);
    ctrPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));

    ctrPanel.add(jComboBox);
    ctrPanel.add(Box.createRigidArea(new Dimension(50, 15)));
    ctrPanel.add(fmtButton);
    mainPanel.add(ctrPanel);

    JTextArea toTextArea = new JTextArea(20, 40);
    JScrollPane toTextAreaScrollPanel = new JScrollPane(toTextArea);
    toTextAreaScrollPanel.setPreferredSize(new Dimension(300, 300));
    toTextAreaScrollPanel.setBorder(BorderFactory.createEmptyBorder(15, 5, 15, 5));
    mainPanel.add(toTextAreaScrollPanel);

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    setVisible(true);
}

public static void main(String[] args) {
    new MainFrame();
}
 }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
jamee
  • 553
  • 1
  • 5
  • 25
  • For better help sooner, post a [MCTaRE](http://stackoverflow.com/help/mcve) (Minimal Complete Tested and Readable Example). One typical approach would be to put those components into a panel, and add the panel to a `GridBagLayout` with no constraint. The panel (and components inside it) will be centered in the container. – Andrew Thompson Mar 04 '14 at 06:50
  • You can use setLayout(null) and place all the individual components using setBounds(x,y,width,height) method whereever you want – Stunner Mar 04 '14 at 06:50
  • 3
    `.setPreferredSize(` See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) @Stunner *"You can use setLayout(null)"* Alternately, you could use a course of action that is not going to create twice as many problems as it solves. Until you come up with some better advice, please read rather than comment. – Andrew Thompson Mar 04 '14 at 06:52

2 Answers2

3

You could use a GridBagLayout instead of a BoxLayout...

JPanel ctrPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
ctrPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));

gbc.fill = GridBagConstraints.HORIZONTAL;
ctrPanel.add(jComboBox, gbc);
ctrPanel.add(Box.createRigidArea(new Dimension(50, 15)), gbc);
gbc.fill = GridBagConstraints.NONE;
ctrPanel.add(fmtButton, gbc);

enter image description here

Take a look at Laying Out Components Within a Container for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • @alex2410 You're output looks better then mine ;) - Good example of how different Look and Feels effect layouts though +1 – MadProgrammer Mar 04 '14 at 06:59
  • thanks very much, but I am still not know why these two components(combobox and button) placed in the middle of the ctrlPanel? – jamee Mar 04 '14 at 07:37
  • It's the way that layout manager works. Not an expert with `BoxLayout` – MadProgrammer Mar 04 '14 at 07:39
  • Thanks, anyway, another problem is: when I resize the frame, these textarea doesnot stretch with frame size? – jamee Mar 05 '14 at 08:22
  • My answer would be to use `GridBagLayout`, allowing the `GridBagConstraints` to use `weightx` and `weighty` equal `1` and `fill` to equal `GridBagConstraints.BOTH`... – MadProgrammer Mar 05 '14 at 08:46
3

For that purposes I recommend you to use another LayoutManager, for example GridBagLayout change creation of ctrPanel like next :

JButton fmtButton = new JButton("Format >>");
JComboBox jComboBox = new JComboBox(formats);
jComboBox.setBorder(BorderFactory.createTitledBorder("Text Format"));

JPanel ctrPanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx=0;
c.gridy=1;
ctrPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
ctrPanel.add(fmtButton,c);
c.gridy=0;
c.fill = GridBagConstraints.HORIZONTAL;
ctrPanel.add(jComboBox,c);
mainPanel.add(ctrPanel);

And it looks like:

enter image description here

alex2410
  • 10,904
  • 3
  • 25
  • 41