Results 1 to 5 of 5
  1. #1
    erikn is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default What's a good Swing component to list elements of a String array?

    Greetings all. Beginner here, first post and I'm sure this is a pretty basic question...

    I'm making a Swing GUI and I need a pane into which I can gradually add one line of String text at a time (and delete if need be). If it gave me automatic scroll bars, that would be even better. I put a JScrollPane into it, but I'm not finding any documentation on how to add text Strings. Seems I have to put the text into a JLabel first (I'm pretty new at this so I could be wrong). I don't want to have to do this...

    Am I being unreasonable? Thanks in advance...

    erik

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

    Default Re: What's a good Swing component to list elements of a String array?

    I think that you're right on the money with using a JList and putting it into a JScrollPane. Give it a try and see if it works for you.

  3. #3
    erikn is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Re: What's a good Swing component to list elements of a String array?

    Quote Originally Posted by Fubarable View Post
    I think that you're right on the money with using a JList and putting it into a JScrollPane. Give it a try and see if it works for you.
    Woah, thanks for mentioning the JList component....forgot about that one, I think it may be just what I'm looking for.

    Also, I have another question now about uneditable JComboBoxes. In my app, I'm trying to take Date input from 3 JComboBoxes - a String for the month, and ints for the day and year respectively. When I try to read the month in my ActionPerformed(), I'm getting a null pointer exception on that line.

    I am instantiating the JComboBox like this:

    String[] months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jly", "Aug", "Sep", "Oct", "Nov", "Dec" };
    JComboBox monthBox = new JComboBox(months);

    and attempting to access it in ActionPerformed() like this:

    String month = (String)monthBox.getSelectedItem();

    I suspect that this might have to do with not adding a listener to the ComboBox, but I'm not positive. I didn't add a listener because there are other areas of the form I want the user to fill out first. I don't want info pulled from the ComboBox until a button is pressed on the bottom of the form....and also, what if the default value in the box happens to be the value the user wants? Wouldn't the listener not be called unless the value in the box changes? I apologize for what is probably a silly question.

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

    Default Re: What's a good Swing component to list elements of a String array?

    You do not need to add a listener to the JComboBox to be able to extract information from it, so no, the problem lies elsewhere in code not shown. At this point all we can do is guess what's wrong, and if you still need help, you'll need to do some debugging to isolate the error and to get that information to us.

  5. #5
    erikn is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Re: What's a good Swing component to list elements of a String array?

    Quote Originally Posted by Fubarable View Post
    You do not need to add a listener to the JComboBox to be able to extract information from it, so no, the problem lies elsewhere in code not shown. At this point all we can do is guess what's wrong, and if you still need help, you'll need to do some debugging to isolate the error and to get that information to us.
    Hey, thanks for responding again Fubarable. I actually figured out what I was doing wrong earlier today.
    It was in this assignment:

    JComboBox monthBox = new JComboBox(months);

    I had already declared monthBox as an instance variable of that GUI class, so that second declaration instantiated it as a local variable. Once I changed that line to

    monthBox = new JComboBox(months);

    I was able to extract the content in actionPerformed() no problem. Thanks a lot for the help though, and for recommending JList. Worked great!
    Last edited by erikn; 01-29-2013 at 07:42 AM.

Similar Threads

  1. Replies: 1
    Last Post: 05-11-2012, 03:23 PM
  2. Assign elements of list to an array
    By pink123 in forum New To Java
    Replies: 1
    Last Post: 03-07-2011, 04:25 PM
  3. Adding elements in array list and vectors using threads
    By hina.yousuf@seecs.edu.pk in forum Advanced Java
    Replies: 2
    Last Post: 10-10-2010, 04:07 AM
  4. Get value of a String into elements of an Array.
    By mainy in forum New To Java
    Replies: 1
    Last Post: 08-01-2009, 10:17 PM
  5. Splitting single string into array elements
    By phil128 in forum New To Java
    Replies: 11
    Last Post: 01-12-2009, 12:51 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
  •