Swing Busy Waiting Problem (JComboBox)

  • Themenstarter Themenstarter majin159
  • Beginndatum Beginndatum
M

majin159

Gast
Hallo zusammen,

ich möchte mit dem Ergebnis der JComboBox in meinem Programm weiter arbeiten.
Also hab ich erstmal simpel darauf gewartet, aber folgendes passiert:

Java:
	public static void main(String[] args) {
		CustomComboBoxDemo asd = new CustomComboBoxDemo();
		asd.createAndShowGUI();
		while(true) {
			System.out.println(asd.a); //Sobald ich diese Zeile auskommentiere, ist es eine Endlosschleife
			if(asd.a != 99) {
				break;
			}
		}
		System.out.println("#" + asd.getA());
	}

Warum ist das so?
 
ui - while(true) schaut schonmal gar nicht gut aus....

ansonsten ist es schwer zu sagen was [c]asd.a[/c] macht bzw dann [c]asd.getA()[/c] ohne code
 
Ich kann die Bedingung auch in die Schleife schreiben, das ist ja nur ein Beispiel.
und asd.a und asd.getA() tun das gleiche, sie holen sich einfach das selected item nachdem
eins ausgewählt wurde. Wobei ich getA() mitlerweile entfernt habe.


Java:
package graphiti.help;

public class CustomComboBoxDemo extends JPanel {
    ImageIcon[] images;
    static String[] petStrings = {"dog", "cat", "pig", "bird"};
    static JComboBox petList;
    public int a = 99;

	/*
     * Despite its use of EmptyBorder, this panel makes a fine content
     * pane because the empty border just increases the panel's size
     * and is "painted" on top of the panel's normal background.  In
     * other words, the JPanel fills its entire background if it's
     * opaque (which it is by default); adding a border doesn't change
     * that.
     */
    public CustomComboBoxDemo() {
        super(new BorderLayout());

        //Load the images and create an array of indexes.
        images = new ImageIcon[petStrings.length];
        Integer[] intArray = new Integer[petStrings.length];
        for (int i = 0; i < petStrings.length; i++) {
            intArray[i] = new Integer(i);
            images[i] = createImageIcon("images/" + petStrings[i] + ".gif");
            if (images[i] != null) {
                images[i].setDescription(petStrings[i]);
            }
        }

        //Create the combo box.
        petList = new JComboBox(intArray);
        ComboBoxRenderer renderer = new ComboBoxRenderer();
        renderer.setPreferredSize(new Dimension(200, 130));
        petList.setRenderer(renderer);
        petList.setMaximumRowCount(3);

        //Lay out the demo.
        add(petList, BorderLayout.PAGE_START);
        setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
        
    }

    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = CustomComboBoxDemo.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
                return null;
        }
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    public void createAndShowGUI() {
        //Create and set up the window.
        final JFrame frame = new JFrame("CustomComboBoxDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton jButton1 = new JButton("Button");

        //Create and set up the content pane.
        JComponent newContentPane = new CustomComboBoxDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);
        final JList jList1 = new JList(petStrings);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              Object contents = petList.getSelectedItem();
              a = petList.getSelectedIndex();
              System.out.println(contents + " " + a);
              frame.setVisible(false);
              frame.dispose();
//              WindowEvent closingEvent = new WindowEvent(frame, WindowEvent.WINDOW_CLOSING);
//              Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(closingEvent);
            }
          });
        frame.add(jButton1,"South");
        
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    class ComboBoxRenderer extends JLabel implements ListCellRenderer {
        private Font uhOhFont;

        public ComboBoxRenderer() {
            setOpaque(true);
            setHorizontalAlignment(CENTER);
            setVerticalAlignment(CENTER);
        }

        /*
         * This method finds the image and text corresponding
         * to the selected value and returns the label, set up
         * to display the text and image.
         */
        public Component getListCellRendererComponent(
                                           JList list,
                                           Object value,
                                           int index,
                                           boolean isSelected,
                                           boolean cellHasFocus) {
            //Get the selected index. (The index param isn't
            //always valid, so just use the value.)
            int selectedIndex = ((Integer)value).intValue();

            if (isSelected) {
                setBackground(list.getSelectionBackground());
                setForeground(list.getSelectionForeground());
            } else {
                setBackground(list.getBackground());
                setForeground(list.getForeground());
            }

            //Set the icon and text.  If icon was null, say so.
            ImageIcon icon = images[selectedIndex];
            String pet = petStrings[selectedIndex];
            setIcon(icon);
            if (icon != null) {
                setText(pet);
                setFont(list.getFont());
            } else {
                setUhOhText(pet + " (no image available)",
                            list.getFont());
            }

            return this;
        }

        //Set the font and text when no image was found.
        protected void setUhOhText(String uhOhText, Font normalFont) {
            if (uhOhFont == null) { //lazily create this font
                uhOhFont = normalFont.deriveFont(Font.ITALIC);
            }
            setFont(uhOhFont);
            setText(uhOhText);
        }
    }
}
 
Dafür muss man die Threads mit wait/notifyAll oder den java.util.concurrent-Klassen synchronisieren. Wichtig könnte sein, welcher Thread denn dort auf das Ergebnis der ComboBox-Auswahl warten soll...?!
 
Dafür muss man die Threads mit wait/notifyAll oder den java.util.concurrent-Klassen synchronisieren. Wichtig könnte sein, welcher Thread denn dort auf das Ergebnis der ComboBox-Auswahl warten soll...?!

?
Die Schleife wartet und ich wills einfach ausgeben, wenn ich das kann, kann ich alles andere ja auch...

warum nicht einfach mit einem listener arbeiten?

tu ich ja, aber ich frag mich halt, warum der nicht merkt das sich a geändert hat.
Das merkt er nur, wenn ich in der Schleife _irgendetwas_ ausgebe...
 
In welchem Thread läuft die Schleife, und warum läuft die Schleife (wenn das, was dort gemacht wird, auch vom Listener gemacht werden könnte)
 
?
Mein erster Post, mehr existiert nicht. Die Schleife ist mein busy waiting, wie soll ich da n Listener
implementieren?
 
Habs gefixed, bin aber ehrlich ich weiß nicht genau warum es funktioniert.
Vllt kann mir das jemand erklären.
Marco hatte recht, ich habe mir hier ja n MultiThreading erzeugt, also muss ich die Zugriffe
auf die Variable synchronisieren, daher hab ich nun


Java:
    private synchronized void changeIndex(int selectedIndex) {
		this.a = selectedIndex;
	}
    
    public synchronized int getSelectedIndex() {
    	return this.a;
    }

in der ComboBox hinzugefügt und in der anderen Klasse frag ich dann die Methode ab.
Aber warum funktioniert das nun?
 
Hab die Antwort:

The most likely explanation is that the variable is only read once, turning the while into an infinite loop (or a no-op). Since you haven't declared test as volatile, the compiler is allowed to perform such an optimization.

Once you call an external function from within the loop, the compiler can no longer prove that test remains invariant across loop iterations, and doesn't perform the optimization.
 
Schau:
Java:
comboBox.addItemListener(new ItemListener() 
               {
			public void itemStateChanged(ItemEvent e) 
			{
				if(e.getStateChange()==ItemEvent.SELECTED)
				{
					System.out.println(e.getItem());
				}
			}
		});
 

Zurück
Oben