1

Basically I have a program with cards(I'm using the CardLayout), when the user types a sentance or what ever they type I would like that to be added to a label on the following page when they hit a button named create. I'm not sure how i would get the entered field to be saved and placed as a variable into the label. any ideas? I can provide my code if necessary.

createButton2.addActionListener(new ActionListener() {   //Back button listener, switches back to ADMIN fixtures panel
            @Override
            public void actionPerformed(ActionEvent e) {
                cardLayout.show(container, "6");
                String theText = descriptionField.getText();
                fixtureDescLabel.setText( theText );
                fixtureDescLabel.setBounds(250, 150, 200, 40);
                add(fixtureDescLabel);
            }
        });
mKorbel
  • 109,525
  • 20
  • 134
  • 319

1 Answers1

2

It's pretty straight forward.

Take the text from the textArea:

String theText = myTextArea.getText();

Put in a label:

myLabel.setText( theText );

In a button listener:

myButton.addActionListener( new ActionListener() {
    @override public actionPerformed( ActionEvent event )
    {
        String theText = myTextArea.getText();
        myLabel.setText( theText );
    }
} );

EDIT

Looking at your edit, your problem is that you add a component to your frame without revalidating (JFrame#revalidate()) it.

MByD
  • 135,866
  • 28
  • 264
  • 277
  • I added it but it isn't showing up? Added code to main question# –  Feb 17 '14 at 20:56
  • why do you add the jlabel again? – MByD Feb 17 '14 at 20:59
  • I applied the string to the label and tthen add that label to the position `fixtureDescLabel.setBounds(250, 150, 200, 40);` and then I displayed it by using `add(fixtureDescLabel);` –  Feb 17 '14 at 21:00
  • So your problem is different. After adding components to the frame you need to call JFrame#revalidate. otherwise it will not be updated.. – MByD Feb 17 '14 at 21:02
  • I'm not sure you understand me. Basically I have to screens, one containing a JTextArea and a button, and the other containing a label. I want the user to enter something into the TextArea and click the button. When the user clicks the button I would like it to add the entered text onto the label. –  Feb 17 '14 at 21:05
  • In this case, why do you re-add the label? – MByD Feb 17 '14 at 21:06
  • It's fine no problem. I fixed it I was adding the component in the wrong place. Many thanks! –  Feb 17 '14 at 21:10