-
Bug
-
Resolution: Duplicate
-
P4
-
None
-
1.1.6
-
x86
-
windows_nt
Name: el35337 Date: 06/08/98
After running the example code below, execute the following steps:
1) Click "New" from the "File" menu. The "Window" menu will now be active and contain the item "New1", & the
frame title will include "New1". The system console will also display "New1" (The JMenuItems w/i the "Window" menu).
2) Click "New" again from the "File" menu. The "Window" menu will now also contain "New2", the frame title will
change, & the system console will display both "New1" & "New2".
3) Click "New1" from the "Window" menu. The frame title will change to "New1".
4) Click "Close" from the "File" menu. The frame title will change to "New2" and "New1" will no longer appear in
the "Window" menu.
5) Click "New" once more from the "File" menu. The frame title will contain "New3" & the system console will display
both "New2" & "New3", but the "Window" menu will only show "New2". If you move the menu highlight using the keyboard
keys, the highlight disappears at the times that it should be highlighting the missing name.
This bug doesn't seem to occur if the item removed is the last one in the menu.
Here's the example code:
import java.awt.event.*;
import java.util.*;
import com.sun.java.swing.*;
import com.sun.java.swing.border.*;
class Bug extends JFrame implements ActionListener
{
// Object-Global Constants
final String
// Menu item labels
menuLabelClose = "Close",
menuLabelExit = "Exit",
menuLabelNew = "New",
mgrNameNew = "New"; // Label for unnamed managers
// Object-global variables
JComponent
content;
JMenu
menuWindow;
JMenuItem
menuClose;
String
mgr; // Active manager
Vector
mgrs = new Vector(); // List of open managers
Bug()
{
super();
setTitle();
// Intercept window closing event
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
System.exit(0);
}
});
// Create menu bar
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu;
JMenuItem item;
// Create "File" menu
menu = new JMenu("File");
menuBar.add(menu);
item = new JMenuItem(menuLabelNew);
item.addActionListener(this);
menu.add(item);
menuClose = new JMenuItem(menuLabelClose);
menuClose.setEnabled(false);
menuClose.addActionListener(this);
menu.add(menuClose);
menu.addSeparator();
item = new JMenuItem(menuLabelExit);
item.addActionListener(this);
menu.add(item);
// Create "Window" menu
menuWindow = new JMenu("Window");
menuWindow.setEnabled(false);
menuBar.add(menuWindow);
menuWindow.addSeparator();
content = (JComponent)getContentPane();
content.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
}
public void actionPerformed(ActionEvent event)
{
String cmd = event.getActionCommand();
// Open new manager
if (cmd == menuLabelNew)
{
// Determine unique label for unnamed manager
int ndxNew = 1;
if (mgrs.size() > 0)
{
int ndx = mgrs.size() - 1;
mgr = (String)mgrs.elementAt(ndx);
ndxNew = Integer.parseInt(mgr.substring(mgrNameNew.length(), mgr.length())) + 1;
}
// Add new manager
mgr = mgrNameNew + ndxNew;
loadMgr();
}
// Close manager
else if (cmd == menuLabelClose)
closeMgr();
// Exit application
else if (cmd == menuLabelExit)
System.exit(0);
// Make chosen manager in window menu the active manager
else
{
// Find manager chosen from window menu
int ndx = 0;
while (ndx < mgrs.size() && !cmd.equals(((String)mgrs.elementAt(ndx))))
ndx++;
if (cmd.equals(((String)mgrs.elementAt(ndx))))
{
mgr = (String)mgrs.elementAt(ndx);
// Change application title to reflect new active manager
setTitle();
}
}
}
void closeMgr()
{
// Remove manager from window list
int ndx = 0;
while (ndx < menuWindow.getItemCount() && !menuWindow.getItem(ndx).getText().equals(mgr))
ndx++;
menuWindow.remove(ndx);
// Remove manager
mgrs.removeElement(mgr);
// Activate next manager if exists
if (mgrs.size() > 0)
mgr = (String)mgrs.firstElement();
else
{
mgr = null;
// Disable menu choices that apply to open managers
menuWindow.setEnabled(false);
menuClose.setEnabled(false);
}
// Change application title to reflect new active manager
setTitle();
}
void loadMgr()
{
mgrs.addElement(mgr);
// Change application title to reflect new active manager
setTitle();
// Add manager to window list
JMenuItem item = new JMenuItem(mgr);
item.addActionListener(this);
menuWindow.add(item);
for (int ndx = 0; ndx < menuWindow.getItemCount(); ndx++)
System.out.println("Menu item " + ndx + ": " + menuWindow.getItem(ndx).getText());
// Enable menu choices that apply to new managers
menuWindow.setEnabled(true);
menuClose.setEnabled(true);
}
void setTitle()
{
if (mgr == null)
super.setTitle(getClass().getName());
else
super.setTitle(getClass().getName() + " - " + mgr);
}
public static void main(String args[]) throws Exception
{
Bug wdw = new Bug();
wdw.pack();
wdw.setVisible(true);
}
}
(Review ID: 33169)
======================================================================
- duplicates
-
JDK-4140571 swing-1.0.1: problem at JMenu.add() after JMenu.remove()
-
- Resolved
-