Results 1 to 6 of 6
  1. #1
    Beju is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default Adding component at runtime

    Well, maybe this is not directly related to applets, but maybe I'm doing something wrong or there's a different way to add components to applets at runtime. I have a simple Applet developed in NetBeans 6.5: there's one button, label, combobox and a panel. What I want to do is to add a (label/button/text field, etc....) to the panel when the user clicks the button. I tried:
    Java Code:
    panel.add(new JButton("alamakotka"));
    panel.revalidate();
    panel.repaint();
    but it doesn't work - the button is either not added or not visible.

    Strangely, I can add some of the existing components with success, but not newly created ones.
    Last edited by Beju; 06-15-2009 at 04:15 AM.

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    804
    Rep Power
    6

    Default

    Hi,
    I think in Swings u can't add the component in runtime.
    One thing u can do.Add the components and set the visibility to false.
    Then,after pressing the button u can set the visibility to true.
    Ramya:cool:

  3. #3
    Beju is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default

    You mean in general or just in applet? If in general, I think that's not true because The same code used on JFrame application works without problem.
    I thought about manipulating the component's visibility, but unfortunately in my case this is pointless.

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

    Default

    The layout manager of the "panel" is the key. The default layout manager for JPanel is FlowLayout which does not wrap to a new line when it has too many components to show. Try a different layout manager, eg, GridLayout.
    For more about layout managers see Lesson: Laying Out Components Within a Container.

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

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    I think in Swings u can't add the component in runtime.
    Sorry, but this is complete horse-**** -- you can add components at any time. Much depends (as mentioned by hardwire) on the layout managers used.
    Last edited by Fubarable; 06-16-2009 at 02:33 AM.

  6. #6
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    804
    Rep Power
    6

    Default

    hey,
    sorry, i misunderstood with your question and even answered wrongly. yes, we can add the components runtime. here is the code snippet that adds the component at runtime and removes it at runtime. i will add the reason how i misunderstood after this code piece. Compile & run this.

    ======================
    import javax.swing.*;
    import java.lang.*;
    import java.awt.event.*;

    public class GUITest extends JFrame{

    public JButton b1 = new JButton("Add");
    public JButton b2 = new JButton("Exit..");
    public JTextField t1 = new JTextField(15);
    public JPanel p1 = new JPanel();

    public void addComponentsAndListeners() {

    this.getContentPane().add(p1);
    p1.add(new JButton("Test.."));
    p1.add(b1);
    p1.add(b2);
    //p1.add(b1);
    pack();
    b1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("Add")) {
    p1.add(t1);
    System.out.println("Action ->" + e.getActionCommand());
    b1.setText("Remove");
    b1.setActionCommand("Remove");
    pack();
    }else if (e.getActionCommand().equals("Remove")) {
    p1.remove(t1);
    System.out.println("Action ->" + e.getActionCommand());
    b1.setText("Add");
    b1.setActionCommand("Add");
    pack();
    }
    validate();
    }
    });

    b2.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    System.exit(0);
    }
    });

    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });

    }

    public JPanel getPanel() {
    return p1;
    }

    public static void main(String[] args) {
    GUITest myFrame = new GUITest();
    myFrame.addComponentsAndListeners();
    myFrame.setVisible(true);
    }
    }

    ==============================
    Try doing this:

    1) Uncomment the 5th line in the "addComponentsAndListeners" method, compile and run the program. The 'b1' button you added in the 3rd line in this method is not visible now. This was the misunderstanding that lead to my first answer.

    2) I have a question: When you add a component to the panel like what you said "panel.add(new JButton("alamakotka"));", how will you add an actionListener to this button? I think you need to declare and define this button outside inorder to use this object to addActionListener even as an anonymous inner class.

    Regards
    Ramya
    Ramya:cool:

Similar Threads

  1. add component to component
    By Omarero in forum New To Java
    Replies: 7
    Last Post: 05-17-2009, 02:39 PM
  2. Manually adding a component
    By djc in forum NetBeans
    Replies: 3
    Last Post: 04-01-2009, 11:51 AM
  3. Adding file contents to Choice component
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-07-2008, 09:06 AM
  4. Same component on all JTabbedPane
    By java_novice in forum AWT / Swing
    Replies: 4
    Last Post: 08-06-2007, 09:09 AM
  5. Adding JARs to the classpath at runtime?
    By johann_p in forum Advanced Java
    Replies: 1
    Last Post: 06-26-2007, 10:42 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
  •