title: how to toggle text in a jpanel
body: <p>In my JPanel, I have two JLabels. The top label shows the time, while the bottom label shows the date.</p>
  
  <p>I'm trying to implement a JToggleButton which toggles the time from 12-hour format to 24-hour format, and vice-versa. The problem is, the toggle button is not changing the time. What should I do to fix the problem? Much appreciated!</p>
  
  <p>Full code:</p>
  
  <pre><code>package clock;
  import java.awt.*;
  import java.awt.event.*;
  import java.text.SimpleDateFormat;
  import java.util.Date;
  import javax.swing.*;
  
  public class ClockPanel extends JPanel {
  
      private Date getTime, getDate;
      private JToggleButton hourTypeButton;
      private JLabel timeLabel, dateLabel;
      private SimpleDateFormat timeFormat, dateFormat;
  
      public static void main(String[] args) {
          EventQueue.invokeLater(new Runnable() {
              public void run() {
                  JFrame frame = new JFrame("Clock");
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  frame.setResizable(false);
                  frame.setSize(640, 360);
  
                  ClockPanel clockPanel = new ClockPanel();
                  frame.add(clockPanel);
                  frame.setVisible(true);
              }
          });
      }
  
      public ClockPanel() {
          timeFormat = new SimpleDateFormat("hh:mm a");
          dateFormat = new SimpleDateFormat("EEEE, MMMM dd, yyyy");
  
          hourTypeButton = new JToggleButton("12-Hour");
          hourTypeButton.addActionListener(new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent e) {
                  timeFormat = new SimpleDateFormat((hourTypeButton.isSelected() ? "kk" : "hh") + ":mm a");
                  hourTypeButton.setText((hourTypeButton.isSelected() ? "24" : "12") + "-hour");
              }
          });
          add(hourTypeButton);
  
          getTime = new Date();
          timeLabel = new JLabel(timeFormat.format(getTime));
          timeLabel.setFont(new Font("Segoe UI", Font.PLAIN, 140));
          add(timeLabel);
  
          getDate = new Date();
          dateLabel = new JLabel(dateFormat.format(getDate));
          dateLabel.setFont(new Font("Segoe UI", Font.PLAIN, 50));
          dateLabel.setForeground(Color.GRAY);
          add(dateLabel);
      }
  
  }
  </code></pre>