Results 1 to 11 of 11
  1. #1
    Mass Zero is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default Removing Buttons?

    Yo,

    I'm almost ashamed to ask this, but I'm quite new to Java and GUIs in general, so whatever. :P

    I have to create a veeeeery simple login applet, but I'm stuck with one this: Once the person's username and password have been vierified, how do they move on? What I tried thus far is plain repainting the screen, which works, but keeps the text fields and login button intact, since they're added in the initialization. Plus working like this is quite inefficient, I'm guessing. So... how do I properly do this?

    Thanks for any reply.

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

    Default

    There are so many ways to do.As a starter no need to get ashamed.
    1.Just take a paper.
    2.List down what u want
    3.Try to implement as a code by putting method blocks with proper comments.Definitley, u will acheive what u want.
    Ramya:cool:

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Add those buttons to a panel and then remove (using the remove(Component comp method)) that panel from the main frame/JApplet when they have logged in.

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Well, usually a login is done by creating a popup JDialog. Then when the userid/password is verified you dispose the dialog and continue process. That way the login field are not part of the main GUI.

  5. #5
    Mass Zero is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

    Man, then I have new stuff to learn. :P Isn't there a simple command that just removes a button from a running applet and also maybe to add buttons to a running applet? (I'm guessing there isn't, but it's worth a shot.)

    Now that I'm asking stuff anyway, is there a way to generate a newline in g.drawString?
    Last edited by Mass Zero; 10-26-2009 at 06:18 PM.

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

    Default

    buttons can be added and removed (you'll want to study the Sun layout manager tutorial to learn more), but usually in this situation, I use a dialog (as camickr suggested above), or swap JPanels via a CardLayout.

  7. #7
    Mass Zero is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

    Oh crap, this is making me dizzy. Attempt number 3! I'm so messing up with the CardLayout lol. I get the picture, but... can I in some way make it so that the 2 text fields and button are displayed next to each other?

    Here's what I have so far:

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class O4Y extends Applet {
    	
    	CardLayout cards;
    	
    	JPanel auth;
    	TextField customer;
    	TextField password;
    	Button login;
    	
    	Font big   = new Font("Verdana", Font.BOLD, 24);
    	Font small = new Font("Verdana", Font.PLAIN, 12);
    	
    	public void init() {
    		
    		cards = new CardLayout(100, 10);
    		
    		setLayout(new BorderLayout());
    		
    		auth = new JPanel();
    		auth.setLayout(cards);
    		
    		customer = new TextField(8);
    		password = new TextField(12);
    		login    = new Button("Log In!");
    		
    		auth.add(customer, "customer");
    		auth.add(password, "password");
    		auth.add(login, "login");
    		
    		add(BorderLayout.NORTH, auth);
    		
    	}
    	
    	public void paint(Graphics g) {
    		
    		Image background = getImage(getDocumentBase(), "background.jpg");
    		g.drawImage(background, 0, 0, this);
    		
    		cards.show(auth, "customer");
    				
    	}
    }

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You can put them on a JPanel with the default FlowLayout.

  9. #9
    Mass Zero is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

    But if I do that, how do I make sure they disappear when a user "logs in"? :x Since if I use FlowLayout, won't they just... stay?

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Use the Dialog for them like has already been suggested.

  11. #11
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Quote Originally Posted by Mass Zero View Post
    But if I do that, how do I make sure they disappear when a user "logs in"? :x Since if I use FlowLayout, won't they just... stay?
    You still don't understand the concept. Did you read the tutorial and execute the demo program. The CardLayout is the main layout. Then you add child panels to the card layout. So the "Login Panel" would use a FlowLayout.

    Also, almost all of you program is wrong. I suggest you read the Swing tutorial for more information and examples. A few things I noticed:

    a) You should be extending JApplet, not applet. Read the tutorial for an example on creating an applet.

    b) Never override the paint() method when using Swing. Custom painting is done by overriding the paintComponent() method of a JPanel of JComponent, then you add the component to the GUI. Again the tutorial has a section on Custom Painting

    c) Don't mix AWT and Swing components. Swing components start with a "J". (JTextField, JButton...).

    There are no shortcuts to programming read the tutorial which will ultimately save you time.

Similar Threads

  1. Removing Indexes
    By gilbertsavier in forum JDBC
    Replies: 0
    Last Post: 07-17-2009, 08:23 AM
  2. trouble in removing a value
    By jacline in forum New To Java
    Replies: 5
    Last Post: 03-20-2009, 06:56 PM
  3. removing reference
    By ajith_raj in forum Advanced Java
    Replies: 4
    Last Post: 02-12-2009, 12:46 PM
  4. image removing
    By Triss in forum New To Java
    Replies: 3
    Last Post: 01-20-2008, 09:27 PM
  5. Removing characters
    By kDude in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 03:38 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
  •