Java set background button

Set JButton background color on windows

I used this for mac and it showed up as I wanted it to be. However, upon trying it on windows, the foreground is white(as it should) but the background is empty. Setting background color to JButton says to add button.setContentAreaFilled(false); which I did but had no effect. Most others say to add button.setOpaque(true); which I also did already. What else do I have to do so that it will show up with a black background? EDIT As requested, the SSCCE:

import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class MainSwing extends JFrame < private static final long serialVersionUID = -8231889836024827530L; public static void main(String[] args) < try < System.setProperty("apple.laf.useScreenMenuBar", "true"); System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Test"); UIManager.put("ScrollBarUI", "main.CustomScrollBarUI"); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); >catch(ClassNotFoundException e) < System.out.println("ClassNotFoundException: " + e.getMessage()); >catch(InstantiationException e) < System.out.println("InstantiationException: " + e.getMessage()); >catch(IllegalAccessException e) < System.out.println("IllegalAccessException: " + e.getMessage()); >catch(UnsupportedLookAndFeelException e) < System.out.println("UnsupportedLookAndFeelException: " + e.getMessage()); >SwingUtilities.invokeLater( new Runnable() < public void run() < JFrame frame = new JFrame() < Container c = getContentPane(); JButton button = new JButton("Hello"); < button.setText("Hello"); button.setVisible(true); button.setPreferredSize(new Dimension(100, 50)); button.setBorder(BorderFactory.createLineBorder(Color.WHITE, 1)); button.setBackground(Color.BLACK); button.setForeground(Color.WHITE); button.setOpaque(true); c.add(button); >>; frame.setSize(500, 500); frame.setBackground(Color.BLACK); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); > >); > > 

It seems that the problem has something to do with the line: UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); as when I remove it, the buttons are black.

Источник

Setting Background of a Jbutton

I have 5 JButtons: b1, b2, b3, b4, b5. By default, their color is gray. When I click on any button, the background of that button changes to white. When I click another button, I want that previous clicked button to change its background to gray, and this newly clicked button to change its background to white. Here is the code that I wrote:

int liveButton = 0; //holds the value of the button that is last clicked. //0 indicates no button clicked (in the beginning) private void ChangeInUsersList(int clickedButton) < switch(liveButton) < case 1 : b1.setBackground(Color.GRAY); break; case 2 : b2.setBackground(Color.GRAY); break; case 3 : b3.setBackground(Color.GRAY); break; case 4 : b4.setBackground(Color.GRAY); break; case 5 : b5.setBackground(Color.GRAY); break; default: System.out.println("No button to change"); >liveButton = clickedButton;// store the clicked button to change its //background later > private void b1ActionPerformed(java.awt.event.ActionEvent evt) < ChangeInUsersList(1); b1.setBackground(new java.awt.Color(255,255,255)); >private void b2ActionPerformed(java.awt.event.ActionEvent evt) < ChangeInUsersList(2); b2.setBackground(new java.awt.Color(255,255,255)); >private void b3ActionPerformed(java.awt.event.ActionEvent evt) < ChangeInUsersList(3); b3.setBackground(new java.awt.Color(255,255,255)); >private void b4ActionPerformed(java.awt.event.ActionEvent evt) < ChangeInUsersList(4); b4.setBackground(new java.awt.Color(255,255,255)); >private void b5ButtonActionPerformed(java.awt.event.ActionEvent evt)

However, its not working as expected. When i click on a button, its background does change to white. However, if i click on some other button after that, the former button’s background doesnt change to grey. I tried replacing Color.GREY with new java.awt.Color(236,233,216) — the rgb for grey but it still doesnt work.

Читайте также:  Какие бывают блоки инициализации java

Источник

android set button background programmatically

I would like to know how to set the button color programatically? I have coded the following but fails:

Button11.setBackgroundColor(R.color.red); 

what effect are you trying to get? A plain red rectangle with no additional effects? If not you’re going to need some sort of drawable to achieve what your after, either a png (9patch) or something defined in xml. The system has no built in way to make a button that looks like a normal button except a different color. You’ll have to provide your own resource unless you are looking for just a plain single colored rectangle.

actually what i am doing is that out of a table of 12 buttons, the program would randomly select 6 of them to fill in defined text. I would like these 6 buttons to change into another different color. In this regards, it cannot be done in the xml part but just be programmatically. I have already defined some xml (first unselected is plain green round color, if selected i wish it become plain red round color) If in this way, can it be done?

Right but you either need to have a red button image like a 9patch png, or you’ll need to define a red button shape in xml. If you simply change the background color to red you are going to end up with a plain red rectangle not something that looks like a normal button.

yes u are correct i have modified and it reli gives out a plain red rectangle that even cannot pressed!! I have already separately defined red_button_xml, how could that be incorporated to the Activity? Thanks a lot!

Источник

How to change the background color of a Jbutton on click

I’m trying to change the color of a JButton when you click on it, but a blue color appears instead of the one I wrote about. Goal: The goal is that when you click on the button the color change I did several searches, but each time a blue font appeared. I tried overide paintComponent, several alternatives such as this.getModel, but nothing works. My button class:

package com.tralamy.lancherX.display; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TButton extends JButton < private Color hoverColor = Display.LIGHT_GRAY; private Color pressColor = Display.LIGHT_DARK; private Color firstColor; private Color basic; private MouseAdapter hoverAdapter = new MouseAdapter() < @Override public void mouseEntered(MouseEvent e) < basic = firstColor; TButton.this.setBackground(hoverColor); >@Override public void mouseExited(MouseEvent e) < TButton.this.setBackground(basic); >>; private MouseAdapter pressAdapter = new MouseAdapter() < @Override public void mousePressed(MouseEvent e) < TButton.this.setBackground(pressColor); super.mousePressed(e); >@Override public void mouseReleased(MouseEvent e) < TButton.this.setBackground(basic); super.mouseReleased(e); >>; public TButton (String text) < super(text); init(); >public TButton (Icon icon) < super(icon); init(); >public TButton () < super(); init(); >private void init() < firstColor = this.getBackground(); setBorder(null); setBorderPainted(false); setContentAreaFilled(false); setOpaque(false); setFocusPainted(false); setPressedIcon(new ImageIcon()); >@Override public void setBackground(Color bg) < super.setBackground(bg); firstColor = bg; >public void setHover() < this.addMouseListener(hoverAdapter); >public void removeHover() < this.removeMouseListener(hoverAdapter); >public void setPress() < this.addMouseListener(pressAdapter); >public void removePress() < this.removeMouseListener(pressAdapter); >public void setHoverColor(Color color) < hoverColor = color; >public Color getHoverColor() < return hoverColor; >public Color getPressColor() < return pressColor; >public void setPressColor(Color pressColor) < this.pressColor = pressColor; >> 
private JPanel menuPanel() < mp = new JPanel(); setPercentWidth(mp, 25); mp.setBackground(LIGHT_GRAY); mp.setLayout(new BorderLayout()); JPanel userSection = new JPanel(); userSection.setLayout(new GridBagLayout()); setPercentHeight(userSection, 5); userSection.setPreferredSize(new Dimension(userSection.getWidth(), 0)); userSection.setBackground(LIGHT_DARK); userIconButton.setHorizontalAlignment(SwingConstants.CENTER); userName.setHorizontalAlignment(SwingConstants.CENTER); userIconButton.setBorder(new EmptyBorder(0,0,0,10)); userSection.add(userIconButton); userSection.add(userName); menuButtons.add(new TButton("Library")); menuButtons.add(new TButton("Store")); menuButtons.add(new TButton("Friends")); menuButtons.add(new TButton("News")); JPanel menuSection = new JPanel(); menuSection.setLayout(new BoxLayout(menuSection, BoxLayout.Y_AXIS)); menuSection.setOpaque(false); for (TButton button : menuButtons) < button.setAlignmentX(TButton.CENTER_ALIGNMENT); button.setFont(App.setSemiBoldNunito(48)); button.setForeground(SUPER_SUPER_LIGHT_GRAY); button.setBackground(SUPER_LIGHT_GRAY); button.setBorder(null); button.setBorderPainted(true); button.setContentAreaFilled(true); button.setOpaque(true); button.setHoverColor(DARK_GRAY); button.setHover(); button.setPressColor(LIGHT_DARK); button.setPress(); TButton marginLabel = new TButton(); marginLabel.setSize(MAIN_MENU_MARGIN, MAIN_MENU_MARGIN); marginLabel.setMaximumSize(new Dimension(MAIN_MENU_MARGIN, MAIN_MENU_MARGIN)); marginLabel.setMinimumSize(new Dimension(MAIN_MENU_MARGIN, MAIN_MENU_MARGIN)); setPercentWidth(button, 20); menuSection.add(marginLabel); menuSection.add(button); >mp.add(menuSection); mp.add(userSection, BorderLayout.SOUTH); return mp; > 

Источник

Оцените статью