4 Replies - 244 Views - Last Post: 15 October 2019 - 03:58 AM Rate Topic: -----

#1 robgeek   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 193
  • Joined: 15-January 13

JComboBox: Can't set default items using addItem().

Posted 14 October 2019 - 11:24 AM

Hi!

I'm trying to make a JComboBox with default items and I don't understand why I'm getting the following error message. I know I can make a String[] and pass it as argument to the constructor, but I would like to make this using addItem() if is possible.

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Window extends JFrame {

	private JComboBox comboBox;
	private JPanel panel;

	public Window() {
		createComponents();
		configComponents();
		placeComponents();
		configJFrame();
	}

	private void configComponents() {
		this.comboBox.setEditable(false);
		this.comboBox.addItem("Item 1");
		this.comboBox.addItem("Item 2");
	}

	private void configJFrame() {
		setSize(350, 246);
		setLocation(150, 100);
		setResizable(false);
		setVisible(true);
	}

	private void createComponents() {
		this.comboBox = new JComboBox();
		this.panel = new JPanel();
	}
	
	private void placeComponents() {
		this.panel.add(this.comboBox);
		add(this.panel);
	}

	public static void main(String[] args) {
		new Window();
	}
}

[[email protected] ~]$ javac window.java 
Note: window.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.


[[email protected] ~]$ javac -Xlint window.java 
window.java:7: warning: [rawtypes] found raw type: JComboBox
	private JComboBox comboBox;
	        ^
  missing type arguments for generic class JComboBox<E>
  where E is a type-variable:
    E extends Object declared in class JComboBox
window.java:19: warning: [unchecked] unchecked call to addItem(E) as a member of the raw type JComboBox
		this.comboBox.addItem(makeObj("Item 1"));
		                     ^
  where E is a type-variable:
    E extends Object declared in class JComboBox
window.java:20: warning: [unchecked] unchecked call to addItem(E) as a member of the raw type JComboBox
		this.comboBox.addItem("Item 2");
		                     ^
  where E is a type-variable:
    E extends Object declared in class JComboBox
window.java:31: warning: [rawtypes] found raw type: JComboBox
		this.comboBox = new JComboBox();
		                    ^
  missing type arguments for generic class JComboBox<E>
  where E is a type-variable:
    E extends Object declared in class JComboBox
window.java:5: warning: [serial] serializable class Window has no definition of serialVersionUID
public class Window extends JFrame {



I tried to declare JComboBox<String> comboBox but I got the same error message.

Is This A Good Question/Topic? 0
  • +

Replies To: JComboBox: Can't set default items using addItem().

#2 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3649
  • View blog
  • Posts: 16,773
  • Joined: 20-September 08

Re: JComboBox: Can't set default items using addItem().

Posted 14 October 2019 - 11:32 AM

Quote

I tried to declare JComboBox<String> comboBox but I got the same error message.

And your code to create it was ..?
Was This Post Helpful? 1
  • +
  • -

#3 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 6,062
  • Joined: 25-December 13

Re: JComboBox: Can't set default items using addItem().

Posted 14 October 2019 - 11:48 AM

Window is the name of a Java SE class. You should NOT use that name for your class.
Was This Post Helpful? 1
  • +
  • -

#4 robgeek   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 193
  • Joined: 15-January 13

Re: JComboBox: Can't set default items using addItem().

Posted 14 October 2019 - 02:34 PM

View Postg00se, on 14 October 2019 - 11:32 AM, said:

Quote

I tried to declare JComboBox<String> comboBox but I got the same error message.

And your code to create it was ..?


Actually I tried:
private JComboBox<String> comboBox;
	private void createComponents() {
		this.comboBox = new JComboBox();
		this.panel = new JPanel();
	}

But changed to this:
this.comboBox = new JComboBox<String>();

And now is working fine.

Quote

Window is the name of a Java SE class. You should NOT use that name for your class.

I'll change that.
Thanks!
Was This Post Helpful? 0
  • +
  • -

#5 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3649
  • View blog
  • Posts: 16,773
  • Joined: 20-September 08

Re: JComboBox: Can't set default items using addItem().

Posted 15 October 2019 - 03:58 AM

Quote

I'll change that.


As it happens, you could use it without fear if your class were in a package (an explicit one as opposed to the default it's currently in)
But it's perhaps better to avoid name clashes nonetheless. Apart from that, your chosen class name does not reflect what the class does. btw you should use nouns, as at present, for class names
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1