Results 1 to 2 of 2
  1. #1
    cazmo is offline Member
    Join Date
    Feb 2010
    Posts
    1
    Rep Power
    0

    Default getContentPane update from actionPerformed method

    Hi,
    I am trying to add a new JLabel on a new line of a JApplet from an ActionListener. Below is my code. Rather than using the setText method inside the actionPerformed I want to add a new label. How can I do that?

    I've tried using the contentPane.add(label) inside the actionPerformed method but the applet shows blank. Which forces me to have to create the JLabel from within the TesterApplet and use the setText from within the actionPerformed.


    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.Container;

    public class TesterApplet extends JApplet implements ActionListener {

    private JLabel lbl1;
    private JLabel lbl2;
    private JTextField inputLine;

    Container contentPane = getContentPane();

    public TesterApplet()
    {
    contentPane.setLayout(new FlowLayout(FlowLayout.LEFT));


    lbl1 = new JLabel("Greetings ");
    lbl2 = new JLabel();

    inputLine = new JTextField(30);

    contentPane.add(lbl1);
    contentPane.add(inputLine);
    contentPane.add(lbl2);

    inputLine.addActionListener(this);

    }

    public void actionPerformed(ActionEvent event)
    {
    String st = inputLine.getText();
    lbl2.setText(st);
    }

    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It works for me. You're probably not calling revalidate on the contentPane:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class TesterApplet extends JApplet implements ActionListener {
    
      private JLabel lbl1;
      private JLabel lbl2;
      private JTextField inputLine;
    
      // contentPane here is a JPanel.  If you cast it as such, 
      // you can call revalidate easily on it.
      JPanel contentPane = (JPanel) getContentPane();
    
      public TesterApplet() {
        contentPane.setLayout(new FlowLayout(FlowLayout.LEFT));
    
        lbl1 = new JLabel("Greetings ");
        lbl2 = new JLabel();
    
        inputLine = new JTextField(30);
    
        contentPane.add(lbl1);
        contentPane.add(inputLine);
        contentPane.add(lbl2);
    
        inputLine.addActionListener(this);
    
      }
    
      public void actionPerformed(ActionEvent event) {
        String st = inputLine.getText();
        contentPane.add(new JLabel(st));
        contentPane.revalidate();
        //lbl2.setText(st);
      }
    
    }
    Oh, and welcome to the forum!

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2010, 05:12 PM
  2. Replies: 2
    Last Post: 04-03-2009, 05:04 AM
  3. Help with actionPerformed
    By mayhewj7 in forum New To Java
    Replies: 8
    Last Post: 02-10-2009, 06:45 PM
  4. trouble with actionPerformed
    By diggitydoggz in forum New To Java
    Replies: 2
    Last Post: 12-26-2008, 02:18 AM
  5. actionPerformed problem
    By tomitzel in forum New To Java
    Replies: 1
    Last Post: 01-08-2008, 06:10 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
  •