Results 1 to 13 of 13
  1. #1
    javamula is offline Member
    Join Date
    Sep 2009
    Posts
    22
    Rep Power
    0

    Default how to getText() properly?

    Dear All,
    Please help for the above subject.
    I have a main class JFM...which call to other class SHOWPANE to set up the program environment such as JFrame, menubar, and finally displaying the JTabbedPane which contains JPanel to collect information from the user JTextField username and JTextField password and one button Connect.
    Here is some code of SHOWPANE:
    JTabbedPane tabbedPane.addTab("Login", sp.showPane1());
    loginname = new JTextField(" ")
    loginpass = new JTextField(" ")
    After running the project, i put loginname as "AAA" and loginpass as "BBB" to access the database...when pressing the button CONNECT...which link to third class DATABASE and get empty string for var1 and var2...(which i think to get AAA and BBB
    String var1 = SHOWPANE.getLoginname();
    String var2 = SHOWPANE.getLoginpass();
    Please help how to getText() properly?

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    832
    Rep Power
    6

    Default

    put ur code and ask.

    Without attaching any listener ,how can u retreive the value from a textfield?

    Gothru this
    How to Use Text Fields (The Java™ Tutorials > Creating a GUI with JFC/Swing > Using Swing Components)
    Ramya:cool:

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    ...

    Without attaching any listener ,how can u retreive the value from a textfield?
    You just do
    Java Code:
    loginname.getText();

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    832
    Rep Power
    6

    Default

    The question is when pressing the button ,value needs to be retreived.

    1. Attach the actionListener

    2.Inside actionPerformed method

    retreive loginname.getText(); and loginpass.getText()
    Ramya:cool:

  5. #5
    javamula is offline Member
    Join Date
    Sep 2009
    Posts
    22
    Rep Power
    0

    Default getText of JTextField....

    Dear All,

    thank you for the response... please find the complete code...

    TIA
    Java Code:
    package JA1.Solution;
    
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.*;
    
    public class AFMB extends JFrame {
        FrameSetUpB fsu = new FrameSetUpB();
        showPaneB sp = new showPaneB();
        JTabbedPane tabbedPane = new JTabbedPane();
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        GridBagConstraints c2 = new GridBagConstraints();
        GridBagConstraints c5 = new GridBagConstraints();
        Container contentPane = getContentPane();
        JDialog dialog = new JDialog((Frame) null, "! DELETE CONFIRMATION !", true);
        Container dialogContentPane = dialog.getContentPane();
        JButton login = new JButton("LOGIN");
        String imagename = "steam.gif", dialogf = "LOGIN FAILED", dialogmessage;
        String sql;
        int dialogtype = JOptionPane.PLAIN_MESSAGE;
        Object[] data = new Object[5];
        DefaultTableModel defaulttablemodel = new DefaultTableModel();
        JTable jtable = new JTable(defaulttablemodel);
    
        public static void main(String args[]) 
        {
            AFMB app = new AFMB();
            app.setSize (800,800);
            app.setTitle("The Winning Team - Purple Solution");
        }
    
        public AFMB()
        {
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setup();
            pack();
            addWindowListener(new WindowEventHandler());
            show();
        }
    
        private void setup() {
            setJMenuBar(fsu.setupMenuBar());
            contentPane.add(tabbedPane, BorderLayout.CENTER);
            tabbedPane.addTab("Login", sp.showPane1());
        }
    }
    
    ----------------------------------------
    
    package JA1.Solution;
    
    import javax.swing.*;
    
    public class FrameSetUpB {
        private javax.swing.JMenuBar JmenuBar;
        private javax.swing.JMenu dbMenu;
        private javax.swing.JMenu importMenu;
        private javax.swing.JMenuItem POItem;
        private javax.swing.JMenuItem IPCItem;
        private javax.swing.JMenuItem QuoteItem;
    
        public JMenuBar setupMenuBar(){
            JmenuBar = new javax.swing.JMenuBar();
            dbMenu = new javax.swing.JMenu();
            importMenu = new javax.swing.JMenu();
            POItem = new javax.swing.JMenuItem();
            QuoteItem = new javax.swing.JMenuItem();
            IPCItem = new javax.swing.JMenuItem();
            
            dbMenu.setText("Database");
            importMenu.setText("Import");
            POItem.setText("PO");
            importMenu.add(POItem);
            QuoteItem.setSelected(true);
            QuoteItem.setText("Quote");
            importMenu.add(QuoteItem);
            IPCItem.setText("IPC");
            importMenu.add(IPCItem);
            dbMenu.add(importMenu);
            JmenuBar.add(dbMenu);
            JMenu fileMenu = new JMenu("File");
            JMenuItem fileOpen = new JMenuItem("Open");
            JMenuItem fileExit = new JMenuItem("Exit");
    
            fileExit.addActionListener(new MenuItemHandler());
            fileOpen.addActionListener(new MenuItemHandler());
            POItem.addActionListener(new MenuItemHandler());
            IPCItem.addActionListener(new MenuItemHandler());
            QuoteItem.addActionListener(new MenuItemHandler());
    
            fileMenu.add(fileOpen);
            fileMenu.add(fileExit);
            JmenuBar.add(fileMenu);
            JmenuBar.add(dbMenu);
            return  JmenuBar;
        }
    }
    
    --------------------------------------------
    
    
    package JA1.Solution;
    
    import java.awt.*;
    import javax.swing.JTextField;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.BorderFactory;
    
    public class showPaneB {
        JTextField login= new JTextField(30);
        JTextField password = new JTextField(30);
        String MYPANEL1 = " LOGIN ";
        String MYPANEL4 = " STUDENT REPORTS ";
        String MYPANEL5 = " SEARCH STUDENT DETAILS ";
        Font titleFont = new Font("courier new", Font.BOLD, 14);
        Font dataFont = new Font("courier new", Font.PLAIN, 12);
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        JLabel jLabel1, jLabel2 = new JLabel();
        JButton button = new JButton();
    
    public JPanel showPane1(){
        JPanel p1 = new JPanel(){
            @Override
            public Dimension getPreferredSize(){
            Dimension size = super.getPreferredSize();
            size.width = 785;
            size.height = 495;
            return size;
            }
        };
        p1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Login Panel"));
        p1.setFont(titleFont);
        p1.setLayout(gridbag);
        jLabel1 = new JLabel("Enter Username : ");
        jLabel1.setFont(dataFont);
        c.weighty = 0.0;
        c.ipady = 2;
        c.ipadx = 2;
        c.anchor = GridBagConstraints.WEST;
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridy = 0;
        gridbag.setConstraints(jLabel1, c);
        p1.add(jLabel1);
        login = new JTextField("      ");
        //login.addlistener???
        c.ipady = 2;
        c.ipadx = 2;
        c.weighty = 0.0;
        c.anchor = GridBagConstraints.WEST;
        c.gridwidth = 2;
        c.gridx = 1;
        c.gridy = 0;
        gridbag.setConstraints(login, c);
        p1.add(login);
        jLabel2 = new JLabel("Enter Password : ");
        jLabel2.setFont(dataFont);
        c.ipady = 2;
        c.ipadx = 2;
        c.weighty = 0.0;
        c.anchor = GridBagConstraints.WEST;
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridy = 1;
        gridbag.setConstraints(jLabel2, c);
        p1.add(jLabel2);
        password = new JTextField("       ");
        //password.addlistener???
        c.ipady = 2;
        c.ipadx = 2;
        c.weighty = 0.0;
        c.anchor = GridBagConstraints.WEST;
        c.gridwidth = 2;
        c.gridx = 1;
        c.gridy = 1;
        gridbag.setConstraints(password, c);
        p1.add(password);
        button = new JButton("LOGIN");
        c.ipady = 2;
        c.ipadx = 2;
        c.weighty = 0.0;
        c.anchor = GridBagConstraints.CENTER;
        c.gridwidth = 1;
        c.gridx = 1;
        c.gridy = 2;
        gridbag.setConstraints(button, c);
        p1.add(button);
        button.addActionListener(new ButtonHandlerB());
        return p1;
    }
    }
    
    ----------------------------------
    
    package JA1.Solution;
    import java.awt.event.*;
    
    public class ButtonHandlerB implements ActionListener{
    DatabaseB db = new DatabaseB();
    //showPane sp = new showPane();
    
    public void actionPerformed(ActionEvent ev){
        String L=ev.getActionCommand();
        if(L.equals("LOGIN")){
        //sp.katakunci.getText();
        //sp.katakunci.getText();
        db.accessDB();
        }
    }
    }
    
    --------------------------------------
    
    package JA1.Solution;
    
    import  java.sql.*;
    import javax.swing.JOptionPane;
    import java.awt.Component;
    
    public class DatabaseB{
        showPane sp = new showPane();
    
    int dialogtype = JOptionPane.PLAIN_MESSAGE;
    
    public void accessDB()
    {
            try
    	{
                // <editor-fold defaultstate="collapsed" desc="Connection">
                    String driver = "org.apache.derby.jdbc.ClientDriver";
                    String url = "jdbc:derby://localhost:1527/InsertPart";
                    ConnectionJDBC cj = new ConnectionJDBC();
                    Statement statement;// </editor-fold>
                    String var1 = sp.pemakai.getText().trim();
                    var1 =  var1.trim();
                    String var2 = sp.katakunci.getText().trim();
    					var2 =  var2.trim();
    					String sql = "SELECT * FROM login WHERE username='"+var1+"' AND password='"+var2+"'";
    			   		Connection conn = cj.makeConnection(driver, url, "Andri", "Andri123");
                                            statement = conn.createStatement();
    					boolean hasResults = statement.execute(sql);
                                            System.out.println(hasResults);
    			   		if(hasResults)
    					{
    			    		ResultSet result = statement.getResultSet();
    			    		if(result!=null)
    					{
                                                System.out.println("KKK");
                                                displayResults(result);
    			   		}
                                           conn.close();
    			  		}
    			  }
    			  catch(Exception ex)
    			  {
    			  }
    		}
    
    public void displayResults(ResultSet r) throws SQLException
                            {
                                    String sql;
      				ResultSetMetaData rmeta = r.getMetaData();
    				int foundrec = 0;
      				int numColumns = rmeta.getColumnCount();
                                    System.out.println(numColumns);
    				while(r.next())
    				{
    						String param3 = r.getString(3).trim();
    						if (param3.equals("accounts"))
    						{
    							foundrec = 1;
    							//tabbedPane.removeTabAt(0);
    							//sp.showpane2345();
    						}
    						else if(param3.equals("principal"))
    						{
    							foundrec = 1;
    							//tabbedPane.removeTabAt(0);
    							//showpane67();
    						}
      				}
                                    System.out.println(foundrec);
                                    if(foundrec==0)
    
    				{
    					String dialogmessage = "Please Re-Login";
    					dialogtype = JOptionPane.INFORMATION_MESSAGE;
    					JOptionPane.showMessageDialog((Component) null, dialogmessage, "Login Failed", dialogtype);
                                            sp.pemakai.setText("");
    					sp.katakunci.setText("");
    				}
    		}
    }
    -----------------------------------
    Last edited by Fubarable; 09-15-2009 at 04:33 PM. Reason: code tags added for readability

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    javamula, welcome to the forum. I've added code tags to aid with the readability of your code. If you consciensiously use these when posting code in the future, more will be willing to read the code. Much luck!

  7. #7
    javamula is offline Member
    Join Date
    Sep 2009
    Posts
    22
    Rep Power
    0

    Default

    Dear Fubarable - Moderator,

    Message noted and thank you for your guidance.

    for future posting, we will follow the code tags

    Sorry for any inconveniences...

    Respectfully..

  8. #8
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    832
    Rep Power
    6

    Default

    we are very very tired to read ur code.That too different variables and n number of classes.Have u tried to implement what we have given as a suggestion?

    Put debug statements.Ur code is not complete.
    Ramya:cool:

  9. #9
    javamula is offline Member
    Join Date
    Sep 2009
    Posts
    22
    Rep Power
    0

    Default

    Dear Ramya,

    Yes, i did to add the listener to those 2 JTextField...but still NULLpointerexception??

    Please help.

  10. #10
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    832
    Rep Power
    6

    Default

    Have u tried to print the value inside the actionperformed meth?

    Ur classes are specific to ur req.Not even able to compile ur code from my system.urs is
    Ramya:cool:

  11. #11
    javamula is offline Member
    Join Date
    Sep 2009
    Posts
    22
    Rep Power
    0

    Default

    yeap...try to println(login); //

    with the following response ...
    javax.swing.JTextField[,0,0,0x0,invalid,layout=javax.swing.plaf.basic.Bas i.....

    Sorry the not able to compile due to try to connect my database....I think we should disable displayResults(result); in DatabaseB.java??? :)

  12. #12
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    832
    Rep Power
    6

    Default

    Hi,
    print login.getText() and see.

    Read Sun tutorial.Why iam telling with lot of pateince is that u have posted big code.

    -Regards
    Ramya
    Ramya:cool:

  13. #13
    javamula is offline Member
    Join Date
    Sep 2009
    Posts
    22
    Rep Power
    0

    Default

    Please help further...so now the JTextField has some response if we press ENTER...
    I wish when i press the button, those 2 JTextField type by the user...to connect the table "LOGIN"..


    here the listener given to the source event in showPaneB Class:
    1.password.addActionListener(new JTextFieldHandlerB(this));
    2.login.addActionListener(new JTextFieldHandlerB(this));
    3.button.addActionListener(new ButtonHandlerB());

    --------------------------------------------------------
    Java Code:
    import java.awt.event.*;
    
    public class ButtonHandlerB implements ActionListener{
    DatabaseB db = new DatabaseB();
    
    public void actionPerformed(ActionEvent ev){
        String L=ev.getActionCommand();
        if(L.equals("LOGIN")){
        db.accessDB();
        }
    }
    }
    ----------------------------------------------------------------

    Java Code:
    import java.awt.event.*;
    
    public class JTextFieldHandlerB implements ActionListener{
    private Pane1 sp;
    String n, p;
    
    public JTextFieldHandlerB (Pane1 sp){
        this.sp = sp;
    }
    
    public void actionPerformed(ActionEvent ev){
    n=sp.setName();
    System.out.println(n);
    p=sp.setPass();
    System.out.println(p);
    }}

    ---------------------------------------

    Excerpt code from DatabaseB Class as follows.

    Java Code:
    ....
    String varName = sp2.getName();
                    varName =  varName.trim();
                    System.out.println(varName);
                    String varPass = sp2.getPass();
                    varPass =  varPass.trim();
                    System.out.println(varPass);
    					String sql = "SELECT * FROM login WHERE username='"+varName +"' AND password='"+varPass +"'";
    ...

Similar Threads

  1. Certain Chinese Characters not displayed properly.
    By kerwintang in forum Advanced Java
    Replies: 1
    Last Post: 08-21-2009, 05:55 AM
  2. Using accessors properly
    By LifeWithJava in forum New To Java
    Replies: 2
    Last Post: 12-23-2008, 02:49 PM
  3. [SOLVED] new2java- gettext settext
    By obdi in forum New To Java
    Replies: 4
    Last Post: 07-21-2008, 09:28 AM
  4. Log4j not working properly....
    By prakash_dev in forum Advanced Java
    Replies: 0
    Last Post: 03-17-2008, 12:13 PM
  5. how to properly express adding...
    By paul in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •