NEWBIE ALERT: Problem with Visibility problem on my GUI


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: NEWBIE ALERT: Problem with Visibility problem on my GUI

  1. #1
    Join Date
    Nov 2002
    Posts
    1

    NEWBIE ALERT: Problem with Visibility problem on my GUI

    I'm just going through a small book on Java programming and the goal is to press a button to show a shape (ie: circle) and then to press a button to hide that same shape.

    My idea was to simply add it in but change the visibility to true and false. Now, I get it to show perfectly but I can't get it to hide (ie: setVisiblity to false) for some reason. The problem lies in the actionPerformed method of the CommandMain class. I'm at a loss as to why I can't simply change the visibility of the object to true and false.

    I've include the relevant code below
    Thanks for all your help in advance.


    ================= COMMANDMAIN.JAVA =================
    public class CommandMain extends JFrame implements ActionListener
    { JButton circleButton = new JButton("Make Circle");
    JButton squareButton = new JButton("Make Square");
    JButton undoButton = new JButton("Undo Last");
    JButton exitButton = new JButton("Exit");
    private static Vector objectList = new Vector(); // Stores objects in order
    JPanel container = new JPanel();
    Button newCircle; // Used to add a Circle object to the vector
    Button newSquare;
    Button newShape;
    Button objectToHide;

    // CONSTRUCTOR
    // PURPOSE: Used to set up all the buttons on our Panel
    public CommandMain()
    { setSize(400,400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Add interactive ability to buttons
    circleButton.addActionListener(this);
    squareButton.addActionListener(this);
    undoButton.addActionListener(this);
    exitButton.addActionListener(this);

    // Add them so we can see them on the panel
    this.container.add(circleButton);
    this.container.add(squareButton);
    this.container.add(undoButton);
    this.container.add(exitButton);
    setContentPane(container);
    setVisible(true);
    }

    // PURPOSE: Used to create an instance of itself so we can see the frame
    public static void main(String args[]) throws IOException
    { CommandMain window = new CommandMain(); }

    // PURPOSE: Used to perform actions based on the button picked by the user
    public void actionPerformed(ActionEvent picked)
    { Object choice = picked.getSource();
    if(choice == circleButton) // CIRCLE WAS SELECTED
    { newShape = new Button("Circle");
    Button newCircle = newShape.getShape();
    this.container.add(newCircle); // Add it to my container
    newShape.execute();
    objectList.add(newShape); // Add the object to my Vector
    setContentPane(container);
    repaint(); // Redraw the GUI because it changed
    }
    else if(choice == undoButton) // UNDO WAS SELECTED
    { if(objectList.size() > 0) // Check to ensure we don't overstep the boundaries
    { System.out.println("UNDO Pressed");
    objectToHide = (Button)objectList.elementAt(objectList.size()-1);
    objectToHide.hideShape(); // <-- PROBLEM: NOT HIDING MY SHAPE

    objectList.removeElementAt(objectList.size()-1);
    setContentPane(container);
    repaint();
    } // End if(objectList.size() > 0)
    } // End undoButton
    else if(choice == exitButton) // EXIT WAS SELECTED
    { System.out.println("Goodbye: Application exiting");
    dispose();
    System.exit(0);
    }
    else // No idea what button was selected
    { System.out.println("ERROR: Invalid option"); }
    }
    }

    ========================== BUTTON.JAVA ==================
    import java.awt.*;
    import javax.swing.*;

    public class Button extends JPanel
    { String objectShape;

    Button()
    { }

    Button(String shape)
    { this.objectShape = shape; }

    public void execute()
    { setVisible(true); }

    public void hideShape()
    { System.out.println("hideShape called");
    super.setVisible(false);
    }

    public Button getShape()
    { if(this.objectShape.equals("Circle"))
    { System.out.println("Making a circle");
    Circle newCircle = new Circle();
    return newCircle;
    }

    else if(this.objectShape.equals("Square"))
    { System.out.println("Making a square");
    Square newSquare = new Square();
    return newSquare;
    }
    return null;
    }
    }

  2. #2
    Join Date
    Nov 2002
    Posts
    8
    Hi there,
    I'm fairly new in the Java realm but I think I can help out. A simple solution to try, redraw the shape in the same color as your background. It technically doesn't hide the shape, but it shouldn't be visible either.
    As for why you can't set the visibilty to false, no clue.

    A good book with tons of stuff on Java swing is
    JAVA 2 Black Book:Comprehensive Problem Solver
    author: Steven Holzner
    publisher: coriolis
    It has a nice example of drawing shapes in a GUI. Should help you solve you problems. Hope this helps!
    Riesa

  3. #3
    Join Date
    Sep 2002
    Posts
    16
    Instead of super.setVisible(false), try this.setVisible(false).... Maybe that will work.
    Jayceepoo

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center