user interface - How to set a background color of a JButton in Java? -


i developing java desktop application. in have 4 jbuttons on jpanel. want whenever button clicked background color changes other color (say orange) represent has been clicked , background color of other 3 buttons reset default color (in case of them had orange background color).

so, @ 1 time 1 button can have orange color.

the current approach have applied have implemented following code in xxxactionperformed() method of jbutton button1

button1.setbackground(color.orange); button2.setbackground(color.gray); button3.setbackground(color.gray); button4.setbackground(color.gray); 

and rest 3 buttons.

now in actual, don't want backgroud color gray (for unclicked button). instead, want default background color backgroud color adjust look-and-feel of gui according end-user's platform's look-and-feel.

q1. how can default background color?

q2. correct approach or there other mechanism through can group 4 buttons in button group 1 can have specified property @ 1 time (like radio buttons)?

  1. just use null use default color:

    button1.setbackground(color.orange); button2.setbackground(null); ... 
  2. consider using jtogglebuttons buttongroup, set icon , pressedicon of buttons. no need change background color.

    button1 = new jtogglebutton(new imageicon("0.jpg")); button1.setselectedicon(new imageicon("1.jpg")); button2 = new jtogglebutton(new imageicon("0.jpg")); button2.setselectedicon(new imageicon("2.jpg")); ... buttongroup group = new buttongroup(); group.add(button1); group.add(button2); ... 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -