Results 1 to 6 of 6
Thread: mirrors in mirrors in JMenu
- 06-12-2008, 11:13 PM #1
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 587
- Rep Power
- 6
mirrors in mirrors in JMenu
Imagine there is a dropdown menu with several color items.
(black, white, red, blue, green and yellow)
Just one color should appear selected (no-color-selected is not a option.
I taught the next code would do it!
(but I believe there is some funny backfiring loop because resetColormenu()
will call itemStateChanged(ItemEvent evt) etc.)
There should be an other way without back looping?
public void itemStateChanged(ItemEvent evt) {
Object source = evt.getSource();
if ( blacktxt.isSelected() ) {
resetColormenu();
blacktxt.setSelected(true);
img[index].infotextcolor = "black";
}
else
if ( whitetxt.isSelected() ) {
resetColormenu();
whitetxt.setSelected(true);
img[index].infotextcolor = "white";
}
else
if ( bluetxt.isSelected() ) {
resetColormenu();
bluetxt.setSelected(true);
img[index].infotextcolor = "blue";
}
else
if ( redtxt.isSelected() ) {
resetColormenu();
redtxt.setSelected(true);
img[index].infotextcolor = "red";
}
else
if ( yellowtxt.isSelected() ) {
resetColormenu();
yellowtxt.setSelected(true);
img[index].infotextcolor = "yellow";
}
else
if ( greentxt.isSelected() ) {
resetColormenu();
greentxt.setSelected(true);
img[index].infotextcolor = "green";
}
}
private void resetColormenu() {
blacktxt.setSelected(false);
whitetxt.setSelected(false);
bluetxt.setSelected(false);
redtxt.setSelected(false);
yellowtxt.setSelected(false);
greentxt.setSelected(false);
}
- 06-12-2008, 11:36 PM #2
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 587
- Rep Power
- 6
okay i found the answer myself
it is called a JRadioButtonMenuItem
thanks to myself
willemjav
- 06-14-2008, 07:04 AM #3
To get the ItemListener to work try:
Java Code:if(e.getStateChange() == ItemEvent.SELECTED) { // your code here... }
- 06-14-2008, 08:40 AM #4
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 587
- Rep Power
- 6
I actually used this (probably your code is better)
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
//if (durationField==source) { // did not work
String str = durationField.getText(); // read out the duration field fields
img[index].duration = Integer.parseInt(str);
if ( blacktxt.isSelected()) {
img[index].infotextcolor = "black";
}
else
if ( whitetxt.isSelected()) {
img[index].infotextcolor = "white";
}
else
etc
- 06-14-2008, 11:53 AM #5
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 587
- Rep Power
- 6
Hardwired, what´s wrong here (cannot find var. SELECTED)?
public void itemStateChanged(ItemEvent evt) {
int x=0;
String str;
if(evt.getStateChange() == durationField.SELECTED) {
do {
str = durationField.getText();
try {
x = Integer.parseInt(str);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(infoPane,"please type number within range 1-360 + return");
ex.printStackTrace();
}
if (x<1 || x>360) JOptionPane.showMessageDialog(infoPane,"please type number within range 1-360 + return");
} while (x<1 || x>360);
img[index].duration = x;
}
}
- 06-14-2008, 06:59 PM #6
Similar Threads
-
Selecting a JMenu paints over the JPanel on the content pane
By Swingset in forum AWT / SwingReplies: 3Last Post: 01-05-2008, 11:13 PM -
JMenu calling another Forms/Panels
By plodos in forum New To JavaReplies: 2Last Post: 12-10-2007, 08:02 AM -
JMenu and JRadioButtonMenuItem
By doron70 in forum AWT / SwingReplies: 3Last Post: 07-18-2007, 06:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks