I am creating an window with a JToolBar
containing two buttons. One is a regular JButton
and the other is a BasicArrowButton
(javax.swing.plaf.basic.BasicArrowButton
). Without doing any additional configuration, the JButton
does not expand in the toolbar, but the BasicArrowButton
expands to occupy the complete toolbar.
I have tried to configure it to fit in a small 16x16 square by setting its maximum and preferred size to 16x16. But that doesn't work. Also tried using setSize() without success. Can anyone tell me where the problem could be?
I have also tried to use a horizontal glue (I am using WindowBuilder with Eclipse) to the right of the BasicArrowButton
. That didn't work either.
I am using JDK1.7.0_07.
public class ToolbarTest {
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ToolbarTest window = new ToolbarTest();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public ToolbarTest() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JToolBar toolBar = new JToolBar();
frame.getContentPane().add(toolBar, BorderLayout.NORTH);
JButton btnEdit = new JButton("Edit");
toolBar.add(btnEdit);
//------------------------------------------------------------
JButton btnUp = new BasicArrowButton(BasicArrowButton.NORTH);
btnUp.setSize(new Dimension(16, 16));
btnUp.setMaximumSize(new Dimension(16, 16));
toolBar.add(btnUp);
//------------------------------------------------------------
}
}
getMaximumSize()
to instead returngetPreferredSize()
. – Escaut