Results 1 to 13 of 13
Thread: how to getText() properly?
- 09-15-2009, 03:23 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 22
- Rep Power
- 0
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?
- 09-15-2009, 03:28 PM #2
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:
- 09-15-2009, 03:49 PM #3
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 09-15-2009, 03:54 PM #4
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:
- 09-15-2009, 04:29 PM #5
Member
- Join Date
- Sep 2009
- Posts
- 22
- Rep Power
- 0
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
-
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!
- 09-15-2009, 04:46 PM #7
Member
- Join Date
- Sep 2009
- Posts
- 22
- Rep Power
- 0
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..
- 09-15-2009, 04:54 PM #8
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:
- 09-15-2009, 04:57 PM #9
Member
- Join Date
- Sep 2009
- Posts
- 22
- Rep Power
- 0
Dear Ramya,
Yes, i did to add the listener to those 2 JTextField...but still NULLpointerexception??
Please help.
- 09-15-2009, 05:06 PM #10
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 isRamya:cool:
- 09-15-2009, 05:14 PM #11
Member
- Join Date
- Sep 2009
- Posts
- 22
- Rep Power
- 0
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??? :)
- 09-15-2009, 05:16 PM #12
Hi,
print login.getText() and see.
Read Sun tutorial.Why iam telling with lot of pateince is that u have posted big code.
-Regards
RamyaRamya:cool:
- 09-16-2009, 05:45 AM #13
Member
- Join Date
- Sep 2009
- Posts
- 22
- Rep Power
- 0
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
-
Certain Chinese Characters not displayed properly.
By kerwintang in forum Advanced JavaReplies: 1Last Post: 08-21-2009, 05:55 AM -
Using accessors properly
By LifeWithJava in forum New To JavaReplies: 2Last Post: 12-23-2008, 02:49 PM -
[SOLVED] new2java- gettext settext
By obdi in forum New To JavaReplies: 4Last Post: 07-21-2008, 09:28 AM -
Log4j not working properly....
By prakash_dev in forum Advanced JavaReplies: 0Last Post: 03-17-2008, 12:13 PM -
how to properly express adding...
By paul in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:08 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks