• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Sonny Gill
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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.
Problems:
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.
thanks
Sandeep
//_______________________________
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
if(!menus.contains(m))menus.add(m);
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
removeAll();
extM.removeAll();
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
if(sum+mW<maxWidth){
add(m);
sum += mW;
}
// if there is not enough space left on the menu bar, add it to the "extended" menu
else{
sum += mW;
addToExt = true;
extM.add(m);
}
}
// if any menus have been added to the "extended" menu, add it to the menubar
if(addToExt)add(extM);
// 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
mbar.setBackground(Color.red);
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]);
mbar.add(jm);
}
f.setJMenuBar(mbar);
f.setSize(800, 200);
f.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});
f.show();
}
}
 
Paper has not yet been outlawed – so let your Java Swing application print with wild abandon: JPDF
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!