Add a JChooserColor to a JMenuItem
Hi to all ! i am a newbie and i don't know a lot of things..i am making a programm..i have make JMenuBar(i am working with netbeans) and i have add some JmenuItems...i want to know how can add a JChooserColor to an item so the user can choose the color he wants for the background!!
I make an example-----> File | Edit | Options
-------------------------
-----------------------------------> ColorChoose--->?????
(ColorChoose is an item of Options )
where the ??? i want to put the JfileChooser class so the user can see the colors..i mean when he puts the mouse over the ColorChoose then opens a window to the right side with the background colors!
or i have to make another class and when he goes to ColorChoose opens the new interfacce and connect them so he can select the color?
i am so sorry about my english!! i am from greece so i don't speak very well english!! i hope that i have made to understand what i want!
Re: Add a JChooserColor to a JMenuItem
Hi,
your Objective is to bring out a color Palette when u click on a menu item, right!!!
This can be done in two steps :
1. Add Actionlistenerto the required MenuItem.
2.In the actionPerformed method, write the following code
Code:
Color newColor = JColorChooser.showDialog(
this, //refers the parent frame
"Choose Background Color", //Title of ColorChooser Dialog
Color.blue //Initial color selected in Chooser
);
the above code pops up the color chooser dialog and the selected color is in the newColor variable.
Re: Add a JChooserColor to a JMenuItem
well i i have try everything you said me and it is working..thank you so much!!
but now i have another problem...when i choose a color this doesn't set a color of background...what i have to do now? remains the default color !! of tha programm... i use (netbeans)
the code now is like this :
private void jMenuItem25_actionPerformed(java.awt.event.ActionEvent evt) {
Color newColor = JColorChooser.showDialog(
this, //refers the parent frame
"Choose Background Color", //Title of ColorChooser Dialog
Color.blue //Initial color selected in Chooser
);
}
i was thinking to put something like this: setBackground but i have tried it and doesn't works..maybe i am not setting well the command !! can you help plz?
and now another thing !!! if i make the JmenuItem25 like a Jmenu25 under the Jmenu Options
Options
jmenu25>....
and i want to open a window parent(not another window but opens in the right of jmenu25 with a color panel(or opens always JcolorChooser) it is difficult to make this?i have try it but nothing...
thank you for eveything..so kind!!! :) and i am so sorry if i am tiring!!!
1 Attachment(s)
Re: Add a JChooserColor to a JMenuItem
hi again!! well i have find one way to all this...i have created another class with the name Colori
and i have created into this the JcolorChooserDialog and 3 buttons!!!
then i went to the main class ( has the name HomeEstimo2) and i have
put this : Colori = new Estimo2.Colori();
and then under the JMenu3 ( has the name Colore Sfondo=in english means Color Backgorund) i have done this
jMenu3.add(Colori);
private Estimo2.Colori Colori;
i upload the image to see how works!! now my problem is how when i go to the button OK , i dont'know what command line i have to put for il background!! because the button "OK" is in the JPanel !!
it must be like this?
Color newColor;
newColor = JColorChooser.showDialog(((Component) e.getSource())
.getParent(), "Choose Background Color", Color.blue);
contentPane.setBackground(c);
i don't think so..but plzzz anyone who knows help me!!!
Re: Add a JChooserColor to a JMenuItem
Hey,
I'll come to ur doubt later.
I've a doubt in the last part of ur code.
The color returned by the ColorChooser is in variable 'newColor'.
But while setting the background color, u hav used another variable.
Is that OK?
Re: Add a JChooserColor to a JMenuItem
i am so sorry..you have right ! i have coppied not well !!
Color newColor;
newColor = JColorChooser.showDialog(((Component) e.getSource())
.getParent(), "Choose Background Color", Color.blue);
contentPane.setBackground(newColor);
like this i have put it into the programm!!