I have an assignment where I need to make a list box containing an array of item
ID: 663696 • Letter: I
Question
I have an assignment where I need to make a list box containing an array of items, when I user selects one of the items the price is returns in a text field. Below is what I've got so far? What's unnessary? What can I be doing better? So far it compiles but it doesn't work. Ideally, I wouldn't use a button and they'd jjust select the list item
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JStore extends JFrame implements ActionListener {
private JPanel topPanel;
private JList listbox;
private JButton button;
String[] item = {"Dog Food", "Lighter", "Apple", "Bread"};
double[] cost = {10.00, 1.00, 0.50, 1.10};
//constructor
public JStore()
{
super("JStore Box Demo");
setLayout(new FlowLayout());
topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
getContentPane().add(topPanel);
setSize(400, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
listbox = new JList(item);
button = new JButton("Check");
button.addActionListener(this);
topPanel.add(listbox, BorderLayout.CENTER);
getContentPane().add( topPanel );
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("Check"))
{
int choice = listbox.getSelectedIndex();
System.out.println(item[choice] + ". Cost: " + cost[choice]);
}
else
{
return;
}
}
public static void main(String[] args)
{
// Run the program now.
JStore app = new JStore();
}
}
Explanation / Answer
Whenever you click an item in the listbox, the valueChanged() method is called and we can get the index of the selected item and set the cost in the textfield.
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.*;
public class JStore extends JFrame implements ListSelectionListener {
private JPanel topPanel;
private JList listbox;
String[] item = { "Dog Food", "Lighter", "Apple", "Bread" };
double[] cost = { 10.00, 1.00, 0.50, 1.10 };
private JTextField displayCost;
Container c = getContentPane();
// constructor
public JStore() {
super("JStore Box Demo");
c.setLayout(new BorderLayout());
topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
listbox = new JList(item);
listbox.addListSelectionListener(this);
topPanel.add(listbox);
c.add(topPanel,BorderLayout.CENTER);
displayCost = new JTextField();
c.add(displayCost, BorderLayout.SOUTH);
setVisible(true);
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Check")) {
int choice = listbox.getSelectedIndex();
System.out.println(item[choice] + ". Cost: " + cost[choice]);
} else {
return;
}
}
public static void main(String[] args) {
// Run the program now.
JStore app = new JStore();
}
public void valueChanged(ListSelectionEvent ls)
{
if(ls.getValueIsAdjusting())
{
displayCost.setText(""+cost[listbox.getSelectedIndex()]);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.