Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Java GUI Help

maakshifmaakshif Posts: 2Member
I have 2 problems with a GUI program I need some help with please. I'm doing a kind of invoice GUI for a car care company (this is a class project not for a real company) and I can't get the layout exactly as I want it and I'm having a problem with a loop.

Problem #1: I'm using grid layout on a JPane set to WEST on BorderLayout and I have the Frame set to be maximized and the jtextfields are too big. I have to have a JTextArea which is set to the CENTER of the BorderLayout in order to show the invoice. (For now I have a JOptionPane dialog showing the info because printing to JTextArea is for another part of the project.) I want to have First Name, Last Name, City, and State on one row; Invoice Number, Invoice Date, Storage Unit, and Months on the next row; Vin, Make, Model, and Year on the next row; and Deployed Military check box, and the button group on the last row.

Problem #2: I'm trying to write a loop to check if one of the radio buttons in my button group is selected so it can add to the invoice what type of payment is selected. If no button is selected I want a joptionpane message to pop up telling the user to select a payment type. (I have this code written and it works up to this point. The code is a while loop that is commented out about 3/4 of the way down.) After the user clicks OK in the dialog box I can't get the program to allow the user to select the payment type. It just goes ahead and runs the program the rest of the way through. Also, we are not allowed to use exception handling in this part of the project.

If I need to break the code into segments I will.

[code]
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.ImageIcon;
import java.text.NumberFormat;
import javax.swing.JOptionPane;
import java.awt.Dimension;


public class StorageInvoiceGUI extends JFrame {

//Declare GUI components
private JTextField jtfFirstName;
private JTextField jtfLastName;
private JTextField jtfCity;
private JComboBox jcbState;
private JTextField jtfInvoiceNumber;
private JTextField jtfInvoiceDate;
private JTextField jtfStorageUnit;
private JTextField jtfVin;
private JTextField jtfMake;
private JTextField jtfModel;
private JTextField jtfYear;
private JTextField jtfMonths;
private JCheckBox jchkDeployedMilitary;
private JRadioButton jrbCash;
private JRadioButton jrbCheck;
private JRadioButton jrbCreditCard;
private JRadioButton jrbInvisible;
private JLabel jlblFirstName;
private JLabel jlblLastName;
private JLabel jlblCity;
private JLabel jlblState;
private JLabel jlblStorageUnit;
private JLabel jlblInvoiceNumber;
private JLabel jlblInvoiceDate;
private JLabel jlblVin;
private JLabel jlblMake;
private JLabel jlblModel;
private JLabel jlblYear;
private JLabel jlblMonths;
private JLabel jlblTitle;
private JTextArea jtaDisplay;
private JPanel entryPanel;
private JPanel buttonPanel;
private JPanel titlePanel;
private JPanel displayPanel;

private JButton jbtDisplayInvoice;
private JButton jbtClearAll;

// Instantiate and customize JFrame
public static void main(String[] args) {
JFrame frame = new StorageInvoiceGUI();
frame.setTitle("Storage Invoice");
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
frame.setLocation( 0, 0 );
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible( true );

}


//GUI constructor
public StorageInvoiceGUI() {
setLayout(new BorderLayout(10,5));

// Create Entry Panel
jlblFirstName = new JLabel( "First Name: ", JLabel.CENTER );
jtfFirstName = new JTextField();
jlblLastName = new JLabel( "Last Name: ", JLabel.CENTER );
jtfLastName = new JTextField();
jlblCity = new JLabel( "City: ", JLabel.CENTER );
jtfCity = new JTextField();
jlblState = new JLabel( "State: ", JLabel.CENTER );
String[] states = {"AL", "FL", "GA", "KY", "ME", "NH", "NY", "PA", "RI", "TN", "VA", "VT"};
jcbState = new JComboBox(states);
jlblInvoiceNumber = new JLabel( "Invoice Number: ", JLabel.CENTER );
jtfInvoiceNumber = new JTextField();
jlblInvoiceDate = new JLabel( "Invoice Date: ", JLabel.CENTER );
jtfInvoiceDate = new JTextField();
jlblStorageUnit = new JLabel( "Storage Unit: ", JLabel.CENTER );
jtfStorageUnit = new JTextField();
jlblVin = new JLabel( "Vin: ", JLabel.CENTER );
jtfVin = new JTextField();
jlblMake = new JLabel( "Make: ", JLabel.CENTER );
jtfMake = new JTextField();
jlblModel = new JLabel( "Model: ", JLabel.CENTER );
jtfModel = new JTextField();
jlblYear = new JLabel( "Year: ", JLabel.CENTER );
jtfYear = new JTextField();
jlblMonths = new JLabel( "Months Stored:", JLabel.CENTER);
jtfMonths = new JTextField();
jchkDeployedMilitary = new JCheckBox("Deployed Military", false);
jrbCash = new JRadioButton("Cash");
jrbCheck = new JRadioButton("Check");
jrbCreditCard = new JRadioButton("Credit Card");
jrbInvisible = new JRadioButton("None Selected");
jrbInvisible.setVisible(false);
entryPanel = new JPanel();
entryPanel.setLayout( new GridLayout(4, 2, 5, 5) );

// make jTextArea uneditable and add to displayPanel
jtaDisplay = new JTextArea();
jtaDisplay.setEditable( false );
displayPanel = new JPanel();
displayPanel.setLayout( new BorderLayout( ) );

// create buttonPanel
jbtDisplayInvoice = new JButton( "Display Invoice" );
jbtClearAll = new JButton( "Clear All" );
buttonPanel = new JPanel();

// create titlePanel
ImageIcon imageIcon = new ImageIcon("MCCC.gif");
jlblTitle = new JLabel( "Middlebury Classic Car Care" ,imageIcon, JLabel.CENTER );
jlblTitle.setFont(new Font("Serif", Font.BOLD, 48));
jlblTitle.setForeground(Color.BLUE);



// Add radio buttons to a logical group so only one can be selected at a time
ButtonGroup bg = new ButtonGroup();
bg.add( jrbCash );
bg.add( jrbCheck );
bg.add( jrbCreditCard );
bg.add( jrbInvisible );


// add GUI components to entryPanel
entryPanel.add( jlblFirstName );
entryPanel.add( jtfFirstName );
entryPanel.add( jlblLastName );
entryPanel.add( jtfLastName );
entryPanel.add( jlblCity );
entryPanel.add( jtfCity );
entryPanel.add( jlblState );
entryPanel.add( jcbState );
entryPanel.add( jlblInvoiceNumber );
entryPanel.add( jtfInvoiceNumber );
entryPanel.add( jlblInvoiceDate );
entryPanel.add( jtfInvoiceDate );
entryPanel.add( jlblStorageUnit );
entryPanel.add( jtfStorageUnit );
entryPanel.add( jlblMonths );
entryPanel.add( jtfMonths );
entryPanel.add( jlblVin );
entryPanel.add( jtfVin );
entryPanel.add( jlblMake );
entryPanel.add( jtfMake );
entryPanel.add( jlblModel );
entryPanel.add( jtfModel );
entryPanel.add( jlblYear );
entryPanel.add( jtfYear );
entryPanel.add( jchkDeployedMilitary );
entryPanel.add( jrbCash );
entryPanel.add( jrbCheck );
entryPanel.add( jrbCreditCard );
entryPanel.add( jrbInvisible );

// Set tool tip text
jtfFirstName.setToolTipText("Enter First Name here");
jtfLastName.setToolTipText("Enter Last Name here");
jtfCity.setToolTipText("Enter your City here");
jcbState.setToolTipText("Select your State");
jtfInvoiceNumber.setToolTipText("Enter your Invoice Number here");
jtfInvoiceDate.setToolTipText("Enter your Invoice Date here mm/dd/yyyy or mmm/dd/yyyy");
jtfStorageUnit.setToolTipText("Enter your Storage Unit here");
jtfVin.setToolTipText("Enter vehicle's VIN here");
jtfMake.setToolTipText("Enter vehicle's Make here");
jtfModel.setToolTipText("Enter vehicle's Model here");
jtfYear.setToolTipText("Enter vehicle's Year here");
jtfMonths.setToolTipText("Enter number of Months stored here");

// Set Label Foreground
jlblFirstName.setForeground(Color.BLUE);
jlblLastName.setForeground(Color.BLUE);
jlblCity.setForeground(Color.BLUE);
jlblState.setForeground(Color.BLUE);
jlblInvoiceNumber.setForeground(Color.BLUE);
jlblInvoiceDate.setForeground(Color.BLUE);
jlblStorageUnit.setForeground(Color.BLUE);
jlblVin.setForeground(Color.BLUE);
jlblMake.setForeground(Color.BLUE);
jlblModel.setForeground(Color.BLUE);
jlblYear.setForeground(Color.BLUE);
jlblMonths.setForeground(Color.BLUE);
jchkDeployedMilitary.setForeground(Color.RED);
jrbCash.setForeground(Color.RED);
jrbCheck.setForeground(Color.RED);
jrbCreditCard.setForeground(Color.RED);
jrbInvisible.setForeground(Color.RED);


// Set component borders
jtfFirstName.setBorder( BorderFactory.createLineBorder( Color.BLACK, 2 ) );
jtfLastName.setBorder( BorderFactory.createLineBorder( Color.BLACK, 2 ) );
jtfCity.setBorder( BorderFactory.createLineBorder( Color.BLACK, 2 ) );
jcbState.setBorder( BorderFactory.createLineBorder( Color.BLACK, 2 ) );
jtfInvoiceNumber.setBorder( BorderFactory.createLineBorder( Color.BLACK, 2 ) );
jtfInvoiceDate.setBorder( BorderFactory.createLineBorder( Color.BLACK, 2 ) );
jtfStorageUnit.setBorder( BorderFactory.createLineBorder( Color.BLACK, 2 ) );
jtfVin.setBorder( BorderFactory.createLineBorder( Color.BLACK, 2 ) );
jtfMake.setBorder( BorderFactory.createLineBorder( Color.BLACK, 2 ) );
jtfModel.setBorder( BorderFactory.createLineBorder( Color.BLACK, 2 ) );
jtfYear.setBorder( BorderFactory.createLineBorder( Color.BLACK, 2 ) );
jtfMonths.setBorder( BorderFactory.createLineBorder( Color.BLACK, 2 ) );
jtaDisplay.setBorder( BorderFactory.createLineBorder( Color.BLACK, 4 ) );

// Set button mnemonics
jbtDisplayInvoice.setMnemonic(KeyEvent.VK_D);
jbtClearAll.setMnemonic(KeyEvent.VK_C);

// Add GUI components to displayPanel
displayPanel.add( jtaDisplay );

//Add GUI components to buttonPanel
buttonPanel.add( jbtDisplayInvoice );
buttonPanel.add( jbtClearAll );

//Add titlePanel
add(jlblTitle, BorderLayout.NORTH);

//Add JTextArea
add(displayPanel, BorderLayout.CENTER);

// Add entryPanel to the frame
add(entryPanel, BorderLayout.WEST);

//Add buttonPanel to the frame
add(buttonPanel, BorderLayout.SOUTH);

//Register Display Invoice Button
ActionListener display = new DisplayInvoiceListener();
jbtDisplayInvoice.addActionListener( display );

// Register Clear All Button
jbtClearAll.addActionListener( new ClearAllListener() );


} // End GUI Constructor

// DisplayInvoice
private class DisplayInvoiceListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String First = jtfFirstName.getText();
String Last = jtfLastName.getText();
String City = jtfCity.getText();
String State = (String)jcbState.getSelectedItem();
String Vin = jtfVin.getText();
String Make = jtfMake.getText();
String Model = jtfModel.getText();
String Year = jtfYear.getText();
String InvoiceNumber = jtfInvoiceNumber.getText();
String InvoiceDate = jtfInvoiceDate.getText();
String StorageUnit = jtfStorageUnit.getText();
String aString = jtfMonths.getText();
int Months = Integer.parseInt(aString);
boolean DeployedMilitary = jchkDeployedMilitary.isSelected();
String PaymentMethod = "";
boolean payment = false;
boolean selected = false;






/*{
while(payment == false) {

if (jrbCash.isSelected()) {
payment = true;
PaymentMethod = "Cash";
}

else if (jrbCheck.isSelected()) {
payment = true;
PaymentMethod = "Check";
}

else if (jrbCreditCard.isSelected()){
payment = true;
PaymentMethod = "Credit Card";
}

else{
payment = false;
JOptionPane.showMessageDialog( null, "Please Select a Payment Type", null, JOptionPane.WARNING_MESSAGE );
break;

}

}

}*/



String output = "";
StorageInvoice.setMonthlyRate(150.00);

//instantiate clients
Client client1 = new Client( First, Last, City, State);


//instantiate vehicles
Vehicle owner1 = new Vehicle ( Vin, Make, Model, Year, client1);

//instantiate invoice
StorageInvoice invoice1 = new StorageInvoice(InvoiceNumber, InvoiceDate, owner1, StorageUnit, Months, DeployedMilitary);

// use the Invoice reference variables to call methods on the subclass objects
// convert total due to currency format with dollar signs, commas, and 2 places past decimal
double bill = Math.random() * 1000;
bill = invoice1.computeBill();
NumberFormat nf = NumberFormat.getCurrencyInstance();
output += invoice1.toString() + "Total Due: " + nf.format(bill) + "
" + "*********************************" + "
" + "Payment will be made by " + PaymentMethod;

//Message Dialog box showing the output
JOptionPane.showMessageDialog( null, output,"Storage Invoice", JOptionPane.INFORMATION_MESSAGE );



} // end actionPerformed

}// end DisplayInvoiceListener class


// ClearAll
private class ClearAllListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
jtfFirstName.setText("");
jtfLastName.setText("");
jtfCity.setText("");
jcbState.setSelectedItem("AL");
jtfInvoiceNumber.setText("");
jtfInvoiceDate.setText("");
jtfStorageUnit.setText("");
jtfVin.setText("");
jtfMake.setText("");
jtfModel.setText("");
jtfMonths.setText("");
jtfYear.setText("");
jchkDeployedMilitary.setSelected(false);
jrbCash.setSelected(false);
jrbCheck.setSelected(false);
jrbCreditCard.setSelected(false);
jrbInvisible.setSelected(true);

} // end actionPerformed

}// end ClearListener class




} // End StorageInvoiceGUI Class
[/code]
Sign In or Register to comment.