Reputation: 5
Newbie question:
So this code here
public void comboItemItemStateChanged(java.awt.event.ItemEvent evt) {
ArrayList<String> arrayItem = new ArrayList<>();
Iterator<String> iter;
if(comboGroup.getSelectedItem().equals("Betta Fish")){
comboItem.removeAllItems();
arrayItem.add("Plakat");
arrayItem.add("Halfmoon");
arrayItem.add("Crown Tail");
arrayItem.add("Double Tail");
iter = arrayItem.iterator();
while(iter.hasNext()){
comboItem.addItem(iter.next());
}
}
else if(comboGroup.getSelectedItem().equals("Snails")){
comboItem.removeAllItems();
arrayItem.add("Apple Horn");
arrayItem.add("RamsHorn");
arrayItem.add("Pond Snail");
arrayItem.add("Assassin Snail");
iter = arrayItem.iterator();
while(iter.hasNext()){
comboItem.addItem(iter.next());
}
works when I try it on comboBoxes from Design
tab in NetBeans. But when I try to apply it to my coded ComboBox, I get a message from evt
saying Unused method parameters should be removed
. Can I get an explanation why and what is the alternative for hardcoded comboBox
?
Purpose of code: a dynamic comboBox
so whatever I pick from comboBox1
will have each own set of lists for comboBox2
NOTE: I also tried to change comboItemItemStateChanged
to just itemStatChanged
.
Source code of my project: https://github.com/kontext66/GuwiPos/blob/main/GuwiPos
Sorry for the confusion everyone, I do have a main class.
public class Main{
public static void main(String[] args){
new GuwiPos();
new HelpWin();
}
}
Main.java, GuwiPos.java
Upvotes: 0
Views: 121
Reputation: 6168
ActionListener
)public class SwingApp {
private static JComboBox<String> comboItem;
private static JComboBox<String> productCombo;
public static void main (String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run () {
createAndShowGUI();
}
});
}
private static void createAndShowGUI () {
JFrame frame = createMainFrame();
JPanel bluePanel = createBluePanel();
JPanel greenPanel = createGreenPanel();
JPanel redPanel = createRedPanel();
JPanel yellowPanel = createYellowPanel();
frame.add(bluePanel);
frame.add(greenPanel);
frame.add(redPanel);
frame.add(yellowPanel);
frame.setVisible(true);
}
private static JFrame createMainFrame () {
JFrame frame = new JFrame("Open Betta POS");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.setSize(900, 590);
frame.setResizable(false);
return frame;
}
private static JPanel createBluePanel () {
ComboSelectionListener productComboListener = new ComboSelectionListener();
JPanel panel = new JPanel(null); // Sets layout manager to null which is a bad idea!
String[] products =
{"Select Item...", "Betta Fish", "Snails", "Supplies", "Food"};
productCombo = new JComboBox<>(products);
productCombo.setBounds(130, 35, 150, 40);
productCombo.setActionCommand("selectProduct");
productCombo.addActionListener(productComboListener);
comboItem = new JComboBox<>();
comboItem.setBounds(130, 85, 150, 40);
// TextFields
JTextField txtPrice = new JTextField();
txtPrice.setBounds(130, 135, 150, 40);
JTextField txtQty = new JTextField();
txtQty.setBounds(130, 185, 150, 40);
JTextField txtTotal = new JTextField();
txtTotal.setBounds(130, 235, 150, 40);
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);
panel.setBackground(Color.blue);
panel.setBounds(0, 0, 300, 300); // x,y,width,height
panel.add(txtQty);
panel.add(txtTotal);
panel.add(txtPrice);
panel.add(comboItem);
panel.add(productCombo);
panel.add(labelProduct);
panel.add(labelItem);
panel.add(labelPrice);
panel.add(labelQty);
panel.add(labelTotal);
return panel;
}
private static JPanel createGreenPanel () {
JPanel panel = new JPanel(null);
panel.setBackground(Color.green);
panel.setBounds(0, 300, 300, 450);// x,y,width,length
JButton buttonBasket = new JButton();
buttonBasket.setBounds(0, 0, 300, 50);
buttonBasket.setText("Add to Basket");
buttonBasket.setFocusable(false);
buttonBasket.setBorder(BorderFactory.createEtchedBorder());
buttonBasket
.addActionListener(e -> System.out.println("Added Item to Basket: "
+ productCombo.getSelectedItem() + "\n" + comboItem.getSelectedItem()));
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"));
panel.add(buttonBasket);
panel.add(buttonPay);
panel.add(buttonCancel);
panel.add(buttonDiscount);
panel.add(buttonHelp);
panel.add(buttonDelete);
return panel;
}
private static JPanel createRedPanel () {
JPanel panel = new JPanel(null);
panel.setBackground(Color.red);
panel.setBounds(300, 0, 600, 300); // x,y,width,length
return panel;
}
private static JPanel createYellowPanel () {
JPanel panel = new JPanel();
panel.setBackground(Color.yellow);
panel.setBounds(0, 300, 900, 450); // x,y,width,length
return panel;
}
private static class ComboSelectionListener implements ActionListener {
@Override
public void actionPerformed (ActionEvent e) {
JComboBox<String> comboGroup = (JComboBox<String>) e.getSource();
ArrayList<String> arrayItem = new ArrayList<>();
Iterator<String> iter;
if (comboGroup.getSelectedItem().equals("Betta Fish")) {
comboItem.removeAllItems();
arrayItem.add("Plakat");
arrayItem.add("Halfmoon");
arrayItem.add("Crown Tail");
arrayItem.add("Double Tail");
iter = arrayItem.iterator();
while (iter.hasNext()) {
comboItem.addItem(iter.next());
}
} else if (comboGroup.getSelectedItem().equals("Snails")) {
comboItem.removeAllItems();
arrayItem.add("Apple Horn");
arrayItem.add("RamsHorn");
arrayItem.add("Pond Snail");
arrayItem.add("Assassin Snail");
iter = arrayItem.iterator();
while (iter.hasNext()) {
comboItem.addItem(iter.next());
}
} else if (comboGroup.getSelectedItem().equals("Supplies")) {
comboItem.removeAllItems();
arrayItem.add("Small Fine Net");
arrayItem.add("Large Fine Net");
arrayItem.add("Flaring Mirror");
arrayItem.add("Aquarium Hose");
iter = arrayItem.iterator();
while (iter.hasNext()) {
comboItem.addItem(iter.next());
}
} else if (comboGroup.getSelectedItem().equals("Food")) {
comboItem.removeAllItems();
arrayItem.add("Tubifex");
arrayItem.add("Daphnia");
arrayItem.add("Optimum Pellets");
iter = arrayItem.iterator();
while (iter.hasNext()) {
comboItem.addItem(iter.next());
}
} else if (comboGroup.getSelectedItem().equals("Select Item...")) {
comboItem.removeAllItems();
arrayItem.add("Select Item...");
iter = arrayItem.iterator();
while (iter.hasNext()) {
comboItem.addItem(iter.next());
}
}
}
}
}
ActionListener
SwingUtilities
to launch Swing ApplicationUpvotes: 1
Reputation: 20914
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
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());
}
}
Upvotes: 2