Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - having buttons displaying arrows

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

like image 975
Pavel Janicek Avatar asked Dec 14 '11 09:12

Pavel Janicek


3 Answers

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);
like image 89
Harry Joy Avatar answered Oct 23 '22 15:10

Harry Joy


Using an ImageIcon on the button, or one of the Swing BasicArrowButtons is probably the best approach. However, you could also use Unicode arrow symbols. For example

\u25C4: ◄

\u2190: ←

\u25BA: ►

\u2192: →

Some resources:

  • http://en.wikipedia.org/wiki/Arrow_%28symbol%29
  • http://www.alanwood.net/unicode/arrows.html
  • Unicode symbols (arrows) in Java

You would need to ensure that the font(s) your application may use supports these symbols.

like image 42
sudocode Avatar answered Oct 23 '22 16:10

sudocode


Just use a image:

ImageIcon icon = new ImageIcon("images/icon.gif");
JButton button2 = new JButton(icon);
like image 29
RoflcoptrException Avatar answered Oct 23 '22 14:10

RoflcoptrException