Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

implementing a dynamically changing menubar (like in IE etc.)  RSS feed

Sonny Gill
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Totally stumped with this one. Have tried all kinds of different things, nothing is working.
I tried Sun's java tutorial, searching on google and a few different forums, but no luck.
I am trying to implement a dynamic menubar. When the window is resized the menubar should only display the menus that can be displayed at their preferred size, and rest of the menus should be moved to an extension menu at the end, as its submenus.( like Internet Explorer etc.)
Here is an inefficient but safe way, i tried to implement it.
1. Sometimes the "extension" menu is displayed, sometimes it is not, very erratic behaviour
2. Sometimes menus just disappear, if i suddenly resize to too small
3. Sometimes, there is an ugly gap appears between the second-to-last menu and the "extension" menu.
Please help, any pointers, hints, links to relevant resources will be much appreciated.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
class DynMBar extends javax.swing.JMenuBar{
private Vector menus = new Vector();
// Extension menu to display menus that cannot be displayed in the menubar
JMenu extM = new JMenu(">>");
public DynMBar(){
setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
addComponentListener(new ResizeListener());
public JMenu add(JMenu m){
// Every menu item is stored in the private vector, if it does not already contain it
return super.add(m);
// Every time the menu is resized, check what menus can be displayed at their preferred size and move the rest to
class ResizeListener extends ComponentAdapter{
public void componentResized(ComponentEvent evt){
int maxWidth = getSize().width; // menubar width
int sum = 0;
int extW = extM.getPreferredSize().width;
int numM = menus.size();
// This is admittedly inefficient, but just to be on safe side
boolean addToExt = false;
for(int i=0;i<numM;i++){
JMenu m = (JMenu)menus.get(i);
int mW = m.getPreferredSize().width;

// If there is enough space, add the menu to the menubar
sum += mW;
// if there is not enough space left on the menu bar, add it to the "extended" menu
sum += mW;
addToExt = true;
// if any menus have been added to the "extended" menu, add it to the menubar
// repaint(); // calling repaint makes no difference

/* Main-class to test the "dynamic" menu */
public class LayoutTest{
public static void main(String args[]){
JFrame f = new JFrame("Test Layout");

String[] menuLabels = {"File", "Edit", "Format", "View", "Tools", "Configure", "Admin", "Compile", "Options", "Help"};
DynMBar mbar = new DynMBar();
// make the menubar stand out
for(int i=0;i<menuLabels.length;i++){
//add the value of 'i' to menu-text to easily identify it
JMenu jm = new JMenu(i + ". " + menuLabels[i]);
f.setSize(800, 200);
f.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});;
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!