Link to home
Start Free TrialLog in
Avatar of sydpc
sydpc

asked on

Please help - how to set the background color without closing down the JColorChooser box?

i all,

I am a beginner studying Java and have a question about stateChanged method in the JColorChooser class.

The program consists four different areas and if a user rightclick on an area, a color chooser dialog
is then displayed. The background color of current working area should be changed immediately once
a different color is chosen. However, my program does not change the background color without closing down the color
dialog.

I was trying to find the answer on the Internet for 1 day, but could not find the answer.
I would really appreciate if anyone can give me any help on how to solve this problem.

Please see the code below for details.

Richard


/*
 * SwingPopupMenu.java
 * SwingPopupMenu - Class to display/use a popup menu
 * Right click on each area to change its background color
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.colorchooser.*;
import javax.swing.event.* ;

public class mycolorpopup extends JFrame implements ActionListener,
MouseListener,  ChangeListener{
    private static String appTitle="Swing Popup Menu";
    private Component curComponent;

    // Frame components

    private JPanel mainPanel, subPanel;
    private JTextArea text1Area, text2Area, text3Area, text4Area;
    private JButton quitButton;

    private JColorChooser cchoice;
    private ColorSelectionModel cmodel ;

    // Menu and components
    private JPopupMenu backgroundMenu;


    /** Creates a new instance of SwingPopupMenu */
    public mycolorpopup() {
        super(appTitle);
        addWindowListener(new WindowHandler());

        // set up the popup menu
        backgroundMenu=new JPopupMenu();

        // set up the other components

        mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());

        subPanel = new JPanel();
        subPanel.setLayout(new GridLayout(2,2,3,3));

        text1Area = new JTextArea("Area 1", 8, 12);
        text2Area = new JTextArea("Area 2", 8, 12);
        text3Area = new JTextArea("Area 3", 8, 12);
        text4Area = new JTextArea("Area 4", 8, 12);

         cchoice = new JColorChooser() ;

        subPanel.add(text1Area);
        subPanel.add(text2Area);
        subPanel.add(text3Area);
        subPanel.add(text4Area);

        quitButton = new JButton("Quit");
        mainPanel.add(subPanel,BorderLayout.CENTER);
        mainPanel.add(quitButton, BorderLayout.SOUTH);

        // add all the listeners

        quitButton.addActionListener(this);

        // change listener (notice it's fired by a selection model object, not the chooser)
        cchoice = new JColorChooser() ;

        cmodel = cchoice.getSelectionModel() ;
        cmodel.addChangeListener(this) ;

        // these components will respond to a mouse click

        text1Area.addMouseListener(this);
        text2Area.addMouseListener(this);
        text3Area.addMouseListener(this);
        text4Area.addMouseListener(this);
        quitButton.addMouseListener(this);

        // display the frame
        getContentPane().add(mainPanel);
        pack();
        setVisible(true);
    } // end constructor

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        mycolorpopup s = new mycolorpopup();
    } // end main


    public void mouseClicked(java.awt.event.MouseEvent mouseEvent) {
    }

    public void mouseEntered(java.awt.event.MouseEvent mouseEvent) {
        System.out.println("Mouse entered component");
    }

    public void mouseExited(java.awt.event.MouseEvent mouseEvent) {
        System.out.println("Mouse exited component");
    }

    public void mousePressed(java.awt.event.MouseEvent mouseEvent) {
    }

    public void mouseReleased(MouseEvent mEvent) {
        if (mEvent.isPopupTrigger()) {
            curComponent=mEvent.getComponent();
            Color c = cchoice.showDialog(null, "Select a color",Color.blue);
            curComponent.setBackground(c);

        }
    }

    // Check the event source and process
    public void actionPerformed(java.awt.event.ActionEvent ae) {
        if (ae.getSource() == quitButton)
            System.exit(0);

    } // end actionperformed


    public void stateChanged(javax.swing.event.ChangeEvent e) {
        // if stt is not really needed. The color model is the only
component in this example
        System.out.println("In the stateChanged event");
        if (e.getSource() == cmodel) {
            Color newcolour = cchoice.getColor() ;
            curComponent.setBackground(newcolour) ;
        } // end if
    }

} // end class
Avatar of tomboshell
tomboshell

That is specifically what is coded (by defalut).  The dialog returns the value when it is closed.

or as specifically stated in the JavaDocs : "Shows a modal color-chooser dialog and blocks until the dialog is hidden. If the user presses the "OK" button, then this method hides/disposes the dialog and returns the selected color. If the user presses the "Cancel" button or closes the dialog without pressing "OK", then this method hides/disposes the dialog and returns null."

The best idea would be to use the "createDialog" method which will return a JDialog.  You will need to create your own listeners to give to the constructor instead of using the default actions like above.  

Tom
Hi.
The problem is because the Swing is single threaded, which means that the method that will paint the current working area will be invoked only after closing the dialog.
To solve this problem, your application should be multithreaded.
You should found in the Java Documention site some examples how to solve this kind of questions.
Avatar of sydpc

ASKER

Thank you very much for your help.

Due to my limited knowledge of Java, could anyone please give me a working solution to this problem so that I can study?

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sydpc:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

-- Points for objects

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TimYates
EE Cleanup Volunteer