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.

Save record from JComboBox

mamat294mamat294 Posts: 1Member
i want to save the value inside the no of child to "guests.txt" ;) below is the coding:

import javax.swing.*;
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.File;
import java.util.Scanner;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.*;
import javax.swing.JComboBox;
import javax.swing.JFrame;

public class assignment extends JFrame// inherits from the JFrame class
{
// static variables to hold frame dimensions in pixels
private static final int WIDTH = 500;
private static final int HEIGHT = 400;

private JLabel cbColor, idLabel, nameLabel, addressLabel, paymentLabel, nationalityLabel, flightVehicleLabel, adultLabel, childLabel, checkinLabel, checkoutLabel, noofdaysLabel, roomtypeLabel;
private JTextField cbColorComboBox, cbColorTextField, cbColor1TextField, cbColor2TextField, idTextField, nameTextField, addressTextField, paymentTextField, nationalityTextField, flightVehicleTextField, adultTextField, childTextField, checkinTextField, checkoutTextField, noofdaysTextField;
private JButton acceptB, clearB;
String noOfAdults;


// declare event handlers
private AcceptButtonHandler abHandler;
private ClearButtonHandler cbHandler;

public assignment() // constructor for GUI
{
setTitle( "New Guests" ); // set the title of the frame
//setSize( 450, 450 ); // set the frame size
setSize(550, 550);
centerFrame(WIDTH, HEIGHT);

// prepare the container
Container pane = getContentPane(); // get content pane
GridLayout aGrid = new GridLayout( 14, 10 ); // create a 5-row, 2-column layout
pane.setLayout( aGrid ); // set the layout for the pane

// instantiate JLabel objects
JRadioButton Suite,Standard;
//JFrame frame = new JFrame("Creating a JRadioButton Component");
JPanel panel = new JPanel();
JPanel buttonPanel = new JPanel();
ButtonGroup buttonGroup = new ButtonGroup();

idLabel = new JLabel( " ID: ");
nameLabel = new JLabel( " Name: " );
addressLabel = new JLabel( " Address (street, city, state, zip): " );
nationalityLabel = new JLabel(" Nationality: ");
flightVehicleLabel = new JLabel (" Flight No/Vehicle No: ");
adultLabel = new JLabel (" No of Adults: ");
childLabel = new JLabel (" No of Childs: ");
checkinLabel = new JLabel (" Check In Date: ");
checkoutLabel = new JLabel (" Check Out Date: ");
noofdaysLabel = new JLabel (" No of Days: ");
roomtypeLabel = new JLabel (" Type of Room: ");
paymentLabel = new JLabel(" Payment: " );

// instantiate JTextField objects
idTextField = new JTextField( 2 ); // 2 spaces in the text field
nameTextField = new JTextField( 50 ); // 50 spaces in the text field
addressTextField = new JTextField( 50 ); // 50 spaces in the text field
nationalityTextField = new JTextField ( 25 );
flightVehicleTextField = new JTextField ( 20 );
adultTextField = new JTextField ( 2 );
cbColorTextField = new JTextField ( 2 );
cbColor1TextField = new JTextField ( 2 );
cbColor2TextField = new JTextField ( 2 );
childTextField = new JTextField ( 2 );
checkinTextField = new JTextField ( 15 );
checkoutTextField = new JTextField ( 15 );
noofdaysTextField = new JTextField ( 2 );
paymentTextField = new JTextField( 12 );
//SuiteJRadioButton = new JRadioButton(20);
//StandardJRadioButton = new JRadioButton(20);
//JFrame frame = new JFrame("Creating a JRadioButton Component");
//JPanel panel = new JPanel();


// instantiate button and handler, register handler
acceptB = new JButton( "Accept" );
abHandler = new AcceptButtonHandler(); // create button handler
acceptB.addActionListener( abHandler ); // register handler with button

// instantiate button and handler, register handler
clearB = new JButton( "Clear" );
cbHandler = new ClearButtonHandler(); // create button handler
clearB.addActionListener( cbHandler ); // register handler with button


// add components to the pane
pane.add( idLabel );
pane.add( idTextField );
pane.add( nameLabel );
pane.add( nameTextField );
pane.add( addressLabel );
pane.add( addressTextField );
pane.add( nationalityLabel);
pane.add( nationalityTextField );
pane.add( flightVehicleLabel );
pane.add( flightVehicleTextField );
pane.add( adultLabel);
//pane.add( adultTextField );
//add(Box.createHorizontalStrut(2));
JComboBox cbColor = new JComboBox();
cbColor.addItem("1");
cbColor.addItem("2");
add(cbColor);
pane.add( childLabel);
//pane.add( childTextField );
JComboBox cbColor1 = new JComboBox();
cbColor1.addItem("0");
cbColor1.addItem("1");
cbColor1.addItem("2");
add(cbColor1);
pane.add( checkinLabel );
pane.add( checkinTextField );
pane.add( checkoutLabel );
pane.add( checkoutTextField );
pane.add( noofdaysLabel );
pane.add( noofdaysTextField );
pane.add( roomtypeLabel );
//pane.add(buttonPanel, BorderLayout.SOUTH);
//Suite = new JRadioButton("Suite");
//Standard = new JRadioButton("Standard");
//buttonGroup.add(Suite);
//buttonGroup.add(Standard);
//Suite.setSelected(true);
//buttonPanel.add(Suite);
//buttonPanel.add(Standard);
cbColor2.addItem("Suite");
cbColor2.addItem("Standard");
add(cbColor2);
//If buttonGroup = Suite, then paymentTextField = "300";
pane.add( paymentLabel );
pane.add( paymentTextField );
pane.add( acceptB );
pane.add( clearB );

//contentpane.add(comboBox1, BorderLayout.NORTH);
} // end constructor


public void centerFrame(int frameWidth, int frameHeight) {

Toolkit aToolkit = Toolkit.getDefaultToolkit();
Dimension screen = aToolkit.getScreenSize();

int xPostionOfFrame = (screen.width - frameWidth) / 2;
int yPositionOfFrame = (screen.height - frameHeight) / 2;

setBounds(xPostionOfFrame, yPositionOfFrame, frameWidth, frameHeight);

}

// inner class for button handler
private class AcceptButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// declare I/O objects
File outFile;
FileWriter aFileWriter;
PrintWriter aPrintWriter = null;




// place data in string
//String outputStr = anID + ", " + aName + ", " + anAddress + ", "
// + aNationality + ", " + aflightVehicle + ", " + anAdult + ", " + aChild + ", " + aCheckin + ", " + aCheckout + ", " + aNoofdays + ", " + aPayment + "
";


PrintWriter outputStream = null;
try {
outputStream = new PrintWriter(new FileOutputStream ("guests.txt", true));
}
catch (IOException error)
{
System.out.println("Error opening file. Program exits!");
System.exit(0);
}

// get data from text fields
String anID = idTextField.getText();
String aName = nameTextField.getText();
String anAddress = addressTextField.getText();
String aNationality = nationalityTextField.getText();
String aflightVehicle = flightVehicleTextField.getText();
String anAdult = adultTextField.getText();
String aChild = childTextField.getText();
String aCheckin = checkinTextField.getText();
String aCheckout = checkoutTextField.getText();
String aNoofdays = noofdaysTextField.getText();
String aPayment = paymentTextField.getText();
//String acbColor = box.getSelectedItem();
String acbColor = cbColorTextField.getText();
String acbColor1 = cbColor1TextField.getText();
String acbColor2 = cbColor2TextField.getText();

//String acbColor = cbColor.getText();
outputStream.println(anID + ", " + aName + ", "+ anAddress + ", " + aNationality + ", " + aflightVehicle + ", " + acbColor + ", " + acbColor1 + ", " + acbColor2 + ", " + anAdult + ", " + aChild + ", " + aCheckin + ", " + aCheckout + ", " + aNoofdays + ", " + aPayment+ "

"); //open valve

outputStream.close();
System.out.println("New Guests Saved to guests.txt File");


try {
outputStream = new PrintWriter(new FileOutputStream ("deposit.txt", true));
}
catch (IOException error)
{
System.out.println("Error opening file. Program exits!");
System.exit(0);
}
outputStream.println(anID + ", " + aName + ", " + aPayment+ "

"); //open valve
outputStream.close();
System.out.println("New Guests Saved to deposit.txt File");
} // end method
} // end class

// inner class for Clear button handler
private class ClearButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// set text fields to empty strings
idTextField.setText( "" );
nameTextField.setText( "" );
addressTextField.setText( "" );
nationalityTextField.setText( "" );
flightVehicleTextField.setText ( "" );
adultTextField.setText( "" );
childTextField.setText( "" );
checkinTextField.setText( "" );
checkoutTextField.setText ( "" );
noofdaysTextField.setText ( "" );
paymentTextField.setText( "" );
}
} // end class


public static void main( String[] args ) throws IOException // declare main method
{
JFrame aBankAppGuiFive = new assignment(); // create the frame object
aBankAppGuiFive.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
aBankAppGuiFive.setVisible( true ); // display the frame
String acbColor[] = { "A", "B", "C", "D","E", "F", "G", "H","I", "J" };


} // end main

} // end class

Sign In or Register to comment.