Results 1 to 5 of 5
  1. #1
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    127
    Rep Power
    0

    Default This shouldn't be too hard.

    so yes, i listened to you guys and read through the whole API documentation for the JOptionPane.
    i found how to add components to it, Jrmonkey97 (or something) already told me to do it that way but i didn't listen..

    well i made an object like:
    Java Code:
    label4 = new JLabel("Check this out");
    label5 = new JLable("I dont know how to add this label");
    object[] test = {label4, label5};
    
    JOptionPane.showMessageDialog(null, test[0], "Log in", JOptionPane.INFORMATION_MESSAGE);
    so YES i managed to add that label to the JOptionpane, my only question is: how do i add multiple labels/textfields/passwordfields to that optionpane, because in the API it says that you should be able to do:
    Java Code:
    JOptionPane.showMessageDialog(null, test[0], test[1], "log in" , JOptionPane.INFORMATION_MESSAGE);
    but that doesnt work, it gives me this error:
    The method showMessageDialog(Component, Object, String, int, Icon) in the type JOptionPane is not applicable for the arguments (null, Object, Object, String, int)


    so yeah, if anyone would be able to tell me how to add more components from that array in a JOptionpane, that would be amazing! :)

    thanks in advance

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default Re: This shouldn't be too hard.

    What exactly are you trying to do? Are you looking to implement a login, with username and password?

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

    Default Re: This shouldn't be too hard.

    Straight from the API documentation:

    messageA descriptive message to be placed in the dialog box. In the most common usage, message is just a String or String constant. However, the type of this parameter is actually Object. Its interpretation depends on its type:

    Object[]An array of objects is interpreted as a series of messages (one per object) arranged in a vertical stack. The interpretation is recursive -- each object in the array is interpreted according to its type.

    ComponentThe Component is displayed in the dialog.

    IconThe Icon is wrapped in a JLabel and displayed in the dialog.

    othersThe object is converted to a String by calling its toString method. The result is wrapped in a JLabel and displayed.
    Carefully read what it has to say if you pass an array of Objects ...

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    256
    Rep Power
    3

    Default Re: This shouldn't be too hard.

    You dont need to specify and index, just pass the array in and it will recursively go through the array.

    Java Code:
    JOptionPane.showMessageDialog(null, test[0], test[1], "log in" , JOptionPane.INFORMATION_MESSAGE);
    
    //becomes:
    
    JOptionPane.showMessageDialog(null, test, "log in" , JOptionPane.INFORMATION_MESSAGE);
    ALSO you have miss spelt JLabel as JLable you might want to change that

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

    Default Re: This shouldn't be too hard.

    LOL thanks for noticing the typo man,

    thanks

Similar Threads

  1. This ap course is so hard :(((
    By Army in forum New To Java
    Replies: 22
    Last Post: 01-10-2013, 02:27 PM
  2. Replies: 1
    Last Post: 10-10-2011, 05:51 PM
  3. Final variables shouldn't also be static?
    By emilioJazz in forum Advanced Java
    Replies: 5
    Last Post: 11-29-2010, 12:10 PM
  4. How hard is it.....
    By neilp123 in forum New To Java
    Replies: 4
    Last Post: 07-22-2010, 05:21 PM

Posting Permissions

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