Member Avatar for sakura_fujin

Hi. I'm new with Java's GUI Components and I'm having a hard time with my program
design right now. My program deals with manipulating database elements (i.e. fields
and records) and I'm stuck with asking the user to add a new record into the
database. My program originally works in the command line interface but when I
redesigned it to GUI, I got stuck since I'm not that familiar with the implementation.
The program initially does not know how many fields there is in the database. It
first asks the user to enter a textfile before calculating it. So when the file
is read and when the user clicks on the add button, a new frame will appear, asking
for new field values per field. I already had set up the frame but my problem is
how can I get the user-inputted values from the textFields and place them into
my database table? I always get null field values.. My problem lies somewhere
in this part of the code:

int n = dbHeaders.size();
values = new String[dbHeaders.size()];

addFrame=new JFrame("Add new record");
addFrame.setLayout(new BorderLayout());
addFrame.setSize(400,100);
addFrame.setVisible(true);

xPanel=new JPanel();
xPanel.setLayout(new GridLayout(n,1,4,4));

for(int i=0; i<dbHeaders.size();i++){
    labels=new JLabel("Please enter "+dbHeaders.get(i)+": ");
    fields=new JTextField(15);
    xPanel.add(labels);
    xPanel.add(fields);
    values[i]=fields.getText();//something's wrong here...                
    addFrame.add(xPanel,BorderLayout.NORTH);
}

yPanel=new JPanel();
yPanel.setLayout(new FlowLayout());
aOk=new JButton("OK");
yPanel.add(aOk);
aOk.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event){
        for(int j=0; j<values.length;j++){
            String string=values[j];
            System.out.println("fields: "+string);
            //when i check in this part, string is always null...
        }
        record = new DBRecord(dbHeaders, values);
        dbTable.add(record);
        JOptionPane.showMessageDialog(null, "Record added.");
                    }
}); 

..And does anybody knows how to implement the "reset" button?

thanks in advance.

sakura

Recommended Answers

All 5 Replies

Can you please post whole code and maybe text file that you try to read as given snipplet doesn't show any problems.
Reset button is as any other button in Java it is the functionality that you assign to it that matters. Be more specific of what you expect from it and we can help you

Member Avatar for sakura_fujin

here's the code.. it's kinda long and scattered though.. i haven't organized everything yet...

public class DatabaseDriver {

    private JFrame main = new JFrame("Main Menu");//main menu
    private JFrame frame;
    private JFrame addFrame;
    private JFrame updateFrame;
    private JFrame printFrame;
    private JPanel panel1,panel2;
    private JPanel aPanel,bPanel,cPanel;
    private JPanel xPanel,yPanel,zPanel;
    private JPanel uPanel1,uPanel2,uPanel3;
    private JPanel pPanel1, pPanel2,pPanel3;
    private JLabel label1,label2;
    private JLabel labels;
    private JLabel uLabel1, uLabel2, uLabel3,uLabel4,uLabel5,uLabel6;
    private JLabel pLabels1,pLabels2;
    private JButton exit,add,update,delete,search,save,print,avl;    
    private JTextField anInput;
    private JTextField fields;
    private JTextField uTextField1, uTextField2, uTextField3;
    private JButton ok, cancel;
    private JButton aOk,aReset, aBack;
    private JButton uOk,uReset,uBack;
    private JButton pBack;

    private static DBRecord record;
    private static DBTable dbTable;
    static String[] values;
    static int a=0,b=0;
    private static ArrayList<String> dbHeaders;

    public DatabaseDriver(){
        //set up welcome message
        JOptionPane.showMessageDialog(null, "Welcome to the Database Manipulation Program!");
        String name=JOptionPane.showInputDialog("What is your name, user?");
        frame=new JFrame("Welcome");
        frame.setLayout(new BorderLayout());
        aPanel=new JPanel();
        aPanel.setLayout(new FlowLayout());
        label2=new JLabel("Hello, "+name+" !");
        aPanel.add(label2);
        frame.add(aPanel, BorderLayout.NORTH);

        bPanel=new JPanel();
        bPanel.setLayout(new FlowLayout());
        label1=new JLabel("Please enter the filename of your database: ");
        anInput=new JTextField(15);
        bPanel.add(label1);
        bPanel.add(anInput);
        frame.add(bPanel, BorderLayout.CENTER);

        cPanel=new JPanel(new FlowLayout());
        ok=new JButton("OK");
        cancel=new JButton("Cancel");
        cPanel.add(ok);
        cPanel.add(cancel);

        frame.add(cPanel, BorderLayout.SOUTH);
        frame.getContentPane().setBackground(Color.CYAN);
        frame.setSize(300,150);
        frame.setVisible(true);

        ReadHandler readHandler=new ReadHandler();
        ok.addActionListener(readHandler);//press ok button to read file
        ExitHandler exitHandler=new ExitHandler();
        cancel.addActionListener(exitHandler);//press cancel to exit program

        //set up main menu GUI interface
        main.setLayout(new BorderLayout());

        add=new JButton("Add");
        update=new JButton("Update");
        delete=new JButton("Delete");
        search=new JButton("Search");
        avl=new JButton("Create AVL");
        print=new JButton("Print");
        save=new JButton("Save changes");
        exit=new JButton("Exit");

        panel1=new JPanel();
        panel1.setLayout(new BorderLayout());
        label1=new JLabel("                 What do you want to do?");
        panel1.add(label1);
        panel1.setBackground(Color.ORANGE);
        main.add(panel1,BorderLayout.CENTER);

        panel2 = new JPanel();
        panel2.setLayout(new GridLayout(8,1,4,4));
        panel2.setBackground(Color.ORANGE);

        panel2.add(add);
        AddHandler aHandler = new AddHandler();
        add.addActionListener(aHandler);

        panel2.add(update);
        UpdateHandler updateHandler=new UpdateHandler();
        update.addActionListener(updateHandler);

        panel2.add(delete);
        DeleteHandler deleteHandler=new DeleteHandler();
        delete.addActionListener(deleteHandler);

        panel2.add(search);
        SearchHandler searchHandler=new SearchHandler();
        search.addActionListener(searchHandler);

        panel2.add(avl);
        CreateHandler createHandler=new CreateHandler();
        avl.addActionListener(createHandler);

        panel2.add(print);
        PrintHandler printHandler=new PrintHandler();
        print.addActionListener(printHandler);

        panel2.add(save);
        SaveHandler saveHandler=new SaveHandler();
        save.addActionListener(saveHandler);

        panel2.add(exit);
        ExitHandler eHandler=new ExitHandler();
        exit.addActionListener(eHandler);

        main.add(panel2, BorderLayout.EAST);

        main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        main.setSize(350,200);
    }

    //register button listeners 
    private class ReadHandler implements ActionListener{
        public void actionPerformed(ActionEvent event){
            String line;
            String nextLine;
            String[] value;
            String s = anInput.getText();
            dbHeaders=new ArrayList<String>();
            try{
                FileReader fr = new FileReader(s);
                BufferedReader br = new BufferedReader(fr);
                line = br.readLine();
                String[]headers =line.split(",");
                for(String string: headers)
                    dbHeaders.add(string);
                dbTable =new DBTable(dbHeaders);
                while((nextLine = br.readLine()) != null){
                    value = nextLine.split(",");
                    record = new DBRecord(dbHeaders, value);
                    dbTable.add(record);
                }
            }
            catch(FileNotFoundException e){
                System.err.println("File Not Found");
            }
            catch(IOException io){
                System.err.println("Error reading file!");
            }

            frame.setVisible(false);
            main.setVisible(true);
        }
    }

    private class AddHandler implements ActionListener{

        public void actionPerformed(ActionEvent event){
            //try{
                main.setVisible(false);

                int n = dbHeaders.size();
                values = new String[dbHeaders.size()];

                addFrame=new JFrame("Add new record");
                addFrame.setLayout(new BorderLayout());
                addFrame.setSize(400,100);
                addFrame.setVisible(true);

                xPanel=new JPanel();
                xPanel.setLayout(new GridLayout(n,1,4,4));

                //String line="";

                for(int i=0; i<dbHeaders.size();i++){
                    labels=new JLabel("Please enter "+dbHeaders.get(i)+": ");
                    fields=new JTextField(15);
                    xPanel.add(labels);
                    xPanel.add(fields);

                    addFrame.add(xPanel,BorderLayout.NORTH);
                    //line=JOptionPane.showInputDialog("Please enter "+dbHeaders.get(i)+": ");
                }

                yPanel=new JPanel();
                yPanel.setLayout(new FlowLayout());
                aOk=new JButton("OK");
                yPanel.add(aOk);
                aOk.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent event){
                        for(int j=0; j<values.length;j++){
                            String string=values[j];
                            System.out.println("fields: "+string);
                        }
                        record = new DBRecord(dbHeaders, values);
                        dbTable.add(record);
                        JOptionPane.showMessageDialog(null, "Record added.");
                    }
                });

                aReset=new JButton("RESET");
                yPanel.add(aReset);
                addFrame.add(yPanel,BorderLayout.CENTER);

                zPanel=new JPanel();
                zPanel.setLayout(new BorderLayout());
                aBack=new JButton("Back to Main Menu");
                zPanel.add(aBack,BorderLayout.EAST);
                addFrame.add(zPanel,BorderLayout.SOUTH);
                addFrame.pack();
                aBack.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent event){
                        addFrame.setVisible(false);
                        main.setVisible(true);
                    }
                });



            //}
            //catch(IOException io){
                //System.err.println("Bad input");
            //}
        }
    }

    private class UpdateHandler implements ActionListener{

        public void actionPerformed(ActionEvent event){

            main.setVisible(false);
            updateFrame=new JFrame("Update record");
            updateFrame.setLayout(new FlowLayout());
            updateFrame.setSize(300,200);
            updateFrame.setVisible(true);

            int n=dbHeaders.size();

            uPanel1=new JPanel();
            uPanel1.setLayout(new FlowLayout());
            uLabel1=new JLabel("Please enter record number: ");
            uPanel1.add(uLabel1);
            uTextField1=new JTextField(10);
            uPanel1.add(uTextField1);
            uLabel2=new JLabel("*record number starts from 0 onwards.");
            uPanel1.add(uLabel2);
            uLabel2.setFont(new Font("Serif", Font.ITALIC,12));
            String str=uTextField1.getText();;
            a=Integer.parseInt(str);
            uLabel3=new JLabel("What field do you want to update in "+dbTable.get(a)+"?");
            uPanel1.add(uLabel3);
            updateFrame.add(uPanel1);

            uPanel2=new JPanel();
            uPanel2.setLayout(new GridLayout(n,1,3,3));
            for(int i=0; i<dbHeaders.size();i++){
                uLabel4=new JLabel("["+i+"]"+ dbHeaders.get(i));
                uPanel2.add(uLabel4);
                updateFrame.add(uPanel2);
            }

            uPanel3=new JPanel();
            uPanel3.setLayout(new FlowLayout());
            uLabel5=new JLabel("Please enter your choice:");
            uTextField2=new JTextField(10);
            String str2=uTextField2.getText();
            b=Integer.parseInt(str2);
            record = dbTable.get(a);
            String[] values = record.toString().split(",");
            uLabel6=new JLabel("Please enter your new value to replace "+values[b]);
            uTextField3=new JTextField(15);
            uOk=new JButton("OK");
            uOk.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    record.put(dbHeaders.get(b), uTextField3.getText());
                }
            });
            uBack=new JButton("Back to Main Menu");
            uPanel3.add(uLabel5);
            uPanel3.add(uTextField2);
            uPanel3.add(uLabel6);
            uPanel3.add(uTextField3);
            uPanel3.add(uOk);
            uPanel3.add(uBack);
            updateFrame.add(uPanel3);
            updateFrame.pack();

            uBack.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    updateFrame.setVisible(false);
                    main.setVisible(true);
                }
            });

        }
    }

    private class DeleteHandler implements ActionListener{
        public void actionPerformed(ActionEvent event){
            dbTable.deleteRecord();
        }
    }

    private class SearchHandler implements ActionListener{
        public void actionPerformed(ActionEvent event){
            dbTable.searchRecord();
        }
    }

    private class CreateHandler implements ActionListener{
        public void actionPerformed(ActionEvent event){
            dbTable.createAVL();
        }
    }

    private class PrintHandler implements ActionListener{
        public void actionPerformed(ActionEvent event){
            main.setVisible(false);
            printFrame=new JFrame("Database");
            printFrame.setLayout(new BorderLayout());
            printFrame.setSize(200,150);
            printFrame.setVisible(true);

            int m=dbHeaders.size();
            int n=dbTable.size();

            pPanel1=new JPanel();
            pPanel1.setLayout(new FlowLayout());
            for(int i=0;i<m;i++){
                pLabels1=new JLabel(dbHeaders.get(i));
                pPanel1.add(pLabels1);
                printFrame.add(pPanel1, BorderLayout.NORTH);
            }

            pPanel2=new JPanel();
            pPanel2.setLayout(new GridLayout(n,1,3,3));
            for(int j=0; j<n;j++){
                pLabels2=new JLabel(dbTable.get(j).toString());
                pPanel2.add(pLabels2);
                printFrame.add(pPanel2,BorderLayout.CENTER);
            }

            pPanel3=new JPanel();
            pPanel3.setLayout(new GridLayout(1,1,4,4));
            pBack=new JButton("Back to Main Menu");
            pBack.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    printFrame.setVisible(false);
                    main.setVisible(true);
                }
            });
            pPanel3.add(pBack);
            printFrame.add(pPanel3,BorderLayout.SOUTH);
            printFrame.pack();
            printFrame.setResizable(true);
        }
    }

    private class SaveHandler implements ActionListener{
        public void actionPerformed(ActionEvent event){
            dbTable.saveChanges();
        }
    }


    private class ExitHandler implements ActionListener{
        public void actionPerformed(ActionEvent event){
            System.exit(1);
        }
    }

    //main program
    public static void main(String[] args)throws IOException{
        new DatabaseDriver();
    }

here's a sample content of the file/database

StudNum, Name, Exer1, Exer2, Exer3
99-60088, jpatino, 88, 90, 90
05-17955, kgo, 78, 80, 76
08-12345, jgo, 90, 78, 60
05-45678, aty, 78, 67, 80
06-34567, aco, 90, 89, 94
98-67890, byao, 69, 77, 66
00-99999, tyu, 56, 65, 86
01-23422, wuy, 43, 23, 20
92-00023, ptio, 78, 88, 65
95-00100, llim, 90, 45, 85
Member Avatar for sakura_fujin

oh and about the reset button, i want to implement it like that of forms wherein if the user wants to cancel the inputs he typed in, the inputs would be erased by clicking the reset button... does it help if i call the constructor again? but i think it'll just open another bunch of main menu frames...

Member Avatar for sakura_fujin

actually i still have problems with regards to this project of mine, GUI is really not the priority, il post a new thread with regards to it, tnx

actually i still have problems with regards to this project of mine, GUI is really not the priority, il post a new thread with regards to it, tnx

And use code tags! Both here and in the other post, you have posted a huge wall of code without any formatting, which no one wants to wade through. Please read the announcement at the top of the forum and use code tags as requested.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.