Results 1 to 4 of 4
  1. #1
    sublixt's Avatar
    sublixt is offline Member
    Join Date
    Jul 2011
    Location
    somewhere
    Posts
    37
    Rep Power
    0

    Default Destroy jtextarea and jbutton.

    I am trying to destroy a jtextarea and jbutton and then remake them.
    Java Code:
        static JFrame frame = new JFrame("Stars");
        JScrollPane scrollPane;
        JTextArea textArea = new JTextArea(30, 70);
        JButton button;
        GridBagConstraints g = new GridBagConstraints();
        HandelerClass hc = new HandelerClass();
        public GUI() {
            super(new GridBagLayout());
            textArea.setEditable(false);
            button = new JButton("More Stars?");
            scrollPane = new JScrollPane(textArea);
            g.fill = GridBagConstraints.BOTH;
            g.weightx = 1.0;
            g.weighty = 1.0;
           	add(scrollPane, g);
            add(button, g);
            button.addActionListener(hc);
            bla = starNum();
            for(int fs = 0; fs < bla.length; fs++){
            	textArea.append(bla[fs] + "\n");
            }
        }
        private static void create() {
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new GUI());
            frame.pack();
            frame.setVisible(true);
        }
        public static void main(String[] args) {
        	create();
        }
        public class HandelerClass implements ActionListener {
    		public void actionPerformed(ActionEvent arg0) {
                            remove(textArea);
                            remove(button);
    			pannel.dispose();
                            create();
    		} 
        }
    I think that is all the code that is needed, it works fine in the sense that everything vanishes and the program reruns but the old textarea and button are on top of the new one. How can i completely get rid of those textareas and buttons.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,814
    Rep Power
    25

    Default

    Where are the new versions of those components that are to replace the old ones?
    Why do you think the old ones have not been replaced?

    There is a problem in your code creating GUI components from an action listener. You should start a new thread to do that and exit the actionlisteners thread.

  3. #3
    sublixt's Avatar
    sublixt is offline Member
    Join Date
    Jul 2011
    Location
    somewhere
    Posts
    37
    Rep Power
    0

    Default

    1) I think I see what you mean since the old component still has it's data when I removed it I just took it off the screen instead of removing the data from it.
    2) because the old data is still there adding new data just creates error and displaying both data instead of just the newest one.

    How what do you mean exit the actionlistener thread (just learning about threads, only know the basics).

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    There is a problem in your code creating GUI components from an action listener. You should start a new thread to do that and exit the actionlisteners thread.
    Um, no. Listener event code runs on the EDT.

    @sublixt: What's your pannel variable, that you call dispose() on? Why do you add a new GUI() to the frame every time the button is clicked? Do you realize that the default BorderLayout adds these to the CENTER region and manages the size of only the last one added?

    To get better help sooner, post a SSCCE that clearly demonstrates your problem.

    db

Similar Threads

  1. How to destroy a Process in windows?
    By hahanizhu in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-26-2011, 11:19 AM
  2. Jbutton to destroy process
    By peterhammond in forum AWT / Swing
    Replies: 1
    Last Post: 04-19-2011, 07:58 PM
  3. JFrame+JPanel+JButton+JtextArea+ImageIcon
    By zusss in forum AWT / Swing
    Replies: 11
    Last Post: 02-18-2011, 11:40 AM
  4. Destroy object
    By anjanesh in forum New To Java
    Replies: 11
    Last Post: 01-02-2010, 08:25 PM
  5. Replies: 1
    Last Post: 08-08-2007, 02:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •