GUI with arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • javatech007
    New Member
    • Nov 2007
    • 51

    GUI with arrays

    I am trying to save data into an array from JTextFields but dont know how to go about it. I have a number of textfields to enter information into it. I dont know where to start with how to use arrays with gui. Any help?

    Code so far

    [CODE:JAVA]
    import java.awt.*;
    import java.awt.event. *;
    import java.io.*;
    import javax.swing.*;

    public class TimetableAdmin extends JFrame implements ActionListener
    {

    private JTextField routeTxt, rTime1Txt, rTime2Txt, rTime3Txt;
    private JTextfield route2Txt, r2Time1Txt, r2Time2Txt, r2Time3Txt;
    private JLabel routeLbl, timeLbl;

    private JButton saveBtn, backBtn;

    private JPanel timetablePanel, buttonsPanel;

    private Container c;

    public TimetableAdmin( )
    {
    super("Timetabl e Admin");

    Container c = getContentPane( );
    FlowLayout flow = new FlowLayout();
    c.setLayout(flo w);


    timetablePanel = new JPanel();


    buttonsPanel = new JPanel();

    setSize(500, 400);


    routeTxt = new JTextField(10);
    rTime1Txt = new JTextField(5);
    rTime2Txt = new JTextField(5);
    rTime3Txt = new JTextField(5);

    routeLbl = new JLabel("Route:" );
    timeLbl = new JLabel("Dep. Time:");

    saveBtn = new JButton ("Save");
    saveBtn.addActi onListener(this );
    backBtn = new JButton ("Back");
    backBtn.addActi onListener(this );

    timetablePanel. add(routeLbl);
    timetablePanel. add(routeTxt);
    timetablePanel. add(timeLbl);
    timetablePanel. add(rTime1Txt);
    timetablePanel. add(rTime2Txt);
    timetablePanel. add(rTime3Txt);
    timetablePanel. add(routeLbl);
    timetablePanel. add(route2Txt);
    timetablePanel. add(timeLbl);
    timetablePanel. add(r2Time1Txt) ;
    timetablePanel. add(r2Time2Txt) ;
    timetablePanel. add(r2Time3Txt) ;


    buttonsPanel.ad d(saveBtn);
    buttonsPanel.ad d(backBtn);

    c.add(timetable Panel);
    c.add(buttonsPa nel);

    setVisible(true );


    }

    public static void main(String args[])
    {
    TimetableAdmin timetableadmin = new TimetableAdmin( );

    }

    public void actionPerformed (ActionEvent e)
    {

    }

    }
    [/JAVA]
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Originally posted by javatech007
    I am trying to save data into an array from JTextFields but dont know how to go about it. I have a number of textfields to enter information into it. I dont know where to start with how to use arrays with gui. Any help?

    Code so far

    [CODE:JAVA]
    import java.awt.*;
    import java.awt.event. *;
    import java.io.*;
    import javax.swing.*;

    public class TimetableAdmin extends JFrame implements ActionListener
    {

    private JTextField routeTxt, rTime1Txt, rTime2Txt, rTime3Txt;
    private JTextfield route2Txt, r2Time1Txt, r2Time2Txt, r2Time3Txt;
    private JLabel routeLbl, timeLbl;

    private JButton saveBtn, backBtn;

    private JPanel timetablePanel, buttonsPanel;

    private Container c;

    public TimetableAdmin( )
    {
    super("Timetabl e Admin");

    Container c = getContentPane( );
    FlowLayout flow = new FlowLayout();
    c.setLayout(flo w);


    timetablePanel = new JPanel();


    buttonsPanel = new JPanel();

    setSize(500, 400);


    routeTxt = new JTextField(10);
    rTime1Txt = new JTextField(5);
    rTime2Txt = new JTextField(5);
    rTime3Txt = new JTextField(5);

    routeLbl = new JLabel("Route:" );
    timeLbl = new JLabel("Dep. Time:");

    saveBtn = new JButton ("Save");
    saveBtn.addActi onListener(this );
    backBtn = new JButton ("Back");
    backBtn.addActi onListener(this );

    timetablePanel. add(routeLbl);
    timetablePanel. add(routeTxt);
    timetablePanel. add(timeLbl);
    timetablePanel. add(rTime1Txt);
    timetablePanel. add(rTime2Txt);
    timetablePanel. add(rTime3Txt);
    timetablePanel. add(routeLbl);
    timetablePanel. add(route2Txt);
    timetablePanel. add(timeLbl);
    timetablePanel. add(r2Time1Txt) ;
    timetablePanel. add(r2Time2Txt) ;
    timetablePanel. add(r2Time3Txt) ;


    buttonsPanel.ad d(saveBtn);
    buttonsPanel.ad d(backBtn);

    c.add(timetable Panel);
    c.add(buttonsPa nel);

    setVisible(true );


    }

    public static void main(String args[])
    {
    TimetableAdmin timetableadmin = new TimetableAdmin( );

    }

    public void actionPerformed (ActionEvent e)
    {

    }

    }
    [/JAVA]
    Just read first the action-events, have some experiments and embed...

    Algorithm:Exper iment....

    After all initialization have made,
    Add an actionListeners to you textfields...
    After you have it, call the method were the manipulation of the values(converti on) that exists in the textfield will be transfered in the array....

    When you press 'enter', the actionListener will be triggered and do what is inside of it ( the statements ( method calling ))....

    Sukatoa...

    Comment

    Working...