import java.awt.*;
public class sampleComboBox extends Frame {
public sampleComboBox() {
setSize(400,150);
show();
Choice ch = new Choice();
//ch.addItemListener(this);
ch.addItem("First ");
ch.addItem("Second");
ch.addItem("Third ");
ch.addItem("Fourth");
ch.addItem("Fifth ");
setLayout(null);
ch.setBounds(150,100,80,50);
add(ch);
}
public static void main(String[] args) {
new Choices();
}
}
Sample java FileOutputStream
import java.io.*;
class TestFilePrintWriter1 {
public static void main (String[] args) throws IOException{
File outFile = new File ("PrintDat.data");// "PrinDat.data" --> filename
FileOutputStream outStream = new FileOutputStream(outFile);
PrintWriter outWriter = new PrintWriter(outStream );
String nameis = "This is a sample String";
outWriter.println(nameis);
outWriter.println("age is 21");
outWriter.println("gender : Male");
outWriter.println('A');
outWriter.println(true);
outWriter.close();
}
}
class TestFilePrintWriter1 {
public static void main (String[] args) throws IOException{
File outFile = new File ("PrintDat.data");// "PrinDat.data" --> filename
FileOutputStream outStream = new FileOutputStream(outFile);
PrintWriter outWriter = new PrintWriter(outStream );
String nameis = "This is a sample String";
outWriter.println(nameis);
outWriter.println("age is 21");
outWriter.println("gender : Male");
outWriter.println('A');
outWriter.println(true);
outWriter.close();
}
}
Posted by
Jade Isaiah Romano
0
comments
Using FileInputstream and FileOutputStream in GUI (JAVA sample code)
this is a sample code on how to use FileInputStream and Fileoutput Stream in java.
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
class Registration extends JFrame implements ActionListener, ItemListener{
JFrame Register = new JFrame("Registration");
String myGender;
Label namelbl = new Label("Name :");
TextField nametxt = new TextField();
Label agelbl = new Label("Age :");
TextField agetxt = new TextField();
Label SelectGenderlbl = new Label("Gender :");
Choice SelectGender = new Choice();
TextArea myInput = new TextArea();
Button btnSave = new Button("Save");
Button btnOpen = new Button("Open");
TextArea viewfiletxt = new TextArea();
public Registration(){
Register.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Register.setSize(400,300);
Register.setResizable(false);
Register.setBackground(Color.white);
Register.setVisible(true);
ActionListener listener;
Container content = Register.getContentPane();
//name
namelbl.setBounds(10,30,50,20);
namelbl.setVisible(true);
content.add(namelbl);
nametxt.setBounds(70,30,100,20);
nametxt.setVisible(true);
content.add(nametxt);
//age
agelbl.setBounds(10,55,50,20);
agelbl.setVisible(true);
content.add(agelbl);
agetxt.setBounds(70,55,50,20);
agetxt.setVisible(true);
content.add(agetxt);
//gender
SelectGenderlbl.setBounds(10,80,50,20);
SelectGenderlbl.setVisible(true);
content.add(SelectGenderlbl);
SelectGender.addItem(" ");
SelectGender.addItem("Male ");
SelectGender.addItem("Female");
SelectGender.addItemListener(this);
SelectGender.setBounds(70,80,80,20);
content.add(SelectGender);
//textarea
btnSave.setBounds(30,105,50,20);
btnSave.setVisible(true);
btnSave.addActionListener(this);
content.add(btnSave);
btnOpen.setBounds(85,105,50,20);
btnOpen.setVisible(true);
btnOpen.addActionListener(this);
content.add(btnOpen);
//textarea
myInput.setBounds(10,150,150,100);
myInput.setVisible(true);
content.add(myInput);
viewfiletxt.setBounds(200,30,150,50);
viewfiletxt.setVisible(true);
content.add(viewfiletxt);
}//public Registration
public void itemStateChanged(ItemEvent event){
myGender = SelectGender.getSelectedItem();
}//end itemStateChanged
public void actionPerformed(ActionEvent event){
JOptionPane msgbox = new JOptionPane();
//msgbox.showMessageDialog(null,"Select your gender","Error",JOptionPane.ERROR_MESSAGE);
String Name, Age , Gender;
if (event.getActionCommand().equals("Save")){
try{
File outFile = new File ("Registration.txt");
FileOutputStream outStream = new FileOutputStream(outFile);
//ObjectOutputStream outWriter = new ObjectOutputStream (outStream );
PrintWriter outWriter = new PrintWriter(outStream );
Name = nametxt.getText();
Age = agetxt.getText();
Gender = myGender;
String Register = "My Name is: " + Name + "\nI'm " + Age + "\nIm a " + Gender;
myInput.setText(String.valueOf(Register));
//outWriter.writeObject(persons);
outWriter.println(Register);
outWriter.close();
}//try
catch(IOException e){
}
catch(NullPointerException e){
}
}//Save
if (event.getActionCommand().equals("Open")){
FileReader fr;
try {
//BufferedReader br = new BufferedReader( new FileReader(new File("Person.txt")));
fr = new FileReader (new File("Registration.txt"));
BufferedReader br = new BufferedReader (fr);
String line = br.readLine();
while (line != null) {
viewfiletxt.setText(String.valueOf(line));
line = br.readLine();
}
br.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}//end actionPerformed
public static void main(String args[]) {
Registration main = new Registration();
}//main
}//class Registration
this code is just my work.
if you have any comments or suggestion on how to make this code more easy feel free to email me.
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
class Registration extends JFrame implements ActionListener, ItemListener{
JFrame Register = new JFrame("Registration");
String myGender;
Label namelbl = new Label("Name :");
TextField nametxt = new TextField();
Label agelbl = new Label("Age :");
TextField agetxt = new TextField();
Label SelectGenderlbl = new Label("Gender :");
Choice SelectGender = new Choice();
TextArea myInput = new TextArea();
Button btnSave = new Button("Save");
Button btnOpen = new Button("Open");
TextArea viewfiletxt = new TextArea();
public Registration(){
Register.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Register.setSize(400,300);
Register.setResizable(false);
Register.setBackground(Color.white);
Register.setVisible(true);
ActionListener listener;
Container content = Register.getContentPane();
//name
namelbl.setBounds(10,30,50,20);
namelbl.setVisible(true);
content.add(namelbl);
nametxt.setBounds(70,30,100,20);
nametxt.setVisible(true);
content.add(nametxt);
//age
agelbl.setBounds(10,55,50,20);
agelbl.setVisible(true);
content.add(agelbl);
agetxt.setBounds(70,55,50,20);
agetxt.setVisible(true);
content.add(agetxt);
//gender
SelectGenderlbl.setBounds(10,80,50,20);
SelectGenderlbl.setVisible(true);
content.add(SelectGenderlbl);
SelectGender.addItem(" ");
SelectGender.addItem("Male ");
SelectGender.addItem("Female");
SelectGender.addItemListener(this);
SelectGender.setBounds(70,80,80,20);
content.add(SelectGender);
//textarea
btnSave.setBounds(30,105,50,20);
btnSave.setVisible(true);
btnSave.addActionListener(this);
content.add(btnSave);
btnOpen.setBounds(85,105,50,20);
btnOpen.setVisible(true);
btnOpen.addActionListener(this);
content.add(btnOpen);
//textarea
myInput.setBounds(10,150,150,100);
myInput.setVisible(true);
content.add(myInput);
viewfiletxt.setBounds(200,30,150,50);
viewfiletxt.setVisible(true);
content.add(viewfiletxt);
}//public Registration
public void itemStateChanged(ItemEvent event){
myGender = SelectGender.getSelectedItem();
}//end itemStateChanged
public void actionPerformed(ActionEvent event){
JOptionPane msgbox = new JOptionPane();
//msgbox.showMessageDialog(null,"Select your gender","Error",JOptionPane.ERROR_MESSAGE);
String Name, Age , Gender;
if (event.getActionCommand().equals("Save")){
try{
File outFile = new File ("Registration.txt");
FileOutputStream outStream = new FileOutputStream(outFile);
//ObjectOutputStream outWriter = new ObjectOutputStream (outStream );
PrintWriter outWriter = new PrintWriter(outStream );
Name = nametxt.getText();
Age = agetxt.getText();
Gender = myGender;
String Register = "My Name is: " + Name + "\nI'm " + Age + "\nIm a " + Gender;
myInput.setText(String.valueOf(Register));
//outWriter.writeObject(persons);
outWriter.println(Register);
outWriter.close();
}//try
catch(IOException e){
}
catch(NullPointerException e){
}
}//Save
if (event.getActionCommand().equals("Open")){
FileReader fr;
try {
//BufferedReader br = new BufferedReader( new FileReader(new File("Person.txt")));
fr = new FileReader (new File("Registration.txt"));
BufferedReader br = new BufferedReader (fr);
String line = br.readLine();
while (line != null) {
viewfiletxt.setText(String.valueOf(line));
line = br.readLine();
}
br.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}//end actionPerformed
public static void main(String args[]) {
Registration main = new Registration();
}//main
}//class Registration
this code is just my work.
if you have any comments or suggestion on how to make this code more easy feel free to email me.
Posted by
Jade Isaiah Romano
0
comments
Subscribe to:
Posts (Atom)