-
Bug
-
Resolution: Fixed
-
P3
-
1.3.0
-
beta
-
generic
-
generic
Name: yyT116575 Date: 01/16/2001
D:\jdk1.3.0_01\jre\bin>java -version
java version "1.3.0_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0_01)
Java HotSpot(TM) Client VM (build 1.3.0_01, mixed mode)
I am using a JToolBar that is placed inside of a JFrame. The JFrame is using
BorderLayout. The toolbar placement within the JFrame is "North" when the
JFrame first comes up. When the JFrame first becomes visible, the toolbar has
three buttons on it. These three buttons are always present (call these buttons
the "built-in" buttons). Additional buttons are added or removed on the fly
based on user interaction with the nodes of a JTree that is also contained
inside of the JFrame (call these buttons the "dynamic" buttons). The toolbar is
floatable.
Dynamic buttons are added to the toolbar on the fly by creating an Action
object and then adding the object to the toolbar through a call to
JToolBar.add( Action ). Dynamic buttons are removed from the toolbar on the fly
through a call to JToolBar.remove( Component ).
When the JToolBar is docked to any of the borders within the JFrame, it updates
properly when a dynamic button is removed and when a dynamic button is added.
The problem occurs when the toolbar is not docked as described below:
1. Start out with a docked toolbar that has only the three built-in buttons on it.
2. Drag the toolbar outside of the JFrame and leave it floating. The toolbar
automatically sizes itself to the exact size it needs to show the three built-
in buttons.
3. Add two new dynamic buttons to the toolbar as described in detail above. The
toolbar does not update itself to accomodate the new buttons. Only the original
three built-in buttons are visible.
4. Drag the toolbar back to a docked position inside of the JFrame and it
updates properly.
Another scenario:
1. Start out with a docked toolbar that has five buttons on it; the three built-
in buttons and two dynamic buttons that were added on the fly while the toolbar
was docked.
2. Drag the toolbar outside of the JFrame and leave it floating. The toolbar
automatically sizes itself to the exact size it needs to show the five buttons.
3. While the toolbar is floating, remove the two dynamic buttons. The toolbar
does not update itself. The original five buttons still appear even though the
two dynamic buttons were removed.
4. Drag the toolbar back to a docked position inside of the JFrame and it
updates properly.
The same problem occurs in 1.2.2.
Any suggestions on how to force the floating toolbar to update itself properly?
Here is sample code that will demonstrate the problem. When you execute this
code by calling its main() method, you must pass in the name of an icon to use
in the toolbar for the demo.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
// Class to demonstrate the floating toolbar bug. The floating
// toolbar does not update when buttons are added or removed while
// the toolbar is floating.
//
// To start this class execute the main method and pass in the
// name of an icon. The icon will be used to add three "built in"
// toolbar buttons that cannot be removed. The icon will also be
// used to add or remove "dynammic" toolbar buttons that can
// be added or removed in response to user interaction.
public class JFrameDemoFloatingToolBarBug extends javax.swing.JFrame
{
// The name of the toolbar icon
static String szToolBarIcon;
// Create a toolbar for this frame
JToolBar JToolBarDemo = new JToolBar();
// Create a panel to hold a couple of buttons for user interaction
JPanel JPanelCenter = new JPanel();
// Buttons for adding/removing a toolbar button
JButton JButtonAddToolBarButton = new JButton();
JButton JButtonRemoveToolBarButton = new JButton();
// Create three so called "built in" toolbar buttons
// that cannot be removed dynamically
JButton JButtonBuiltIn1 = new JButton();
JButton JButtonBuiltIn2 = new JButton();
JButton JButtonBuiltIn3 = new JButton();
public JFrameDemoFloatingToolBarBug()
{
// Setup this JFrame
setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
setTitle( "Demo Floating ToolBar Bug" );
getContentPane().setLayout( new BorderLayout(0,0) );
setSize( 400,350 );
getContentPane().add( BorderLayout.NORTH,JToolBarDemo );
// Initialize the "built in" toolbar buttons
JButtonBuiltIn1.setIcon( new ImageIcon( szToolBarIcon ) );
JButtonBuiltIn2.setIcon( new ImageIcon( szToolBarIcon ) );
JButtonBuiltIn3.setIcon( new ImageIcon( szToolBarIcon ) );
JButtonBuiltIn1.setActionCommand( "BuiltIn1" );
JButtonBuiltIn2.setActionCommand( "BuiltIn2" );
JButtonBuiltIn3.setActionCommand( "BuiltIn3" );
JToolBarDemo.add( JButtonBuiltIn1 );
JToolBarDemo.add( JButtonBuiltIn2 );
JToolBarDemo.add( JButtonBuiltIn3 );
// Initialize command button that allows a
// dynamic toolbar button to be added
JButtonAddToolBarButton.setBounds( 105,113,194,28 );
JButtonAddToolBarButton.setText( "Add ToolBar Button" );
JButtonAddToolBarButton.setActionCommand( "Add Toolbar Button" );
// Initialize command button that allows a
// dynamic toolbar button to be removed
JButtonRemoveToolBarButton.setBounds( 105,169,194,28 );
JButtonRemoveToolBarButton.setActionCommand( "Remove Toolbar Button" );
JButtonRemoveToolBarButton.setText( "Remove ToolBar Button" );
// Initialize center panel that will contain
// the add/remove buttons
JPanelCenter.setLayout( null );
getContentPane().add( BorderLayout.CENTER,JPanelCenter );
JPanelCenter.setBackground( Color.white );
JPanelCenter.add( JButtonAddToolBarButton );
JPanelCenter.add( JButtonRemoveToolBarButton );
// Listener for window events
addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent event ) {
System.exit( 0 );
}
});
// Listener for add command button
JButtonAddToolBarButton.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent event ) {
// Add a new toolbar button to the toolbar
new DeviceAction();
JFrameDemoFloatingToolBarBug.this.repaint();
}
});
// Listener for remove command button
JButtonRemoveToolBarButton.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent event ) {
// Get a list of all components on the toolbar
Component[] aComponents = JToolBarDemo.getComponents();
// Walk the list of components
for( int nIndex = 0; nIndex < aComponents.length; ++nIndex )
{
// Check for a "dynamic" buttons
JButton jbutton = (JButton) aComponents[ nIndex ];
if( jbutton.getActionCommand().equals( "Dynamic" ) )
{
// Remove one dynamic button
JToolBarDemo.remove( jbutton );
JFrameDemoFloatingToolBarBug.this.repaint();
break;
}
}
}
});
} // End constructor
// Action class for adding new buttons to the toolbar
// using an Action object
class DeviceAction extends AbstractAction
{
public DeviceAction()
{
// Add this action to the toolbar resulting
// in a new button getting added to the
// toolbar
JButton jbutton = JToolBarDemo.add( this );
jbutton.setIcon( new ImageIcon( szToolBarIcon ) );
jbutton.setActionCommand( "Dynamic" );
}
public void actionPerformed( ActionEvent e )
{
}
} // End DeviceAction class
// Pass in the name of the icon to use in the
// demo.
static public void main(String args[])
{
// Check for icon name passed in from command line
if( args.length == 0 )
{
System.out.println( "Usage: JFrameDemoFloatingToolBarBug <icon name>" );
return;
}
// Save the toolbar icon name
szToolBarIcon = args[ 0 ];
// Create the demo frame
( new JFrameDemoFloatingToolBarBug() ).setVisible( true );
}
} // End class JFrameDemoFloatingToolBarBug
(Review ID: 114999)
======================================================================