Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    130
    Rep Power
    0

    Default i have been asking a lot today, but i have another problem :D

    this program lets you fill in a username and 2 passwords, this opens up some windows.
    the last window is a log in window, so you have to log in with the information you just entered.
    however, i can make the window pop up. but i cant add a password field and a text field to it. (and JLabels), do you have an idea how to add a passwordfield, jlabels, and a textfield to the last window?

    this is the code:

    Java Code:
    import javax.swing.JOptionPane;
    import java.awt.event.ActionListener;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import javax.swing.JLabel;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JTextField;
    import javax.swing.JPasswordField;
    public class LogIn extends JFrame{
    	
    	private JTextField txt1;
    	private JPasswordField ps1;
    	private JPasswordField ps2;
    	private JPasswordField ps3;
    	private JLabel label1;
    	private JLabel label2;
    	private JLabel label3;
    	private JButton b1;
     
    	
    	
    
    
    public LogIn(){
    	super("Account");
    	setLayout (new FlowLayout());
    	label1 = new JLabel("Enter your username: ");
    	txt1 = new JTextField(10);
    	label2 = new JLabel("Enter your password: ");
    	ps1 = new JPasswordField(10);
    	label3 = new JLabel("Confirm your password: ");
    	ps2 = new JPasswordField(10);
    	b1 = new JButton("OK");
    	add(label1);
    	add(txt1);
    	add(label2);
    	add(ps1);
    	add(label3);
    	add(ps2);
    	add(b1);
    	
    	
    	thehandler handler = new thehandler();
    	b1.addActionListener(handler);
    	
    	
    }
    public class thehandler implements ActionListener{
    	public void actionPerformed(ActionEvent event){
    		if(txt1.getText().equals(ps1.getText()))
    		JOptionPane.showMessageDialog(null, "Password error, your username can't be the same as your password","Password Error!",JOptionPane.ERROR_MESSAGE);
    		else if(ps1.getText().equals(ps2.getText()))
    		JOptionPane.showMessageDialog(null, "Account information: "+"\n"+"User: "+txt1.getText()+"\n"+"Password: "+ps1.getText(), "Account Information", JOptionPane.INFORMATION_MESSAGE);
    		else
    		JOptionPane.showMessageDialog(null, "Passwords did not match! try again please!", "Password error!", JOptionPane.ERROR_MESSAGE);
    		if(ps1.getText().equals(ps2.getText()))
    		JOptionPane.showMessageDialog(null, "Please log in here: ", "Log in" , JOptionPane.INFORMATION_MESSAGE);
    
    	}
    }
    
    	
    }
    Last edited by MR bruto; 05-08-2013 at 04:57 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: i have been asking a lot today, but i have another problem :D

    You'll need a JDialog.
    JOptionPane is just a series of handy pre-built JDialogs.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    130
    Rep Power
    0

    Default Re: i have been asking a lot today, but i have another problem :D

    any other solutions guys?

  4. #4
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    130
    Rep Power
    0

    Default Re: i have been asking a lot today, but i have another problem :D

    i really need another solution guys

  5. #5
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: i have been asking a lot today, but i have another problem :D

    make sure your if statements fire correctly, also put them in correct {} blocks, its bad practice to not put them in if you have that many if/else statements.

  6. #6
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    130
    Rep Power
    0

    Default Re: i have been asking a lot today, but i have another problem :D

    BUT i still dont know how to add JButtons, JTextFields, and JPasswordFields to that last window.
    :/

  7. #7
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: i have been asking a lot today, but i have another problem :D

    Any component can be passed as the "message" argument to showMessageDialog. just add it in to a valid Constructor, look up the JOptionPane constructors, You might need separate Panes for user and pass.

  8. #8
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    130
    Rep Power
    0

    Default Re: i have been asking a lot today, but i have another problem :D

    no i already saw that, but i just cant add my password fields and jlabels and textfields to that JOptionPane

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: i have been asking a lot today, but i have another problem :D

    Populate a JPanel with the relevant bits in it and then use that with the showMessageDialog (or whatever).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    130
    Rep Power
    0

    Default Re: i have been asking a lot today, but i have another problem :D

    not sure how im going to do that, can you create an example?

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: i have been asking a lot today, but i have another problem :D

    You probably need to go through the Swing tutorials if you can't create and populate a JPanel.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: i have been asking a lot today, but i have another problem :D

    Quote Originally Posted by MR bruto View Post
    no i already saw that, but i just cant add my password fields and jlabels and textfields to that JOptionPane
    So the API documentation tells us that you can add whatever to a JOptionPane and you're telling us that you can't add simple JComponents; guess who/what I am going to believe ...

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    130
    Rep Power
    0

    Default Re: i have been asking a lot today, but i have another problem :D

    i didn't see how it is done anywhere.

    can you create an example for me

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: i have been asking a lot today, but i have another problem :D

    Quote Originally Posted by MR bruto View Post
    i didn't see how it is done anywhere.

    can you create an example for me
    In four minutes you've read and understood the API documentation for the JOptionPane class? Hats off to you.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: i have been asking a lot today, but i have another problem :D

    you can make other components easilt enough like JButtons, so why cant you make a JPanel?

    Java Code:
    JPanel jp = new JPanel();
    frame.add(jp);
    jp.add(button1);
    jp.add(button2);
    frame.setVisible(true);
    jp.setVisible(true);
    
    //end of JPanel example
    
    //start of JOptionPane
    
    JOptionPane.showMessageDialog(null,  PLACE JPANEL HERE, "Test", JOptionPane.ERROR_MESSAGE); //NOTE: ERROR MESSAGE ISNT THE BEST CHOICE HERE, LOOK UP OTHERS

    Also you can use an Object Array and pass that to the JOptionPane

    Java Code:
    Object[] test = {message, passWordField} // You obviously need to define these before hand
    JOptionPane.showMessageDialog(null, test, "Hello", JOptionPane.PLAIN_MESSAGE);  // PLAIN MESSAGE IS ANOTHER EXAMPLE OF A JOPTIONPANE MESSAGE TYPE
    Thats the logic, now you take that and apply it to your code, but pass the JPanel into the JOptionPane
    Last edited by monkeyjr97; 05-09-2013 at 03:54 PM.

  16. #16
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    130
    Rep Power
    0

    Default Re: i have been asking a lot today, but i have another problem :D

    thanks man! you helped me a lot :)

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: i have been asking a lot today, but i have another problem :D

    Quote Originally Posted by MR bruto View Post
    thanks man! you helped me a lot :)
    You could've read it all yourself in the API documentation before asking here ...

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: i have been asking a lot today, but i have another problem :D

    Quote Originally Posted by JosAH View Post
    You could've read it all yourself in the API documentation before asking here ...

    Jos
    Indeed, he didn't quite understand about the API i don't think! (No offense intended)

  19. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: i have been asking a lot today, but i have another problem :D

    Quote Originally Posted by monkeyjr97 View Post
    Indeed, he didn't quite understand about the API i don't think! (No offense intended)
    Lots of people don't read anymore; they watch youtube a bit or at best skim through the documentation, looking for code snippets they can copy and paste. Without understanding what they're doing and what they have done they come here with a bunch of chaotic questions ... and, (this is the worst part) they don't understand the answers because they haven't read the fundamentals. It's sad ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  20. #20
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: i have been asking a lot today, but i have another problem :D

    ye API can be a bit confusing at times, but you can eventually work it out, the school system isn't really helping either. In uni our class is taught like " Dont worry about this line of code, just copy it " luckily i already understood since i like to get ahead on classed. This being said quite a lot of people get into the habit of copy/pasting code then don't know why it doesn't work, crazy!

    Swing builder are also really bad for beginners, there's been a couple on here lately.

Page 1 of 2 12 LastLast

Similar Threads

  1. Need help with assignment (due today)
    By KAM0002 in forum New To Java
    Replies: 4
    Last Post: 12-08-2010, 07:06 AM
  2. Hello all I joined this forum today
    By psm in forum Advanced Java
    Replies: 4
    Last Post: 04-16-2009, 01:26 PM
  3. hi today i have joined in the forum
    By nagesh_113 in forum Introductions
    Replies: 1
    Last Post: 07-29-2007, 07:03 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
  •