I am wondering if anyone can come up with a work-around to the following layout issue I am having with the Nimbus
look-and-feel.
The issue is that the button in the toolbar is not visible because the JToolBar
layout manager does not correctly account for the width of the JTextField
. The Metal
look-and-feel does not seem to exhibit this bug.
import java.awt.*;
import javax.swing.*;
public class TextFieldTest extends JFrame
{
public TextFieldTest()
{
// Create the text field
JTextField textField = new JTextField( 20 )
{
@Override
public Dimension getMaximumSize()
{
return super.getPreferredSize();
}
};
// Create the tool bar
JToolBar toolBar = new JToolBar();
toolBar.add( textField );
toolBar.add( Box.createHorizontalGlue() );
toolBar.add( new JButton( "Button" ) );
// Layout the frame
getContentPane().setLayout( new BorderLayout() );
getContentPane().add( toolBar, BorderLayout.NORTH );
setPreferredSize( new Dimension( 800, 600 ) );
pack();
}
public static void main( String[] args )
{
SwingUtilities.invokeLater( new Runnable()
{
public void run()
{
TextFieldTest test = new TextFieldTest();
test.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
test.setVisible( true );
}
} );
}
}
Any suggestions are appreciated.
The default layout for JToolBar in Nimbus is:
class javax.swing.plaf.synth.SynthToolBarUI$SynthToolBarLayoutManager
You should set it:
toolBar.setLayout(new BoxLayout(toolBar, BoxLayout.X_AXIS));