It's quite simple, really.
The message is just NetBeans informing you that the code of method comboItemItemStateChanged
does not reference the method parameter evt
. It is not an error nor even a warning. You can ignore it. NetBeans displays these "hints" in its editor when you write code whereas NetBeans GUI builder generates code.
Note that method comboItemItemStateChanged
will be called each time an item in the JComboBox
is selected and also each time an item is de-selected. I'm guessing that you only want the code to run when an item is selected, hence the first line of method comboItemItemStateChanged
should be
if (evt.getStateChange() == ItemEvent.SELECTED) {
and then you are referencing the method parameter and the "hint" will go away.
Refer to How to Write an Item Listener
Edit
I think maybe you would benefit from learning about GUI design. I have never seen one like yours. I would like to know how you arrived at that design. In any case, the below code addresses only the functionality whereby the list in comboItem
adjusts depending on the selected item in comboGroup
.
In my experience, removing and adding items to the JComboBox
via methods removeAllItems
and addItem
, is very slow. It is much more efficient to simply replace the JComboBox
model which is what I have done in the below code. For small lists such as yours, the difference will not be noticed but it increases as the lists get larger.
Also, the [JComboBox
] selected item is in the evt
parameter to method comboItemItemStateChanged
.
Your original code did not add an item listener to comboGroup
nor did it have a main
method. I have added these in the below code. Note that the item listener is added via a method reference.
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import java.awt.Color;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
*
* @author kahbg
*/
public class GuwiPos extends JFrame implements ActionListener {
public static final Map<String, ComboBoxModel<String>> LISTS;
JButton buttonBasket;
JButton buttonHelp;
JTextField txtGroup;
JTextField txtItem;
JTextField txtQty;
JTextField txtTotal;
JTextField txtPrice;
JComboBox<String> comboGroup;
JComboBox<String> comboItem;
static {
LISTS = new HashMap<>();
Vector<String> v = new Vector<>();
v.add("Plakat");
v.add("Halfmoon");
v.add("Crown Tail");
v.add("Double Tail");
ComboBoxModel<String> model = new DefaultComboBoxModel<>(v);
LISTS.put("Betta Fish", model);
v = new Vector<>();
v.add("Apple Horn");
v.add("RamsHorn");
v.add("Pond Snail");
v.add("Assassin Snail");
model = new DefaultComboBoxModel<>(v);
LISTS.put("Snails", model);
v = new Vector<>();
v.add("Small Fine Net");
v.add("Large Fine Net");
v.add("Flaring Mirror");
v.add("Aquarium Hose");
model = new DefaultComboBoxModel<>(v);
LISTS.put("Supplies", model);
v = new Vector<>();
v.add("Tubifex");
v.add("Daphnia");
v.add("Optimum Pellets");
model = new DefaultComboBoxModel<>(v);
LISTS.put("Food", model);
}
GuwiPos() {
// ComboBox
String[] products = {"Select Item...", "Betta Fish", "Snails", "Supplies", "Food"};
comboGroup = new JComboBox<String>(products);
comboGroup.addItemListener(this::comboItemItemStateChanged);
comboGroup.setBounds(130, 35, 150, 40);
comboItem = new JComboBox<String>();
comboItem.setBounds(130, 85, 150, 40);
// TextFields
txtPrice = new JTextField();
txtPrice.setBounds(130, 135, 150, 40);
txtQty = new JTextField();
txtQty.setBounds(130, 185, 150, 40);
txtTotal = new JTextField();
txtTotal.setBounds(130, 235, 150, 40);
// txtTotalAmt = new JTextField(); to code later on
// txtPaid = new JTextField(); to code later on
// txtChange = new JTextField(); to code later on
// Labels
// Product
JLabel labelProduct = new JLabel();
labelProduct.setText("Item Group");
labelProduct.setBounds(10, 5, 100, 100);
// Item
JLabel labelItem = new JLabel();
labelItem.setText("Item");
labelItem.setBounds(10, 55, 100, 100);
// Price
JLabel labelPrice = new JLabel();
labelPrice.setText("Price");
labelPrice.setBounds(10, 105, 100, 100);
// Qty
JLabel labelQty = new JLabel();
labelQty.setText("Quantity");
labelQty.setBounds(10, 155, 100, 100);
// Total
JLabel labelTotal = new JLabel();
labelTotal.setText("Total");
labelTotal.setBounds(10, 205, 100, 100);
// Buttons
buttonBasket = new JButton();
buttonBasket.setBounds(0, 0, 300, 50);
buttonBasket.setText("Add to Basket");
buttonBasket.setFocusable(false);
buttonBasket.setBorder(BorderFactory.createEtchedBorder());
buttonBasket.addActionListener(this);
JButton buttonPay = new JButton();
buttonPay.setText("PAY");
buttonPay.setBounds(0, 50, 150, 100);
buttonPay.setFocusable(false);
buttonPay.setBorder(BorderFactory.createEtchedBorder());
buttonPay.addActionListener(e -> System.out.println("Payment Success"));
JButton buttonCancel = new JButton();
buttonCancel.setText("CANCEL");
buttonCancel.setBounds(0, 150, 150, 100);
buttonCancel.setFocusable(false);
buttonCancel.setBorder(BorderFactory.createEtchedBorder());
buttonCancel.addActionListener(e -> System.out.println("Transaction Cancelled"));
JButton buttonDiscount = new JButton();
buttonDiscount.setText("Apply Discount?");
buttonDiscount.setBounds(150, 50, 150, 50);
buttonDiscount.setFocusable(false);
buttonDiscount.setBorder(BorderFactory.createEtchedBorder());
buttonDiscount.addActionListener(e -> System.out.println("20% Discount Applied"));
JButton buttonHelp = new JButton();
buttonHelp.setText("HELP");
buttonHelp.setBounds(150, 100, 150, 100);
buttonHelp.setFocusable(false);
buttonHelp.setBorder(BorderFactory.createEtchedBorder());
JButton buttonDelete = new JButton();
buttonDelete.setText("DELETE");
buttonDelete.setBounds(150, 200, 150, 50);
buttonDelete.setFocusable(false);
buttonDelete.setBorder(BorderFactory.createEtchedBorder());
buttonDelete.addActionListener(e -> System.out.println("Item Deleted"));
// Panels
// Left contains item and price
JPanel bluePanel = new JPanel();
bluePanel.setBackground(Color.blue);
bluePanel.setBounds(0, 0, 300, 300); // x,y,width,height
bluePanel.setLayout(null);
// Right contains product basket
JPanel redPanel = new JPanel();
redPanel.setBackground(Color.red);
redPanel.setBounds(300, 0, 600, 300); // x,y,width,length
redPanel.setLayout(null);
// Bottom Left contains buttons pay,change,discount
JPanel greenPanel = new JPanel();
greenPanel.setBackground(Color.green);
greenPanel.setBounds(0, 300, 300, 450);// x,y,width,length
greenPanel.setLayout(null);
// Bottom Right contains total amount
JPanel yellowPanel = new JPanel();
yellowPanel.setBackground(Color.yellow);
yellowPanel.setBounds(0, 300, 900, 450);// x,y,width,length
yellowPanel.setLayout(null);
this.setTitle("Open Betta POS");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.setSize(900, 590);
this.setResizable(false);
this.setVisible(true);
// ADDED AWT
bluePanel.add(txtQty);
bluePanel.add(txtTotal);
bluePanel.add(txtPrice);
bluePanel.add(comboItem);
bluePanel.add(comboGroup);
bluePanel.add(labelProduct);
bluePanel.add(labelItem);
bluePanel.add(labelPrice);
bluePanel.add(labelQty);
bluePanel.add(labelTotal);
greenPanel.add(buttonBasket);
greenPanel.add(buttonPay);
greenPanel.add(buttonCancel);
greenPanel.add(buttonDiscount);
greenPanel.add(buttonHelp);
greenPanel.add(buttonDelete);
this.add(bluePanel);
this.add(redPanel);
this.add(greenPanel);
this.add(yellowPanel);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttonBasket) {
System.out.println("Added Item to Basket: " + comboGroup.getSelectedItem() + "\n"
+ comboItem.getSelectedItem());
}
}
// This is not working
public void comboItemItemStateChanged(java.awt.event.ItemEvent evt) {
if (evt.getStateChange() == ItemEvent.SELECTED) {
Object selection = evt.getItem();
ArrayList<String> arrayItem = new ArrayList<>();
Iterator<String> iter;
if (selection.equals("Betta Fish")) {
comboItem.setModel(LISTS.get("Betta Fish"));
}
else if (selection.equals("Snails")) {
comboItem.setModel(LISTS.get("Snails"));
}
else if (selection.equals("Supplies")) {
comboItem.setModel(LISTS.get("Supplies"));
}
else if (selection.equals("Food")) {
comboItem.setModel(LISTS.get("Food"));
}
else if (selection.equals("Select Item...")) {
comboItem.removeAllItems();
arrayItem.add("Select Item...");
iter = arrayItem.iterator();
while (iter.hasNext()) {
comboItem.addItem(iter.next());
}
}
}
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> new GuwiPos());
}
}