Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Buttons need to change colors individually

  1. #1
    Junior Member
    Join Date
    Jan 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Buttons need to change colors individually

    I tried to create an even and then just call said event for each button but it doesn't seem to be working. Down below is the pastebin with the embedded code Just to clarify, there are nine individual buttons. When each button is selected it is supposed to change color. The color to be displayed is shown by its name. However when you execute the code it only alternates between the default colors. As you can see I have tried two different methods to using the ItemEvent and neither of them work.

    ---------------------
    Code starts here
    ---------------------

    package soph.color.arb;

    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.event.ItemEvent;
    import javax.swing.JTextField;
    import javax.swing.JToggleButton;
    import javax.swing.WindowConstants;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    public class ColorShift extends javax.swing.JFrame {
    public JToggleButton jToggleButton1;
    public JToggleButton jToggleButton2;
    public JToggleButton jToggleButton3;
    public JToggleButton jToggleButton4;
    public JToggleButton jToggleButton5;
    public JToggleButton jToggleButton6;
    public JToggleButton jToggleButton7;
    public JToggleButton jToggleButton8;
    public JToggleButton jToggleButton9;

    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    ColorShift inst = new ColorShift();
    inst.setLocationRelativeTo(null);
    inst.setVisible(true);
    }
    });
    }
    public ColorShift() {
    super();
    initGUI();
    }
    private void updateColorIfSelected(JToggleButton btn, ItemEvent ev, Color color) {
    if (ev.getStateChange() == ItemEvent.SELECTED) {
    btn.setBackground(color);
    repaint();
    }
    else
    btn.setBackground(null);
    }
    public void initGUI() {
    try {
    FlowLayout thisLayout = new FlowLayout();
    getContentPane().setLayout(thisLayout);
    setDefaultCloseOperation(WindowConstants.DISPOSE_O N_CLOSE);

    jToggleButton1 = new JToggleButton();
    getContentPane().add(jToggleButton1);
    jToggleButton1.setText("Red");
    jToggleButton1.setPreferredSize(new java.awt.Dimension(100, 100));
    jToggleButton1.addItemListener(ev -> updateColorIfSelected(jToggleButton1, ev, Color.RED));

    jToggleButton2 = new JToggleButton();
    getContentPane().add(jToggleButton2);
    jToggleButton2.setText("Green");
    jToggleButton2.setPreferredSize(new java.awt.Dimension(100, 100));
    jToggleButton2.addItemListener(ev -> updateColorIfSelected(jToggleButton2, ev, Color.GREEN));

    jToggleButton3 = new JToggleButton();
    getContentPane().add(jToggleButton3);
    jToggleButton3.setText("Blue");
    jToggleButton3.setPreferredSize(new java.awt.Dimension(100, 100));
    jToggleButton3.addItemListener(ev -> updateColorIfSelected(jToggleButton3, ev, Color.BLUE));

    jToggleButton4 = new JToggleButton();
    getContentPane().add(jToggleButton4);
    jToggleButton4.setText("Orange");
    jToggleButton4.setPreferredSize(new java.awt.Dimension(100, 100));
    jToggleButton4.addItemListener(ev -> updateColorIfSelected(jToggleButton4, ev, Color.ORANGE));

    jToggleButton5 = new JToggleButton();
    getContentPane().add(jToggleButton5);
    jToggleButton5.setText("Black");
    jToggleButton5.setPreferredSize(new java.awt.Dimension(100, 100));
    jToggleButton5.addItemListener(ev -> updateColorIfSelected(jToggleButton5, ev, Color.BLACK));

    jToggleButton6 = new JToggleButton();
    getContentPane().add(jToggleButton6);
    jToggleButton6.setText("Yellow");
    jToggleButton6.setPreferredSize(new java.awt.Dimension(100, 100));
    jToggleButton6.addItemListener(ev -> updateColorIfSelected(jToggleButton6, ev, Color.YELLOW));

    jToggleButton7 = new JToggleButton();
    getContentPane().add(jToggleButton7);
    jToggleButton7.setText("Magenta");
    jToggleButton7.setPreferredSize(new java.awt.Dimension(100, 100));
    jToggleButton7.addItemListener(ev -> updateColorIfSelected(jToggleButton7, ev, Color.MAGENTA));

    jToggleButton8 = new JToggleButton();
    getContentPane().add(jToggleButton8);
    jToggleButton8.setText("Pink");
    jToggleButton8.setPreferredSize(new java.awt.Dimension(100, 100));
    jToggleButton8.addItemListener(ev -> updateColorIfSelected(jToggleButton8, ev, Color.PINK));

    jToggleButton9 = new JToggleButton();
    getContentPane().add(jToggleButton9);
    jToggleButton9.setText("White");
    jToggleButton9.setPreferredSize(new java.awt.Dimension(100, 100));
    jToggleButton9.addItemListener(ev -> updateColorIfSelected(jToggleButton9, ev, Color.WHITE));

    pack();
    this.setSize(350, 370);
    } catch (Exception e) {
    e.printStackTrace();
    }


    }

    }

    --- Update ---

    https://pastebin.com/7qhJ7GDs
    Heres the pastebin just in case its easier to read this way

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    258
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Buttons need to change colors individually

    You may try this
    UIManager.put("ToggleButton.select",color);
    SwingUtilities.updateComponentTreeUI(btn);
    Whatever you are, be a good one

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,381
    Thanks
    53
    Thanked 2,397 Times in 2,350 Posts

    Default Re: Buttons need to change colors individually

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: June 9th, 2014, 04:06 PM
  2. Java Colors
    By Ravekitty in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 11th, 2013, 08:29 PM
  3. how to change DB design for a change in java
    By harry7ster in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 7th, 2013, 12:57 PM
  4. [SOLVED] Random Colors Problem
    By captain in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 23rd, 2012, 09:40 PM
  5. Replies: 10
    Last Post: November 15th, 2010, 11:12 PM