Results 1 to 18 of 18
Like Tree2Likes
  • 1 Post By DarrylBurke
  • 1 Post By DarrylBurke

Thread: JOption Pane Warning message help

  1. #1
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default JOption Pane Warning message help

    So below is my code for a warning pane to pop up and tell the user to select only 1 name from my JList of names that I have. The only thing is that when this warning message runs, it doesn't show up until I click back inside my GUI, then it pops to the front. What I want to solve is have it so that it just automatically appears on the front when it is executed.
    any ideas?

    Java Code:
    public static String getSelectedNameController(AddressBookGUI ui) {
    		String name1 = ui.getSelectedContact();
    		String name2 = ui.getSelectedFavorite();
    		if(name1 != null && name2 != null){
    			JOptionPane.showMessageDialog(warningFrame,
    				    "Please select names from only one list!",
    				    "Warning!",
    				    JOptionPane.WARNING_MESSAGE);
    			return null;
    		}
    		else if(name1 != null && name2 == null){
    			return name1;
    		}
    		else{
    			return name2;
    		}
    	}

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: JOption Pane Warning message help

    Quote Originally Posted by adjit View Post
    So below is my code for a warning pane to pop up and tell the user to select only 1 name from my JList of names that I have.
    Why don't you just set the JList's selection mode?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: JOption Pane Warning message help

    Moved from New to Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: JOption Pane Warning message help

    Why don't you just set the JList's selection mode?
    because there are 2 different jlists, one is for contacts, the other for favorites

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: JOption Pane Warning message help

    Quote Originally Posted by adjit View Post
    because there are 2 different jlists, one is for contacts, the other for favorites
    So? Looks like you don't know what a JList's selection mode is, and can't be bothered to find out.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: JOption Pane Warning message help

    Quote Originally Posted by DarrylBurke View Post
    So? Looks like you don't know what a JList's selection mode is, and can't be bothered to find out.
    I know what it is. Don't always assume people who post on these forums aren't open for suggestions, because why else would I be posting here.

    In my situation I am using 2 different models. a contactListModel, and a favoriteListModel. So now could you please explain to me how I could do that in the case that I am using 2 separate models. I looked into selection mode and it can be applied to a model, but it doesn't communicate between models... hence my issue, hence why I would like to have a warning box, hence my underlying problem of bringing the warning box to the front

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

    Default Re: JOption Pane Warning message help

    What Darryl is getting at is that JList has a method setSelectionModel(...) that if called correctly will guarantee that only one selection is ever possible, and that the API will show you all you need to know about this.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: JOption Pane Warning message help

    Quote Originally Posted by Fubarable View Post
    What Darryl is getting at is that JList has a method setSelectionModel(...)
    Um, setSelectionMode(...), which forwards to the ListSelectionModel.

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: JOption Pane Warning message help

    Quote Originally Posted by adjit View Post
    I know what it is. Don't always assume people who post on these forums aren't open for suggestions, because why else would I be posting here.

    In my situation I am using 2 different models. a contactListModel, and a favoriteListModel. So now could you please explain to me how I could do that in the case that I am using 2 separate models. I looked into selection mode and it can be applied to a model, but it doesn't communicate between models... hence my issue, hence why I would like to have a warning box, hence my underlying problem of bringing the warning box to the front
    If -- and that's still not clear to me from your ramblings about two ListModels -- you are trying to ensure a single selection across two JLists, the easy way is to set SINGLE_SELECTION_MODE on both and clearSelection() on the other list when a selection is made.

    If -- as seems possible from the names -- the 'favoriteListModel' holds a subset of the elements in the 'contactListModel' then selection listeners on both lists could update the selections to match whenever the selected element is present in both lists. Note that you may need a boolean flag to prevent infinite recursion between the two selection listeners.

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: JOption Pane Warning message help

    Okay, let me outline what both of the lists do. In fact, favoriteList IS a subset of contact list. Any contact in favorite list will be in contact list.
    The way I have it set up is if I add a name to the favorite list it will be added to the contact list.
    So now my two questions are being that I have contactListModel and favoritListModel do do I just need to setSelectionMode of the either listModel and my whole GUI will automatically be set so that only one person can be selected at a time? Or would I have to have an if else

  12. #12
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: JOption Pane Warning message help

    And if I am not relating to what you are saying could you possibly maybe give me some pseudo code to help me out

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: JOption Pane Warning message help

    Quote Originally Posted by adjit View Post
    do do I just need to setSelectionMode of the either listModel and my whole GUI will automatically be set so that only one person can be selected at a time? Or would I have to have an if else
    That's already answered. Read my response at #10. If there's anything you don't understand, go through the tutorial linked from the API for JList. If you still don't get it, then ask a specific question.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: JOption Pane Warning message help

    Quote Originally Posted by adjit View Post
    And if I am not relating to what you are saying could you possibly maybe give me some pseudo code to help me out
    See above ^^^
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  15. #15
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: JOption Pane Warning message help

    okay, so I have a mouse click listener for BOTH my contactList and my favoriteList.
    So, if I click within the contactList, should I do favoriteListModel.clearSelect();
    and vice versa for my favoriteList?

  16. #16
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: JOption Pane Warning message help

    alright, so I actually figured it out, thank you. But still back to my original question, now just for clarity sake and for the future, how would I bring that warning frame to the front of the screen?

  17. #17
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: JOption Pane Warning message help

    Quote Originally Posted by adjit View Post
    okay, so I have a mouse click listener for BOTH my contactList and my favoriteList.
    So, if I click within the contactList, should I do favoriteListModel.clearSelect();
    and vice versa for my favoriteList?
    A MouseListener not the right listener to use to react to changes in a JList's selection. Since you seem to be determined to not go through the tutorial on How to Use Lists, I'm outta here.

    For the future: find and go through the Oracle tutorial on How to Make Dialogs.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  18. #18
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: JOption Pane Warning message help

    Quote Originally Posted by DarrylBurke View Post
    A MouseListener not the right listener to use to react to changes in a JList's selection. Since you seem to be determined to not go through the tutorial on How to Use Lists
    I have gone through the API and I have looked into several things. I am using the MouseListener for something else. When the user clicks in the list it grabs the first selected contact, and does stuff with that information. In my current case, a MouseListener applies, and works the way I want it to. So, my plan was to have it so that when I hear a mouse click in the contact list, clear the selections from favorites. And when it hears a mouse click in the favorite list, clear the selections in contact list. And yes, my lists are also in SINGLE_SELECTION mode

Similar Threads

  1. Help with Int and JOption Input dialog
    By stephanie904 in forum New To Java
    Replies: 1
    Last Post: 02-29-2012, 02:22 AM
  2. I need help with JOption Panel!
    By jakex69 in forum New To Java
    Replies: 3
    Last Post: 01-26-2012, 05:08 AM
  3. Example of JOption
    By RanLineR in forum New To Java
    Replies: 4
    Last Post: 10-20-2010, 01:27 PM
  4. JOption pop up window is behind all other windows
    By biggmann in forum New To Java
    Replies: 3
    Last Post: 10-15-2010, 09:24 PM
  5. JOption Msgbox title help?
    By ZaabZ in forum New To Java
    Replies: 7
    Last Post: 08-26-2010, 10:12 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
  •