Sponsored by:

Java GUI Programming Help?

Java GUI Programming Help?

Postby treasigh » Tue Dec 20, 2011 8:02 am

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…
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(ListSelecti…
options.setVisibleRowCount(5);
options.setBorder(BorderFactory.crea… 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(su…
}
}

private class ListListeners implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent e)
{
Object[] selections = options.getSelectedValues();
selectedOptions.setListData(selecti…
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;
treasigh
 
Posts: 812
Joined: Sat Apr 02, 2011 6:10 am
Top

Java GUI Programming Help?

Postby treasigh » Tue Dec 20, 2011 8:22 am

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(subtot…

3) Get rid of selectedOptions

And that's it!
treasigh
 
Posts: 812
Joined: Sat Apr 02, 2011 6:10 am
Top


Return to Class Action

 


  • Related topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: calin, crosbey, gustav, jorel, osbourne, regenweald, spengler and 0 guests