Layout

hai Friends,Which layout is appreciable in developing a menubar Tino Simon
[95 byte] By [tinosimona] at [2007-10-2 6:01:28]
# 1
FlowLayout.. GridBagLayout is the most flexible one though..
watfora at 2007-7-16 13:01:52 > top of Java-index,Desktop,Core GUI APIs...
# 2

I am trying to use flowlayout for menubar.

I have 4 objects which includes 3 menus and one JComboBox.

and it is found the combobox have a big size and it is not working using setSize(), setBounds().

how can I set the width of combobox fixed in a flow layout?

Tino Simon.

tinosimona at 2007-7-16 13:01:52 > top of Java-index,Desktop,Core GUI APIs...
# 3
setPreferredSize(...)
ajneoa at 2007-7-16 13:01:52 > top of Java-index,Desktop,Core GUI APIs...
# 4
Its not workingcombo.setPreferredSize(new Dimension(30, 25)); But it still consume the rest of area in the menubar and thus seems bigger. Tino Simon.
tinosimona at 2007-7-16 13:01:52 > top of Java-index,Desktop,Core GUI APIs...
# 5
Sorry, it's my mistake. In my example, I was using JPanel to hold the JComboBox, and added the JPanel to the JMenuBar.
ajneoa at 2007-7-16 13:01:52 > top of Java-index,Desktop,Core GUI APIs...
# 6
When usingcombo.setPreferredSize(new Dimension(30, 25)); the height can be adjusted butwidth cannot be......Tino Simon.
tinosimona at 2007-7-16 13:01:52 > top of Java-index,Desktop,Core GUI APIs...
# 7
JMenuBar is using BoxLayout as its Layout Manager, here is what you can do:menuBar.add(Box.createRigidArea(new Dimension(...)));
ajneoa at 2007-7-16 13:01:52 > top of Java-index,Desktop,Core GUI APIs...
# 8
and this as well:menuBar.add(Box.createHorizontalGlue());
ajneoa at 2007-7-16 13:01:52 > top of Java-index,Desktop,Core GUI APIs...
# 9
Did you mean like thismenuBar.add(Box.createRigidArea(new Dimension(25,1)));menuBar.add(databases); But still not working. Tino Simon.
tinosimona at 2007-7-16 13:01:52 > top of Java-index,Desktop,Core GUI APIs...
# 10

When I code as following it works, but I don't think its a proper way

databases = new JComboBox();

JPanel hold_databases = new JPanel();

databases.setPreferredSize(new Dimension(160, 22));

hold_databases.add(databases);

menuBar.add(hold_databases);

Tino Simon.

tinosimona at 2007-7-16 13:01:52 > top of Java-index,Desktop,Core GUI APIs...
# 11

When I done as following it works, but I don't think its a proper way

databases = new JComboBox();

JPanel hold_databases = new JPanel();

databases.setPreferredSize(new Dimension(160, 22));

hold_databases.add(databases);

menuBar.add(hold_databases);

Tino Simon.

tinosimona at 2007-7-16 13:01:52 > top of Java-index,Desktop,Core GUI APIs...
# 12

Here's an example:

import java.awt.*;

import javax.swing.*;

public class MenuBarTest extends JFrame {

private JMenuBar menuBar;

private JMenu menuFile;

private JMenu menuSearch;

private JMenu menuHelp;

private JMenu menuEdit;

private JComboBox combo;

public MenuBarTest() {

try {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

menuBar = new JMenuBar();

setJMenuBar(menuBar);

menuFile = new JMenu("File");

menuBar.add(menuFile);

menuEdit = new JMenu("Edit");

menuBar.add(menuEdit);

menuSearch = new JMenu("Search");

menuBar.add(menuSearch);

menuHelp = new JMenu("Help");

menuBar.add(menuHelp);

menuBar.add(Box.createRigidArea(new Dimension(60,20)));

combo = new JComboBox();

menuBar.add(combo);

menuBar.add(Box.createHorizontalGlue());

pack();

setSize(350, 80);

setLocationRelativeTo(null);

setVisible(true);

} catch (Exception e) { e.printStackTrace(); }

}

public static void main(String[] args) { new MenuBarTest(); }

}

ajneoa at 2007-7-16 13:01:52 > top of Java-index,Desktop,Core GUI APIs...
# 13
Ajneo, THANK YOU very much. Tino Simon.
tinosimona at 2007-7-16 13:01:52 > top of Java-index,Desktop,Core GUI APIs...