Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: JComboBox and array problems

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default JComboBox and array problems

    Hope someone can help me understand what is happening:

    I decided to use a JComboBox in my program and I got this message: "USES UNCHECKED OR UNSAFE OPERATIONS" So I checked online and tried changing the code, then I just tried all the other codes I found online and also examples form the books and I always get this message for any program that uses JComboBox and an array.
    I am using a new computer, a laptop pc, windows 8 and Java 7? Any idea what can be causing this message to appear?
    Thanks a lot.
    Tristan


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: JComboBox and array problems

    First off, this message is a warning, not an error, and it will not prevent your code from running or compiling. The issue I believe is that you're using Java 1.6 code but compiling with a Java 1.7 compiler. Since Java 1.7 JComboBox and its model will accept generic parameters, and the warning is telling you that you've neglected to give your JComboBox a generic parameter. Consider doing that and using a parameter type that is whatever type your JComboBox is holding.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JComboBox and array problems

    Thanks a lot. It is working but as I said I got this warning for every program and example I try, even the ones shown in the Oracle documents that's why I am confused. Anyway what I am trying to do is a very simple combobox inside a program, if you don't mind to have a look and let me know what you think about it:

    private String[] formats = { "US Dollars", "Euros", "BR Reais", "Swiss Francs" };
    private JComboBox formatBox = new JComboBox(formats);
    private String sCurrency;

    ...

    JPanel paneCombo = new JPanel();
    paneCombo.setLayout(new BoxLayout(paneCombo, BoxLayout.Y_AXIS));
    paneCombo.setBorder(BorderFactory.createTitledBord er("Currencies"));

    JLabel formatLabel = new JLabel("Choose a Currency:");
    JPanel formatLbl = new JPanel(new FlowLayout(FlowLayout.CENTER));
    formatLbl.add(formatLabel);
    paneCombo.add(formatLbl);
    paneCombo.add(formatBox);

    westOfEast.add(paneCombo, BorderLayout.SOUTH);


    sCurrency = formatBox.getSelectedItem().toString();

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: JComboBox and array problems

    So you're using a JCombBox of String. I would do what I suggested above and make it a generic JComboBox<String>. Have you read about generics? If not I strongly urge you to read the tutorial(s) on this.

  5. The Following User Says Thank You to curmudgeon For This Useful Post:

    tristannvk (December 6th, 2012)

  6. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JComboBox and array problems

    I am a bit ashamed to say that I don't know much about generics but I thank you for your help and will study it further.

  7. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: JComboBox and array problems

    That sounds like a great plan. Best of luck!

  8. #7
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: JComboBox and array problems

    When you use a JComboBox it's good practice the use a ComboBoxModel to organise the data. Then you can use a constructor of JComboBox model where you can give the model as parameter. So you work on the model (delete, add,..) conform the mvc model.

Similar Threads

  1. Replies: 6
    Last Post: September 5th, 2011, 11:02 AM
  2. [SOLVED] 2 dimentional boolean array problems!
    By masterT in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 15th, 2011, 12:03 AM
  3. Copying Array Problems.. Not what you think
    By xXRedneckXx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 5th, 2011, 12:01 PM
  4. JComboBox selection not showing item from object array
    By oper125 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 24th, 2010, 06:31 AM
  5. Replies: 4
    Last Post: May 27th, 2008, 02:20 PM