Results 1 to 2 of 2
  1. #1
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Unhappy .getText() stopped working?

    Okey, I know that the coding is shit. But could someone tell me why .getText() has stoped working?

    I'll try to explain my problem better after i paste 2 classes:
    Java Code:
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.*;
    import java.awt.*;
    public class GUI extends JFrame {
        private JLabel lb1,lb12,lb13,lb2,lb3,lb4;
        private JPanel cPanel,p1,p1center,p2,p3,p4;
        public static JTextField t1,inputLibCard,ISBN; // Here is inputLibCard
        public static JTextField borrowers,books;
        private CardLayout c1;
        private int currentCard=1;
        public static String tmp,tmp2;
        private Lib lib;
        private Book b;
        private JButton bbooks,bborrow,cbutton,ISBNadd;
        public static String path;
    
    
    
    public GUI(){
            super("Library System V.1");
            setLayout(new FlowLayout());
            cPanel = new JPanel();
             c1 = new CardLayout();
            cPanel.setLayout(c1);
            t1 = new JTextField(10);
            bbooks= new JButton("Browse");
            bborrow = new JButton("Browse");
            cbutton = new JButton("Check Card");
            ISBNadd = new JButton("add book");
            borrowers = new JTextField(10);
            books = new JTextField(10);
            inputLibCard = new JTextField(10); // creating field !
            ISBN = new JTextField(10);
            p1 = new JPanel();
    
            p2 = new JPanel();
            p3 = new JPanel();
            p4 = new JPanel();
            lb1 = new JLabel("Enter the database you wish to use:");
            lb12 = new JLabel("Enter path to Books.txt:");
            lb13 = new JLabel("Enter path to Borrowers.txt:");
            lb2 = new JLabel("Enter your library card number:");
            lb3 = new JLabel("Enter ISBN for the books you want to borrow (One at the time): ");
            lb4 = new JLabel(" ");
            
            p1.add(lb1,BorderLayout.EAST);
            p1.add(t1,BorderLayout.CENTER);
            p1.add(lb12,BorderLayout.EAST);
            p1.add(books,BorderLayout.CENTER);
            p1.add(bbooks,BorderLayout.WEST);
            p1.add(lb13,BorderLayout.EAST);
            p1.add(borrowers,BorderLayout.CENTER);
            p1.add(bborrow,BorderLayout.WEST);
            p2.add(lb2,BorderLayout.EAST);
            p2.add(inputLibCard, BorderLayout.CENTER); //adding it to JPanel
            p2.add(cbutton, BorderLayout.WEST);
            p3.add(lb3,BorderLayout.EAST);
            p3.add(ISBN, BorderLayout.CENTER);
            p3.add(ISBNadd, BorderLayout.WEST);
            p4.add(lb4);
            
            cPanel.add(p1, "1");
            cPanel.add(p2, "2");
            cPanel.add(p3, "3");
            cPanel.add(p4, "4");
            JPanel buttonPanel = new JPanel();
            JButton b1 = new JButton("Previous");
            JButton b2 = new JButton("Next");
            buttonPanel.add(b1);
            buttonPanel.add(b2);
    
              bbooks.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent arg0) {
           JFileChooser fc = new JFileChooser();
            fc.setAcceptAllFileFilterUsed(false);
            fc.setDialogTitle("Select your BOOKS text file");
            fc.setFileFilter(new javax.swing.filechooser.FileNameExtensionFilter("txt files", "txt"));
            int resultat = fc.showOpenDialog(null);
    
            if (resultat != JFileChooser.APPROVE_OPTION){
                JOptionPane.showMessageDialog(null, "No file was choosen.");
                System.exit(0);
            }
           String filnamn = fc.getSelectedFile().getAbsolutePath();
    
    books.setText(filnamn);
    
    
    
                }
            });
             bborrow.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent arg0) {
                           JFileChooser fc = new JFileChooser();
            fc.setAcceptAllFileFilterUsed(false);
            fc.setDialogTitle("Select your Borrowers text file");
            fc.setFileFilter(new javax.swing.filechooser.FileNameExtensionFilter("txt files", "txt"));
            int resultat = fc.showOpenDialog(null);
    
            if (resultat != JFileChooser.APPROVE_OPTION){
                JOptionPane.showMessageDialog(null, "No file was choosen.");
                System.exit(0);
            }
           String filnamn2 = fc.getSelectedFile().getAbsolutePath();
    
    borrowers.setText(filnamn2);
    
                }
            });
    
            b1.addActionListener(new ActionListener() {
                //Bakåt
                public void actionPerformed(ActionEvent arg0) {
                    if (currentCard > 1) {
                        currentCard -= 1;
                        c1.show(cPanel, "" + (currentCard));
                    }
    
                }
            });
           
            cbutton.addActionListener(new ActionListener() {    // Actionlistener for input libcard
                //Bakåt
                public void actionPerformed(ActionEvent arg0) {
                    
                    tmp = inputLibCard.getText();              // HERE it gives me NOTHING!
                    JOptionPane.showMessageDialog(null,"nr: "+ tmp);
                    Database.findUser();
                    
    
                }
            });
             ISBNadd.addActionListener(new ActionListener() {
                //Bakåt
                public void actionPerformed(ActionEvent arg0) {
    
                    tmp2 = ISBN.getText();
                    Database.addBooks();
    
    
                }
            });
            b2.addActionListener(new ActionListener() {
                //Framåt
                public void actionPerformed(ActionEvent arg0) {
                    if (currentCard < 4) {
                         if(currentCard == 1){
                             Lib.filnamn = books.getText();
                             path = borrowers.getText();
                             Database.database = t1.getText();
                               
    
                             Lib.startLib();
                             Borrowers.startBorrowers();
                             Bookobjects.runObjects();
                            Database.connectDatabase();
    
    
    
                    }
                         if (currentCard == 2){
                        if (!Database.login){
                            JOptionPane.showMessageDialog(null, "Dont even try borrowing books without a library card!"
                                    + "\n Shutdown!");
                            System.exit(0);
                        }
                    
                   // Database.findUser();
                    }
                        currentCard += 1;
    
                        c1.show(cPanel, "" + (currentCard));
    
    
                    }
    
                }
            });
            getContentPane().add(cPanel, BorderLayout.CENTER);
            getContentPane().add(buttonPanel, BorderLayout.SOUTH);
        }
    
    
    }
    Java Code:
    import javax.swing.*;
    import java.sql.*;
    
    public class Database {
    private static Statement stmt;
    public static String database,allt3[] = new String[10];
    public static int card,count=0;
    public static boolean blank = false,login = false;
    private static int logcardnr;
    
     public static void connectDatabase(){
    
     Connection con = null;
     if(database.equals("")){
         JOptionPane.showMessageDialog(null, "Can't run without database!");
         System.exit(0);}
    
        try {
          Class.forName("com.mysql.jdbc.Driver").newInstance();
          con = DriverManager.getConnection("jdbc:mysql://",  // Using anonymous cause i feel like it =)
            "root", "test");
    
    
          if(!con.isClosed())
            System.out.println("Duude youre connected " +
              "trough TCP/IP...");
    
          stmt = con.createStatement();
          try{
          stmt.executeUpdate("USE " + database);
          System.out.println("Used old database!");
            }catch (Exception e){
               stmt.executeUpdate("CREATE DATABASE " + database);
               stmt.executeUpdate("USE " + database);
               System.out.println("Database created!");
            }
          stmt.executeUpdate ("CREATE TABLE Borrowers ("
                  + "cardnr INT,"
                  + "name CHAR(60), "
                  + "street CHAR(40), "
                  + "zip INT,"
                  + "town CHAR(50))");
    
                  stmt.executeUpdate ("CREATE TABLE GoodBooks ("
             + " ISBN long,"
            + " CopyNumber INT,"
            + " Title VARCHAR(60),"
            + " Author VARCHAR(30),"
            + " Publisher VARCHAR(50),"
            + " Year INT,"
            + " Statistics INT,"
            + " BorrowDate INT,"
            + " ReturnDate INT,"
            + " LibraryCardNumber INT)");
       for(int j = 0 ; j < Borrowers.borrow.length; j++){
                     stmt.executeUpdate("INSERT INTO Borrowers "
                             +"(cardnr, name, street, zip, town) "
                             + "VALUES "
                             + "('"+ Borrowers.borrow[j].LibraryCardNumber + "',"
                             + "'"+ Borrowers.borrow[j].Name + "',"
                             + "'"+ Borrowers.borrow[j].Street + "',"
                             + "'"+ Borrowers.borrow[j].ZipCode + "',"
                             + "'"+ Borrowers.borrow[j].Town + "')");
            }
    
    
    
    
    
    
    
    for(int j = 0 ; j <Bookobjects.bookO.length ; j++){
                     stmt.executeUpdate("INSERT INTO goodbooks "
                             +"(ISBN, CopyNumber, Title, Author, Publisher, Year, Statistics,"
                             + " BorrowDate, ReturnDate, LibraryCardNumber) "
                             + "VALUES "
                             + "('"+ Bookobjects.bookO[j].ISBN + "',"
                             + "'"+ Bookobjects.bookO[j].CopyNumber + "',"
                             + "'"+ Bookobjects.bookO[j].Title + "',"
                             + "'"+ Bookobjects.bookO[j].Author + "',"
                             + "'"+ Bookobjects.bookO[j].Publisher + "',"
                             + "'"+ Bookobjects.bookO[j].Year + "',"
                             + "'"+ Bookobjects.bookO[j].Statistics + "',"
                             + "'"+ Bookobjects.bookO[j].BorrowDate + "',"
                             + "'"+ Bookobjects.bookO[j].ReturnDate + "',"
                             + "'"+ Bookobjects.bookO[j].LibraryCardNumber + "')");
            }
      
    
    
    
    
        } catch(Exception e) {
          System.err.println("Fail: " + e.getMessage());
          System.out.println("Fail: " + e);
        } /*finally {
          try {
            if(con != null)
               System.out.println("Connecton succesfully closed!");
              con.close();
          } catch(SQLException e) {}
        }*/
    
    
        }
    public static void findUser(){
       
        try{
            if(GUI.tmp.equals("")){
                JOptionPane.showMessageDialog(null, "I don't accept blanks! Enter a number.");
             blank = true;
            }
          
         if(!blank){
            card = Integer.parseInt(GUI.tmp);
            ResultSet RS = stmt.executeQuery("SELECT name FROM borrowers WHERE cardnr = '"+ card + "'");
              
                while (RS.next()) {
                   count++;
                     JOptionPane.showMessageDialog(null,"Your name is "+RS.getString(1)+ " ! You can now borrow books!" );
                     login = true;
                     logcardnr = card;
                         }
            if(count == 0){
                JOptionPane.showMessageDialog(null, "We were unable to find you. Please contact the expedition");
                login = false;
             }
             }
        }catch(Exception e) {
          System.err.println("Fail: " + e.getMessage());
         e.printStackTrace();
    
        }
        blank = false;
        count=0;
        }
    public static void addBooks(){
    
        try{
        ResultSet RS = stmt.executeQuery("SELECT * FROM goodbooks WHERE ISBN = '"+ GUI.tmp2 + "'");
    
                while (RS.next()) {
    
                     JOptionPane.showMessageDialog(null,"You added: "+RS.getString(3) +" "+ RS.getString(4)
                            +" "+ RS.getString(5) + " " + RS.getString(6)
                            +"!");
                     GUI.ISBN.setText(database);
    
                         }
        }catch (Exception e){
            System.err.println("Fail: " + e.getMessage());
            e.printStackTrace();
        }
        }
    }
    My problem is that inputlibcard.getText(); gives me nothing ... And i really dont know why ... it was working fine .. then i changed someting and now i cant reset it =S Thank you for your toughts =)

  2. #2
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default

    I tried to run what you provided, obviously had to comment out calls to classes that I don't have including calls to the Database b/c i don't have your database. getText() seemed to work fine so there must be something wrong in the Database or another class. Try commenting things out until it works and slowly uncomment them to figure out where the issue is coming from.

Similar Threads

  1. Java has stopped working
    By asd123321 in forum New To Java
    Replies: 1
    Last Post: 12-20-2010, 03:57 AM
  2. Help with getText()
    By geekchick in forum New To Java
    Replies: 17
    Last Post: 08-06-2010, 05:36 PM
  3. Java stopped working for me
    By MarcusarilliuS_IRL in forum New To Java
    Replies: 7
    Last Post: 07-07-2010, 04:13 AM
  4. Build stopped
    By Nerijus in forum New To Java
    Replies: 0
    Last Post: 04-26-2010, 08:25 AM
  5. JSF forms stopped validating/working
    By chlebik in forum JavaServer Faces (JSF)
    Replies: 1
    Last Post: 01-19-2010, 02:16 PM

Posting Permissions

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