Results 1 to 6 of 6
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,037
    Rep Power
    8

    Default 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);
    }

  2. #2
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,037
    Rep Power
    8

    Default

    okay i found the answer myself
    it is called a JRadioButtonMenuItem
    thanks to myself
    willemjav

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    To get the ItemListener to work try:
    Java Code:
    if(e.getStateChange() == ItemEvent.SELECTED) {
        // your code here...
    }

  4. #4
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,037
    Rep Power
    8

    Default

    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

  5. #5
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,037
    Rep Power
    8

    Default

    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;
    }
    }

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    // This is specifying a field "SELECTED" in the "durationField"
    // class instance.
    durationField.SELECTED
    // This is specifying a field "SELECTED" in the ItemEvent class.
    // See Field Summary section in ItemEvent api.
    ItemEvent.SELECTED

Similar Threads

  1. Replies: 3
    Last Post: 01-05-2008, 11:13 PM
  2. JMenu calling another Forms/Panels
    By plodos in forum New To Java
    Replies: 2
    Last Post: 12-10-2007, 08:02 AM
  3. JMenu and JRadioButtonMenuItem
    By doron70 in forum AWT / Swing
    Replies: 3
    Last Post: 07-18-2007, 06:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •