4

I am working on a Swing application in which I have to create multiple menubars on clicking a menu. The other menubar either should be added or removed from the container.

I have to use an icon above and below text in the menu, but I have not found a suitable constructor for it.

Is it possible to have a back and forward button in a JMenu that should work as the back and forward buttons work in a browser? They should display the previous view of window, preferably without multiple displays.

Joehot200
  • 1,070
  • 15
  • 44
adesh kumar
  • 129
  • 3
  • 10
  • 4
    Posting the relevant section of your code or better an [SSCCE](http://www.sscce.org) would help us help you better :) – Sujay Sep 28 '12 at 06:25

3 Answers3

1

Do you really mean menu bar? Your buttons sound more like a tool bar thing.

That said, a JMenu is just a special kind of button, so perhaps you can derive your own custom menu class from that, in order to paint top level menus in a different way. For the main menu bar, some platforms (Mac OS in particular) probably won't handle the custom painting code, as that menu bar is handled by native OS code. But for a second menu bar, which gets painted by swing, I'd expect no problems. After all, a JMenuBar is just a Component.

MvG
  • 57,380
  • 22
  • 148
  • 276
1

If you go with multiple instances of JToolBar, shown here, you can add each one to a different panel of CardLayout, shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

Just use two JPanels.

Here is some code that I wrote a few minutes ago for the start of a shop GUI.

public Shop() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);

    JPanel panel = new JPanel();
    contentPane.add(panel, BorderLayout.NORTH);

    JPanel panel_1 = new JPanel();
    contentPane.add(panel_1, BorderLayout.CENTER);
    {
    JMenuBar menu = new JMenuBar();
    final JTextField coins =  new JTextField("Coins: " + TerrainDemo.money); 
    coins.setForeground(Color.red);
    menu.add(coins);
    panel.add(menu);
    }
    {
    JMenuBar menu = new JMenuBar();
    final JTextField coins =  new JTextField("Coins: " + TerrainDemo.money); 
    coins.setForeground(Color.red);
    menu.add(coins);
    panel_1.add(menu);
    }
}

Which results in:

enter image description here

Joehot200
  • 1,070
  • 15
  • 44