-
Enhancement
-
Resolution: Unresolved
-
P4
-
None
-
1.2.0
-
Fix Understood
-
generic
-
solaris_2.5.1
/*
* SeparatorProblem.java
*
* This is a test case to show the separator problems.
* Here is the list of the following separator problems:
*
* 1) JMenuBar.addSeparator(), JToolBar.addSeparator(), and
* JMenuBar.insertSeparator() do not return the
* separator component that was created.
* 2) Removing a separator can be difficult since you do
* not have a handle from methods mentioned in 1).
* 3) There is no JToolBar.insertSeparator();
* You need to do a new on an inner class and
* add it. See the example in updateGUI method.
* 4) Removing a menu separator can only be done by
* specifying a position. Getting the handle when
* it is created and doing a remove does not work.
* See the example in updateGUI method.
*
* To run, do the following:
*
* 1) java SeparatorProblem
* 2) select Test on the File pulldown menu.
* 3) repeat step two.
*
* To see correct behavior, change the remove call in the updateGUI method.
*
*/
import com.sun.java.swing.*;
import com.sun.java.swing.event.*;
import com.sun.java.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.lang.*;
/* test case to test out the glass pane */
public class SeparatorProblem extends JFrame {
JMenuBar menuBar = null;
JToolBar toolBar = null;
TestActionListener actionListener = new TestActionListener();
boolean testState = false; // keep track of state to check if changed
JMenuItem insertedMenuItem = null;
JButton insertedButton = null;
JComponent separator = null;
JToolBar.Separator buttonSepar = null;
public SeparatorProblem( ) {
super( "Test Pane" );
menuBar = createMenuBar();
setJMenuBar( menuBar );
toolBar = createToolBar();
getContentPane().add( "North", toolBar ); /* default is center */
setSize(600,400);
}
/**
* create a test menu bar.
*/
private JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu menu = null;
JMenuItem menuItem = null;
/* File menu pulldown */
menu = new JMenu( "File");
menuItem = new JMenuItem("Open");
menuItem.setActionCommand("Open");
menuItem.addActionListener( actionListener );
menu.add( menuItem );
menuItem = new JCheckBoxMenuItem("Test");
menuItem.setActionCommand("Test");
menuItem.setSelected( testState );
menuItem.addActionListener( actionListener );
menu.add( menuItem );
menu.addSeparator();
menuItem = new JMenuItem( "Exit" );
menuItem.setActionCommand("Exit");
menuItem.addActionListener( actionListener );
menu.add( menuItem );
menuBar.add( menu );
return menuBar;
}
/**
* create a test tool bar.
*/
private JToolBar createToolBar() {
JToolBar toolBar = new JToolBar();
JButton button = new JButton("One");
button.setToolTipText("One");
button.addActionListener( actionListener );
toolBar.add( button );
button = new JButton("Two");
button.setToolTipText("Two");
button.addActionListener( actionListener );
toolBar.add( button );
return toolBar;
}
/**
* Handle the Test and Exit commands.
*/
public class TestActionListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
String action = evt.getActionCommand();
System.out.println("TestActionListener: action = "+action);
if ( action.equals("Exit") ) {
System.exit(0);
}
else if ( action.equals("Test") ) {
/* check if state has changed. */
boolean newTestState =
((JMenuItem)evt.getSource()).isSelected();
if ( newTestState != testState ) {
updateGUI( newTestState );
testState = newTestState;
}
}
}
}
/**
* dynamically update the GUI, especially the menus and the
* toolBar. This is a simplified example.
*/
public void updateGUI( boolean insertFlag ) {
JMenu fileMenu = (JMenu) menuBar.getComponentAtIndex( 0 );
/* change the menu */
if ( fileMenu != null ) {
if ( insertFlag ) {
// note that we can not insert a JSeparator since
// it must of class Action.
fileMenu.insertSeparator( 2 );
separator = (JComponent)fileMenu.getMenuComponent( 2 ); /* save
for removal */
System.out.println("Menu Separator="+separator);
if ( insertedMenuItem == null ) {
insertedMenuItem = new JMenuItem("Inserted");
insertedMenuItem.setActionCommand("Inserted");
insertedMenuItem.addActionListener( actionListener );
}
fileMenu.insert( insertedMenuItem, 3 );
}
else {
fileMenu.remove( insertedMenuItem );
/***********************************************
* LOOK HERE
*
* the following does not remove the separator.
* Instead, must remove by position which is bad
* since other applications are more dynamic.
***********************************************/
fileMenu.remove( separator );
// fileMenu.remove( 3 ); // this works
}
}
/* menu does not need to be repainted since the popup goes away */
/* change tool bar */
if ( insertFlag ) {
if ( insertedButton == null ) {
insertedButton = new JButton("Inserted");
insertedButton.setToolTipText("Inserted");
insertedButton.addActionListener( actionListener );
}
toolBar.add( insertedButton, 0 );
/***********************************************
* LOOK HERE
*
* There is no insertSeparator. So, new the
* inner class.
***********************************************/
if ( buttonSepar == null )
buttonSepar = toolBar.new Separator();
toolBar.add( buttonSepar, 1 );
}
else {
toolBar.remove( insertedButton );
toolBar.remove( buttonSepar ); /* remove separator */
}
toolBar.validate();
toolBar.paintAll( toolBar.getGraphics() );
}
public static void main( String args[] ) {
SeparatorProblem frame = new SeparatorProblem();
frame.setVisible( true );
}
}
nasser.nouri@Corp 1998-06-19
* SeparatorProblem.java
*
* This is a test case to show the separator problems.
* Here is the list of the following separator problems:
*
* 1) JMenuBar.addSeparator(), JToolBar.addSeparator(), and
* JMenuBar.insertSeparator() do not return the
* separator component that was created.
* 2) Removing a separator can be difficult since you do
* not have a handle from methods mentioned in 1).
* 3) There is no JToolBar.insertSeparator();
* You need to do a new on an inner class and
* add it. See the example in updateGUI method.
* 4) Removing a menu separator can only be done by
* specifying a position. Getting the handle when
* it is created and doing a remove does not work.
* See the example in updateGUI method.
*
* To run, do the following:
*
* 1) java SeparatorProblem
* 2) select Test on the File pulldown menu.
* 3) repeat step two.
*
* To see correct behavior, change the remove call in the updateGUI method.
*
*/
import com.sun.java.swing.*;
import com.sun.java.swing.event.*;
import com.sun.java.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.lang.*;
/* test case to test out the glass pane */
public class SeparatorProblem extends JFrame {
JMenuBar menuBar = null;
JToolBar toolBar = null;
TestActionListener actionListener = new TestActionListener();
boolean testState = false; // keep track of state to check if changed
JMenuItem insertedMenuItem = null;
JButton insertedButton = null;
JComponent separator = null;
JToolBar.Separator buttonSepar = null;
public SeparatorProblem( ) {
super( "Test Pane" );
menuBar = createMenuBar();
setJMenuBar( menuBar );
toolBar = createToolBar();
getContentPane().add( "North", toolBar ); /* default is center */
setSize(600,400);
}
/**
* create a test menu bar.
*/
private JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu menu = null;
JMenuItem menuItem = null;
/* File menu pulldown */
menu = new JMenu( "File");
menuItem = new JMenuItem("Open");
menuItem.setActionCommand("Open");
menuItem.addActionListener( actionListener );
menu.add( menuItem );
menuItem = new JCheckBoxMenuItem("Test");
menuItem.setActionCommand("Test");
menuItem.setSelected( testState );
menuItem.addActionListener( actionListener );
menu.add( menuItem );
menu.addSeparator();
menuItem = new JMenuItem( "Exit" );
menuItem.setActionCommand("Exit");
menuItem.addActionListener( actionListener );
menu.add( menuItem );
menuBar.add( menu );
return menuBar;
}
/**
* create a test tool bar.
*/
private JToolBar createToolBar() {
JToolBar toolBar = new JToolBar();
JButton button = new JButton("One");
button.setToolTipText("One");
button.addActionListener( actionListener );
toolBar.add( button );
button = new JButton("Two");
button.setToolTipText("Two");
button.addActionListener( actionListener );
toolBar.add( button );
return toolBar;
}
/**
* Handle the Test and Exit commands.
*/
public class TestActionListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
String action = evt.getActionCommand();
System.out.println("TestActionListener: action = "+action);
if ( action.equals("Exit") ) {
System.exit(0);
}
else if ( action.equals("Test") ) {
/* check if state has changed. */
boolean newTestState =
((JMenuItem)evt.getSource()).isSelected();
if ( newTestState != testState ) {
updateGUI( newTestState );
testState = newTestState;
}
}
}
}
/**
* dynamically update the GUI, especially the menus and the
* toolBar. This is a simplified example.
*/
public void updateGUI( boolean insertFlag ) {
JMenu fileMenu = (JMenu) menuBar.getComponentAtIndex( 0 );
/* change the menu */
if ( fileMenu != null ) {
if ( insertFlag ) {
// note that we can not insert a JSeparator since
// it must of class Action.
fileMenu.insertSeparator( 2 );
separator = (JComponent)fileMenu.getMenuComponent( 2 ); /* save
for removal */
System.out.println("Menu Separator="+separator);
if ( insertedMenuItem == null ) {
insertedMenuItem = new JMenuItem("Inserted");
insertedMenuItem.setActionCommand("Inserted");
insertedMenuItem.addActionListener( actionListener );
}
fileMenu.insert( insertedMenuItem, 3 );
}
else {
fileMenu.remove( insertedMenuItem );
/***********************************************
* LOOK HERE
*
* the following does not remove the separator.
* Instead, must remove by position which is bad
* since other applications are more dynamic.
***********************************************/
fileMenu.remove( separator );
// fileMenu.remove( 3 ); // this works
}
}
/* menu does not need to be repainted since the popup goes away */
/* change tool bar */
if ( insertFlag ) {
if ( insertedButton == null ) {
insertedButton = new JButton("Inserted");
insertedButton.setToolTipText("Inserted");
insertedButton.addActionListener( actionListener );
}
toolBar.add( insertedButton, 0 );
/***********************************************
* LOOK HERE
*
* There is no insertSeparator. So, new the
* inner class.
***********************************************/
if ( buttonSepar == null )
buttonSepar = toolBar.new Separator();
toolBar.add( buttonSepar, 1 );
}
else {
toolBar.remove( insertedButton );
toolBar.remove( buttonSepar ); /* remove separator */
}
toolBar.validate();
toolBar.paintAll( toolBar.getGraphics() );
}
public static void main( String args[] ) {
SeparatorProblem frame = new SeparatorProblem();
frame.setVisible( true );
}
}
nasser.nouri@Corp 1998-06-19