Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How to remove object from jframe as part of actionPerformed(ActionEvent e)??

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to remove object from jframe as part of actionPerformed(ActionEvent e)??

    I currently have a program written that outputs a canvas object and adds a picture of 5 taxis to it.

    I have now added a jtextfield so that a user can add an interger. Ideally if the user was to enter the number 8, there would be 8 taxis added to the canvas.

    I am having trouble with the final part i mentioned... im not sure how to delete the old canvas and output a new one with the amount that the user wrote in the jtextfield.

    Could you please tell me where i am going wrong on my code and offer a solution, any code you can include would be a great help.

    thanks alot

    ps. i am very new to this so please be nice



     
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Random;
    import javax.swing.*;
     
     
    public class TaxiFrame extends JFrame implements ActionListener {
     
     
          private JLabel L1 = new JLabel("Number of Taxis:");
          private JLabel L2 = new JLabel("Type an interger and press enter");
          private JTextField t1 = new JTextField ("            ");
     
     
        public TaxiFrame() {
            super("This is the Frame");
            setSize(600, 400);
            getContentPane().setBackground(Color.CYAN);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setLayout(new BorderLayout(10, 10));
     
            Random rx = new Random();
            Random ry = new Random();
     
            for(int i = 0; i < 5; i ++)
     
            {
                TaxiCanvas tax = new TaxiCanvas();
                tax.setBounds(rx.nextInt(600 - 100), ry.nextInt(400 - 100), 100, 100);
                add(tax);
     
            }
     
     
            JPanel p = new JPanel();     
     
     
             p.setOpaque(false);
             p.add(L1);
             getContentPane().
             add("South", p);
     
              p.setOpaque(false);
              p.add(t1);
              getContentPane().
              add("South", p);
     
              p.setOpaque(false);
              p.add(L2);
              getContentPane().
              add("South", p);
     
            setVisible(true);
     
            t1.addActionListener(this);
     
     
     
        }
     
        public static void main(String[] args) {
            new TaxiFrame();
        }
     
     
       public void actionPerformed(ActionEvent e)
        {
     
            if (e.getSource() == t1)
            {
                if(Integer.parseInt(t1.getText()) > 0)
                {
                        getContentPane().removeAll();
                      TaxiCanvas tax = new TaxiCanvas();           
                   add(tax);
                }
            }
     
        }
     
    }
    Last edited by benread89; March 11th, 2014 at 12:37 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to remove object from jframe as part of actionPerformed(ActionEvent e)??

    When posting code, please use the highlight tags to preserve formatting.

    If you're adding or removing components from a visible container, you have to *revalidate* that container afterwards to tell the GUI to update itself.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to remove object from jframe as part of actionPerformed(ActionEvent e)??

    ahh ok sorry i am new here, how do i add hightlight tags, i can then edit the post.

    Could you expand on how to revalidate please im not sure how that works.

    thanks

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to remove object from jframe as part of actionPerformed(ActionEvent e)??


    [highlight=java]
    //code goes here
    [/highlight]


    A google search of something like "Java Swing revalidate" will be much more detailed than I can be, but the gist is this: a visible component isn't smart enough to redo its layout when you add or remove components from it. You can use the revalidate() function to tell it to redo its layout, and the repaint() function to tell it to redraw itself.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. How do I select an object in a JFrame using the mouse?
    By Tr1N1tro in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 6th, 2013, 09:55 PM
  2. Replies: 5
    Last Post: February 15th, 2013, 05:01 PM
  3. [SOLVED] Graphics Object Won't Draw To The Correct JFrame
    By NickNumero in forum AWT / Java Swing
    Replies: 7
    Last Post: October 27th, 2012, 01:07 PM
  4. How to display a Rectangle object in JFrame (Problem).
    By voltaire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 15th, 2012, 07:22 PM
  5. Replies: 3
    Last Post: March 28th, 2012, 08:47 AM

Tags for this Thread