import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
// user defined type where we put name

    class riot2{
        private String name;
        public riot2(String _name){
            name = _name;   
            }
    public String toString(){
            return name;
                }

        }

            public class finalriot2 extends JFrame{
                Container c = getContentPane();
                private Stack sstack; // built in stack of java.util

                private JLabel lName;
                private JTextField fName; 
                private JTextArea outputArea;
                private JButton bAdd, bRemove, bSearch, bEmpty;

                private FileOutputStream fout; // used for file output
                private FileInputStream fin; // used for file input


                    public finalriot2() throws IOException{
                        // title
                        super("Stack(LIFO)"); 

                        sstack = new Stack();

                            try{
                                // if A file already exist and have some data read it
                                fin = new FileInputStream("Data.dat");
                                BufferedReader br = new BufferedReader(new InputStreamReader(fin));

                                String sName = br.readLine();
                                String sMobile = br.readLine();
                                String sEmail = br.readLine();



                                    while (sName != null){
                                        // Read from file and store them in linked List's object
                                        sstack.add(new riot2(sName));

                                        sName = br.readLine();

                                    }
                                    fin.close();
                                }


                                    catch(Exception err){
                                        // if no file create a new file
                                        fout = new FileOutputStream("Data.dat");
                                    }   

                            lName.setBounds(128,34,100,65);
                                // set graphical user interface
                                    Container c = getContentPane();
                                    c.setLayout(new FlowLayout());
                                        lName.setBounds(128,34,100,65);
                                    lName = new JLabel("Enter a String");
                                    c.add(lName);
                                    fName = new JTextField(20);
                                    c.add(fName);



                                    bAdd = new JButton("Push");
                                    c.add(bAdd);
                                    bAdd.addActionListener( // add event
                                        new ActionListener(){
                                        public void actionPerformed(ActionEvent e){
                                                        StringBuffer str = new StringBuffer();
                                                        str.append(fName.getText());
                                                lName.setText( sstack.push(fName.getText()) +  "" );    
                                                            // if name field is empty
                                                        if (str.length() == 1){                     
                                                            JOptionPane.showMessageDialog(null, "Name Field must be initialized", "Info", JOptionPane.INFORMATION_MESSAGE);
                                                            return; // if name field empty
                                                        }
                                                        update(); // show final result to outputArea
                                                        diskFileUpdate(); // store information to disk      
                                                    }
                                                }
                                            );  





                                            bRemove = new JButton("Pop");
                                            c.add(bRemove);
                                            bRemove.addActionListener(


                                            new ActionListener(){


                                            public void actionPerformed(ActionEvent e){
                                                StringBuffer str = new StringBuffer();
                                                        str.append(fName.getText());

                                            try{
                                                outputArea.setText("Popped:" );

                                            } catch (EmptyStackException exception){
                                            outputArea.setText(exception.toString());
                                            }
                                                update(); // show final result to outputArea
                                                        diskFileUpdate(); // store information to disk      

                                                        }
                                            }
                                            );


                                    bSearch = new JButton("Peek");
                                    c.add(bSearch);
                                    bSearch.addActionListener( // add event
                                        new ActionListener(){
                                        public void actionPerformed(ActionEvent e){
                                        StringBuffer str = new StringBuffer();
                                        str.append(fName.getText());

                                        try{
                                                outputArea.setText("Top:" + sstack.peek());
                                            }
                                             catch (EmptyStackException exception){
                                        outputArea.setText(exception.toString());
                                            }


                                                    }
                                                }
                                            );  

                                    bEmpty = new JButton("Is Empty?");
                                    c.add(bEmpty);
                                    bSearch.addActionListener( // add event
                                        new ActionListener(){
                                        public void actionPerformed(ActionEvent e){

                            String searchKey = fName.getText();
                            int result = sstack.search(searchKey);

                            if(result == -1)
                                outputArea.setText(searchKey + " not found");
                            else
                            outputArea.setText(searchKey + " found at element " + result);



                                                    }
                                                }
                                            );  



                                                                // show the data to outputArea
                                                                outputArea = new JTextArea(20, 40);
                                                                outputArea.setEditable(false);
                                                                c.add(new JScrollPane(outputArea));




                                                                        setSize(1000,500);
                                                                        setVisible(true);
                                                                    }



                                                                        private void update(){
                                                                            // I used Iterator to access stack's object
                                                                            Iterator it = sstack.iterator();
                                                                            String out=" "; // it used for collecting all members
                                                                            int count = 0;


                                                                                while (it.hasNext()){
                                                                                    Object element = it.next();
                                                                                    out += "\n" + count + "";
                                                                                    out += "  ";

                                                                                    out += element.toString();
                                                                                    ++count;
                                                                                }       
                                                                                outputArea.setText(out); 
                                                                            }


    private void diskFileUpdate(){


                                                                                        try{
                                                                                            // store to disk file
                                                                                            fout = new FileOutputStream("Data.dat");
                                                                                            // StringBuffer used to access every character
                                                                                            StringBuffer out = new StringBuffer("");
                                                                                            char c;
                                                                                            Iterator it = sstack.iterator();


                                                                                                while (it.hasNext()){
                                                                                                    Object element = it.next();
                                                                                                    out.append(element.toString());
                                                                                                }
                                                                                                // write file every character by character


                                                                                                    for (int i = 0; i < out.length(); ++i){
                                                                                                        c = out.charAt(i);
                                                                                                        fout.write(c);
                                                                                                    }
                                                                                                    fout.close();
                                                                                                }


                                                                                                    catch(Exception err){
                                                                                                        JOptionPane.showMessageDialog(null, "Error Caught: " + err, "Error", JOptionPane.ERROR_MESSAGE);



                                                                                                    }
    }




                                                                                                    public static void main(String args[]){


                                                                                                            try{
                                                                                                                finalriot window = new finalriot();
                                                                                                                window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                                                                                            }


                                                                                                                catch(Exception e){
                                                                                                                    JOptionPane.showMessageDialog(null, "Error" + e, "Error", JOptionPane.ERROR_MESSAGE);
                                                                                                                }
                                                                                                            }
                                                                                                    }

Edited 3 Years Ago by Reverend Jim: Fixed formatting

firstly, use Code tags, make your post readable.
secondly: what exactly do you mean, output your JButton in a JTextField??

describe what you want it to do and what it is(n't) doing

This article has been dead for over six months. Start a new discussion instead.