Java - having buttons displaying arrows

Pavel Janicek picture Pavel Janicek · Dec 14, 2011 · Viewed 17.7k times · Source

I would like to have a buttons in Java which shows the arrows - like on the keyboard. So far I have this

JButton arrowUp = new JButton("^");
JButton arrowDown = new JButton("v");
JButton arrowLeft = new JButton("<");
JButton arrowRight = new JButton(">");

It kinda works ... but does not look quite nice.

Any help how to improve this is appreciated

Answer

Harry Joy picture Harry Joy · Dec 14, 2011

Swing has a default arrow button class that is BasicArrowButton

Example:

    JFrame frame = new JFrame("Arrow Button Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(new BasicArrowButton(BasicArrowButton.EAST), BorderLayout.EAST);
    frame.add(new BasicArrowButton(BasicArrowButton.NORTH), BorderLayout.NORTH);
    frame.add(new BasicArrowButton(BasicArrowButton.SOUTH), BorderLayout.SOUTH);
    frame.add(new BasicArrowButton(BasicArrowButton.WEST), BorderLayout.WEST);
    frame.pack();
    frame.setVisible(true);