Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Java GUI Programming Help?
I've been stuck trying this for the past 4 hours, can't get anything to work. I could do this with check boxes and radio buttons no problem but I cant build any events that will calculate a total to display. ActionListeners are all void types so I can't insert an if else statement for it to calculate a total and return the value. I'm just stuck on the ListListener section. Can you please review and let me know how I can correct it? The instructions require us to use lists and combo boxes that have the included fields to calculate a total and display it in a read-only section without having to press any buttons. I've been trying to think of any possible way I could correct it but nothings helped. HUGE THANKS IN ADVANCE!
note: if you are trying to run this program yourself, you must delete the greeting panel i created as another class file.
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class ConfRegistration extends JFrame
{
private GreetingPanel banner;
private JPanel regPanel;
private JPanel optionalPanel;
private JPanel totalPanel;
private JComboBox regType;
private JList options;
private JList selectedOptions;
private JTextField regTotal;
private JTextField optTotal;
private JTextField total;
private JLabel regLabel;
private JLabel optLabel;
private JLabel totLabel;
private String[] combo = {"General Registration - $895.00 per person", "Student Registration - $495.00 per person"};
private String[] list = {"Introduction to E-commerce - $295.00", "The Future of the Web - $295.00", "Advanced" +
" Java Programming - $395.00", "Network Security - $395.00", "Opening Night Dinner - $30.00"};
private final double GENERAL_REG_FEE = 895.00;
private final double STUDENT_REG_FEE = 495.00;
public final double E_COMMERCE = 295.00;
public final double FUTURE_WEB = 295.00;
public final double JAVA = 395.00;
public final double NETWORK = 395.00;
public final double DINNER = 30.00;
public ConfRegistration()
{
setTitle("Conference Registration");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
buildRegistrationPanel();
buildOptionalPanel();
buildTotalPanel();
banner = new GreetingPanel();
add(banner, BorderLayout.NORTH);
add(regPanel, BorderLayout.WEST);
add(optionalPanel, BorderLayout.EAST);
add(totalPanel, BorderLayout.SOUTH);
pack();
setVisible(true);
}
private void buildRegistrationPanel()
{
regPanel = new JPanel(); //create a panel to hold the combo box
regType = new JComboBox(combo); //create the combo box
regType.addActionListener(new ComboBoxListener()); //add action listener
regPanel.add(regType); //add combo box to panel
}
private void buildOptionalPanel()
{
optionalPanel = new JPanel(); //create a panel to hold the list
options = new JList(list); //create the list
selectedOptions = new JList(); //create a list to hold the selected options
options.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
options.setVisibleRowCount(5);
options.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
optionalPanel.add(options); //add list to panel
}
private void buildTotalPanel()
{
totalPanel = new JPanel(); //create a panel to hold the total
regLabel = new JLabel("Registration Subtotal: $");
regTotal = new JTextField(10);
optLabel = new JLabel("Optional Subtotal: $");
optTotal = new JTextField(10);
total = new JTextField(10);
totLabel = new JLabel("Total: $");
regTotal.setEditable(false);
optTotal.setEditable(false);
total.setEditable(false);
totalPanel.add(regLabel);
totalPanel.add(regTotal);
totalPanel.add(optLabel);
totalPanel.add(optTotal);
totalPanel.add(totLabel);
totalPanel.add(total);
}
private class ComboBoxListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int index;
double subtotal1 = 0.00;
index = regType.getSelectedIndex(); //get selected item and calculate subtotal
if (index == 0)
subtotal1 += GENERAL_REG_FEE;
else if (index == 1)
subtotal1 += STUDENT_REG_FEE;
regTotal.setText(Double.toString(subtotal1));
}
}
private class ListListeners implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent e)
{
Object[] selections = options.getSelectedValues();
selectedOptions.setListData(selections);
double subtotal2 = 0.00;
if (options.isSelectedIndex(0))
subtotal2 = subtotal2 + E_COMMERCE;
if (options.isSelectedIndex(1))
subtotal2 = subtotal2 + FUTURE_WEB;
if (options.is(2))
subtotal2 = subtotal2 + JAVA;
if (options.isSelectedIndex(3))
subtotal2 = subtotal2 + NETWORK;
thanks ill give that a shot when i get home, the selectedOptions was just more editing i was trying to do to get the program to run, it wasn't complete or anything i was just trying any other possible thing i could have thought of. THANKS A BUNCH! hope it runs well
oh wow i completely missed that out too hahaha i cant believe i missed that simple piece, thats java for you =/ oh well
1 Answer
- tLv 510 years agoFavorite Answer
What is selectedOptions for? I don't think you need (and it made be spend about an hour trying to see why the listener I added wasn't being called).
1) Add the even listener for the JList 'options'. On last line of buildOptionalPanel() put
options.addListSelectionListener(new ListListeners());
2) Correct some tiny things:
if (options.is(2)) // should be isSelectedIndex(2) like the others
subtotal2 = subtotal2 + JAVA;
// rest got cut off, but I am assuming you have the checks for the other list items
// and also
optTotal.setText(Double.toString(subtotal2));
3) Get rid of selectedOptions
And that's it!