简体   繁体   中英

Counter Invoked in Two ActionListeners

I have a counter x that I want to invoke in two separate ActionListener s. When I try to make x into final , I can't increment using x++; . I tried to make x within the nest, but then I can't use the same value in the other ActionListener . Code is as follows:

buttonIn.addActionListener(new ActionListener() {
    String reportDate = "";
    int x = 0;
        public void actionPerformed(ActionEvent e)
        {

            DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm aa");
            Date time = new GregorianCalendar().getTime();
            reportDate = df.format(time);
            String confirm = name.getText() + " has checked in at " + reportDate;
            timeLabel.setText(confirm);
            timeLabel.setVisible(true);
            String action = "Time In";

            reportData[x][0] = name.getText();
            reportData[x][1] = "Time In";
            reportData[x][2] = reportDate;
            x++;
            System.out.println(x);
        }
});
buttonOut.addActionListener(new ActionListener() {
    String reportDate = "";
        public void actionPerformed(ActionEvent e)
        {
            DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm aa");
            Date time = new GregorianCalendar().getTime();
            reportDate = df.format(time);
            String confirm = name.getText() + " has checked out at " + reportDate;
            timeLabel.setText(confirm);
            timeLabel.setVisible(true);
            reportData[x][0] = name.getText();
            reportData[x][1] = "Time Out";
            reportData[x][2] = reportDate;
            x++;
        }
});   

One simple option is to use AtomicInteger instead - then the variable can be final, but you can still increment the wrapped value. So:

final AtomicInteger counter = new AtomicInteger(0);
buttonIn.addActionListener(new ActionListener() {
    // Within here, you can use counter.get and counter.incrementAndGet
});
buttonOut.addActionListener(new ActionListener() {
    // Within here, you can use counter.get and counter.incrementAndGet
});

I'd also strongly consider extracting that code into a separate class though - almost all the code is the same, so you should be able to remove the duplication by parameterizing the differences. So you'd end up with something like:

AtomicInteger counter = new AtomicInteger(0);
buttonIn.addActionListener(new ReportListener(
    counter, reportData, "%s has checked in at %s", "Time In"));
buttonOut.addActionListener(new ReportListener(
    counter, reportData, "%s has checked out at %s", "Time Out"));

(Where ReportListener is the new class implementing ActionListener .)

Additionally:

  • I strongly suspect you want to use HH rather than hh in your SimpleDateFormat
  • Consider which time zone and locale you want to use in your SimpleDateFormat , and specify them explicitly for clarity
  • To get the current time, just call new Date() rather than creating a calendar and extracting the date from it
  • There's no obvious reason for having reportDate as an instance variable
  • For testability, I'd encourage you to use some sort of Clock interface, with an implementation provided by dependency injection, so you can fake time appropriately
  • Consider using Joda Time for all date/time work; it's much cleaner than the built-in date/time API

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