JLabel mit Hilfe von JTextfield benennen

Java Neuling

Mitglied
Hallo!
Ich möchte gerne ein JLabel mit Hilfe eines JTextfields benennen.
Ich habe schon einen kleinen Versuch gestartet, aber es tritt das Problem auf, dass wenn man in das Textfield zweimal etwas eingibt, automtisch zwei Labels erzeugt werden, welche sich überlagern.
Ich möchte aber, dass dann das alte wieder gelöscht wird. Wie muss ich das dann schreiben?
Danke im Vorraus! 😉

Java:
		public void createPage4()
		{
			panel4 = new JPanel();
			panel4.setLayout(null);

			NS1 = new JTextField(20);
			NS1.setBounds( 10, 70, 150, 20 );
			NS1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e ){
					JLabel label1 = new JLabel( NS1.getText() );
					label1.setBounds( 10, 10, 150, 20 );
					panel1.add( label1 );
				}
			
				});
			panel4.add( NS1 );
 
das new in Zeile 10 erzeugt dir jedesmal ein neues Label .. das musst du auslagern, und lediglich auf die ausgelagerte Variable im ActionListener zugreifen.

Zeile 11 uns 12 ebenfalls auslagern, das musst ja dann nur noch 1x machen. Im ActionListener käme lediglich ein lable1.setText(..) rein .. soweit meine Theorie 🙂
 
Java:
		public void createPage4()
		{
			panel4 = new JPanel();
			panel4.setLayout(null);
                        JLabel lbl1=new JLabel()
			NS1 = new JTextField(20);
			NS1.setBounds( 10, 70, 150, 20 );
			NS1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e ){
					lbl1.setText(NS1.getText());
					lbl1.setBounds( 10, 10, 150, 20 );
					panel1.add( lbl1);
				}
			
				});
			panel4.add( NS1 );
[/QUOTE]
 
Zuletzt bearbeitet:
Danke für den Tipp.
Ich habe es jetzt so umgeschrieben.
Aber jetzt bekomme ich sehr viele Errors ???:L :

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at TabbedPaneExample$1.actionPerformed(GUI.java:137)
at javax.swing.JTextField.fireActionPerformed(Unknown Source)
at javax.swing.JTextField.postActionEvent(Unknown Source)
at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(Unknown Source)
at javax.swing.JComponent.processKeyBindings(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
 
den befehl in zeile 11 und 12 auch auslagern .. dann sollte das passen oder?

in dem fall zeige mal die komplette klasse wenn sie nich zu gross sein sollte
 
Mit der Variante von JBuntu bekomme ich folgende Errors:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Cannot refer to a non-final variable lbl1 inside an inner class defined in a different method
Cannot refer to a non-final variable lbl1 inside an inner class defined in a different method
Cannot refer to a non-final variable lbl1 inside an inner class defined in a different method
 
Java:
public void createPage4() {
    panel4 = new JPanel();
    panel4.setLayout(null);
    final JLabel lbl1=new JLabel()
    lbl1.setBounds( 10, 10, 150, 20 );
    NS1 = new JTextField(20);
    NS1.setBounds( 10, 70, 150, 20 );
    NS1.addActionListener(new ActionListener() {        
        public void actionPerformed(ActionEvent e ){
            lbl1.setText(NS1.getText());
        }
            
    });
    panel1.add( lbl1);
    panel4.add( NS1 );
}

eventuell musst du NS1 auch final machen
 

Zurück
Oben