aspose file tools*
The moose likes I/O and Streams and the fly likes selected item from a JComboBox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "selected item from a JComboBox" Watch "selected item from a JComboBox" New topic
Author

selected item from a JComboBox

Freya Tan
Ranch Hand

Joined: Apr 14, 2004
Posts: 45
i having problem getting the selected item from a JComboBox, to save it in a text file.. i'm not sure which & what coding i have to use.. Can anyone help me.. Urgent..
Thanks alot..
[ April 18, 2004: Message edited by: Ernest Friedman-Hill ]
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31079
    
163

Freya,
What do you have so far?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Freya Tan
Ranch Hand

Joined: Apr 14, 2004
Posts: 45
Thank for replying..
This is what i have so far... there are some error here & there which i'm not sure how to solve them.. can u kindly point out to me.. Thanks alot

import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;

class EnterNew extends JFrame implements ActionListener, ItemListener {


//delcare arrayList
ArrayList AppList = new ArrayList();
EnterNew NewEntry;

private static final int FRAME_WIDTH = 420;
private static final int FRAME_HEIGHT = 470;
private static final int FRAME_X_ORIGIN = 280;
private static final int FRAME_Y_ORIGIN = 140;

private static final int BUTTON_WIDTH = 70;
private static final int BUTTON_HEIGHT = 30;

private JButton doneButton;
private JButton clearButton;
private JButton backButton;


// introduce label, list box and text field
private JLabel date ;
private JLabel time ;
private JLabel text ;
private JLabel Appointment ;
private JTextField timeInput ;
private JTextArea appInput ;
private JTextField dayInput;
private JTextField monthInput ;
private JTextField yearInput;
private JComboBox am;
private JLabel image ;



public static void main (String[] args) {
EnterNew frame = new EnterNew();
frame.setVisible (true);
}


public EnterNew (){

try {
readAppointment();
}
catch (IOException a) {
}


Container contentPane = getContentPane ();
// set the frame properties
setSize (FRAME_WIDTH, FRAME_HEIGHT);
setResizable (false);
setTitle ("Appointment Entry");
setLocation (FRAME_X_ORIGIN, FRAME_Y_ORIGIN);


// set the content pane properties
contentPane.setLayout (null);
contentPane.setBackground (Color.pink);



// Add 1st images
image = new JLabel (new ImageIcon("appEntry.jpg"));
image.setBounds (10,20,390,50);
contentPane.add (image);

// Add 2nd images
image = new JLabel (new ImageIcon("2.gif"));
image.setBounds (-40,85,390,50);
contentPane.add (image);

// Add 3rd images
image = new JLabel (new ImageIcon("5.gif"));
image.setBounds (62,85, 390,50);
contentPane.add (image);



// Add Label
date = new JLabel ();
date.setText ("Date");
date.setBounds (33,178,150,25);
contentPane.add (date);



// Add 3 TextFields
text = new JLabel ();
text.setText ("* Please enter all the field");
text.setBounds (33,140,150,25);
contentPane.add (text);

dayInput = new JTextField ();
dayInput.setBounds (127,178,30,20);
contentPane.add (dayInput);

monthInput = new JTextField ();
monthInput .setBounds (196,178,80,20);
contentPane.add (monthInput);

yearInput = new JTextField ();
yearInput.setBounds (306,178,40,20);
contentPane.add (yearInput);



// Add 1st label, text field and action listener
time = new JLabel ();
time.setText ("Time");
time.setBounds (33,225,150,25);
contentPane.add (time);
timeInput = new JTextField ();
timeInput.setBounds (127,225,50,20);
contentPane.add (timeInput);
timeInput.addActionListener (this);



//Add Time List
String[] list = {"AM","PM"};
am = new JComboBox (list);
am.setBounds (196,225,50,20);
contentPane.add (am);


// Add 2nd label, text field and action listener
Appointment = new JLabel ();
Appointment.setText ("Appointment");
Appointment.setBounds (33,270,150,25);
contentPane.add (Appointment);
appInput = new JTextArea ();
appInput.setBounds (127,270,240,80);
contentPane.add (appInput);


// create and place 3 buttons on the frame's content pane
doneButton = new JButton ("Done");
doneButton.setBounds (66,378, BUTTON_WIDTH, BUTTON_HEIGHT);
contentPane.add (doneButton);
backButton = new JButton ("Back");
backButton.setBounds (278,378, BUTTON_WIDTH, BUTTON_HEIGHT);
contentPane.add (backButton);
clearButton = new JButton ("Clear");
clearButton.setBounds (172,378, BUTTON_WIDTH, BUTTON_HEIGHT);
contentPane.add (clearButton);

clearButton.addActionListener(new ActionListener() {




public void actionPerformed(ActionEvent e) {

appInput.setText("");
timeInput.setText("");
dayInput.setText("");
monthInput.setText("");
yearInput.setText("");

}
});
contentPane.add (clearButton);

// register this frame as an action listener of the 3 buttons
doneButton.addActionListener (this);
clearButton.addActionListener (this);
backButton.addActionListener (this);
}



public void addData(String da, String mo, String ye, double ti, String am, String ap ) throws IOException {

App type = new App(da,mo,ye,ti,am,ap);
AppList.add(type);

File out = new File("Appointment.txt");

FileOutputStream output = new FileOutputStream(out);
PrintStream NewApp = new PrintStream(output);

for (int i = 0; i < AppList.size(); i++) {
type = (App)AppList.get(i);
NewApp.println(type.getDate() + ";" + type.getMonth() + ";" + type.getYear() + ";" + type.getYear() + ";" + type.getam() + ";" + type.getApp()); <-------- error
}

output.close();


dayInput.setText("");
monthInput.setText("");
yearInput.setText("");
timeInput.setText("");
am.setSelectedItem( "" ); <-------- error
appInput.setText("");

dayInput.requestFocus();
}



// actionPerformed is invoked when a Button is clicked
public void actionPerformed(ActionEvent e)
{

// when doneButton is clicked
if(e.getSource() == doneButton){
try {

String da = dayInput.getText();
String mo = monthInput.getText();
String ye = yearInput.getText();
double ti = Double.parseDouble(timeInput.getText());
String am = am.getSelectedItem(); <--------- Error
String ap = appInput.getText();
addData(da, mo, ye, ti, am, ap);
AppointmentPage();
}

catch (NumberFormatException nfe ) {

JOptionPane.showMessageDialog(this," You must enter an integer time " );
timeInput.setText("");
timeInput.requestFocus();
}
catch (IOException io) {

System.err.println(" Error writing to file\n" +
io.toString());
System.exit(1);
}

}

// when backButton is clicked
if(e.getSource() == backButton){
appointment();
}
}



public void readAppointment() throws IOException {

boolean ofa = false;
String inputStr = "";
App Newapp;

File inputFile = new File("Appointment.txt");

FileInputStream in = new FileInputStream(inputFile);
InputStreamReader reader = new InputStreamReader(in);
BufferedReader fileInput = new BufferedReader(reader);

while (!ofa) {
try {

inputStr=fileInput.readLine();
if (inputStr == null)
ofa = true;

else {

Newapp = new App(inputStr,";");
AppList.add(Newapp);

}
}

catch (EOFException e) {

ofa = true;
}
catch (IOException e) {

ofa = true;
}
catch (NoSuchElementException e) {

ofa = true;
}

}

in.close();
}



private void AppointmentPage(){

AppointmentPage AppointmentPageFrame = new AppointmentPage();
AppointmentPageFrame.setSize(420,470);
AppointmentPageFrame.setTitle("Appointment Page");
AppointmentPageFrame.setVisible(true);
this.setVisible(false);
}
private void clearForm(){

dayInput.setText("");
timeInput.setText("");
appInput.setText("");
monthInput.setText("");
yearInput.setText("");
}

private void appointment(){

Appointment appointmentFrame = new Appointment();
appointmentFrame.setSize(420,470);
appointmentFrame.setTitle("Appointment");
appointmentFrame.setVisible(true);
this.setVisible(false);


// register 'Exit upon closing' as a default close operation
setDefaultCloseOperation (EXIT_ON_CLOSE);

}


public void actionPerform (ActionEvent event){
if (event.getSource () instanceof JButton){
JButton clickedButton = (JButton) event.getSource();
String buttonText = clickedButton.getText();
setTitle ("You clicked " + buttonText);
} else { // the event source is inputLine
setTitle ("You entered '" + dayInput.getText() + "'");
}
}
}
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31079
    
163

Freya,
Looking quickly, I notice that the variable "am" refers to both a combobox and a string. This is probably causing some errors. In Java, the one declared with the closest scope is the one used. You can resolve this problem easily by using different variable names.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: selected item from a JComboBox