简体   繁体   中英

Button action delayed or performed after exiting

I do not have much coding experience, I have only made one very small (really tiny) application before which was used to copy files from location A to location B and then flush the A directory and I haven't faced this kind of issue with it.

right now I am working on an application that searches a database for data and send it to a label printer for printing.

everything is working as intended except for the button that does the printing; when I click the print button nothing happens, if I click it multiple times (usually 3) the printer starts printing 3 times and after that nothing happens till I close the application (GUI) using the X button. (the issue doesn't exist if I print directly using code)

the code is fairly straightforward, please take a look:

GUI截图

Button Code:

private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {                                      
    // TODO add your handling code here:
    PrintLabel p = new PrintLabel(jTextField1.getText());

    if (ONtoggle.isSelected()) {
        try {
            p.labelPrint(jTextField2.getText(), jTextField3.getText(), "MMC");
        } catch (FileNotFoundException | SQLException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 
    else {
        try {
            p.labelPrint(jTextField2.getText(), jTextField3.getText(), "MMT");
        } catch (FileNotFoundException | SQLException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


}                                     

Printing Class:

public class PrintLabel {

  private final String port;
        public PrintLabel(String port) {
                this.port = port;
        }


        public void labelPrint (String number, String copies, String cut) throws FileNotFoundException, SQLException {   


        String DBurl = "jdbc:oracle:thin:@[DB hostname]/[DB Service Name]"; //DB login info.
        String DBuser = "user";
        String DBpass = "pass";

        Connection DBcon = DriverManager.getConnection(DBurl, DBuser, DBpass); // DB connection



        Statement statement = DBcon.createStatement(); //class used to make statements.


        ResultSet rs = statement.executeQuery([SELECT STATEMENT]);


       if (rs.next()) {

            String name = rs.getString("name");
            String nationality = rs.getString("nationality");
            String sex = rs.getString("sex");
            String birthDate = rs.getString("birthdate");

            FileOutputStream os = new FileOutputStream(port);

             PrintStream ps = new PrintStream (os);


             String commands = 
                     "^XA" + //begin ZPL command.
                     "^" + cut + "" + //kiosk cut mode, needed for cutting. MMC for cutting and MMT for tear-off.
                     "^PQ" + copies + ",0,0,n,y" + //first number indicates number of copies.
                     //Line 1
                     "^FO100,20" +  //X and Y axis alignment.
                     "^A0N,20,20" + //font width and hight in dots.
                     "^FB350,2,5,L,0" + //max lines set to 2 for long names.
                     "^FDName: " + name + "^FS" +  //print text command start and end.
                     //Line 2
                     "^FO100,70" + 
                     "^A0N,20,20" + 
                     "^FD File: " + number + "^FS" + 

                     "^FO420,70" + 
                     "^A0N,20,20" + 
                     "^FD" + sex + "^FS" + 
                    //Line 3
                     "^FO100,100" + 
                     "^A0N,20,20" + 
                     "^FD Date of Birth: " + birthDate + "^FS" + 
                     //Line 4
                     "^FO100,130" + 
                     "^A0N,20,20" + 
                     "^FD Nationality: " + nationality + "^FS" + 
                     //Line 5
                     "^FO100,150 ^BY2,1.0,50" + //Barcode Field Width, ratio and height.
                     "^B3N,50,n,n" + //Barcode code-39.
                     "^FD" + number + "^FS" + 
                     "^XZ";  //end ZPL command.

             ps.println(commands);


        }
        }




}

Much Appreciated.

There could be a number of reasons for your issue, the most obvious is that it simply takes a while for your database operation to connect, run and return a result so its hard to give you a single answer.

From your question its not clear if a single click on the button will ever cause anything to print?

I would suggest debugging your application to help isolate where the issue is, or at least add some logging to help you.

Also, you should always close and free your streams, database connections, statements and resultsets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM