简体   繁体   中英

How to control a Java Swing JLabel with another JLabel

I have an array of classes and in each class I have a Java swing label. This class is in a package with my main GUI class that contains all of my real labels and the array of classes. I'd like to assign the label inside each class to a label in the GUI so that I can access/control them through the array. I'm getting a NullPointerException when trying to change the label's text through the array's label. The code is below.

public class Pin {
   javax.swing.JLabel ioLabel;

   public Pin()
   {
      ioLabel = null;
   }
}

private Pin[] mux;
javax.swing.JLabel io0Label;

public GUI()
{
   mux = new Pin[62];
   io0Label = new javax.swing.JLabel();

   for (int i = 0; i < mux.length; i++)
   {
      mux[i] = new Pin();
   }

   mux[0].ioLabel = io0Label;
}

private void event(evt)
{
   mux[0].ioLabel.setText("Updated");  //The error occurs here
}

Being able to do this would make solving my problem much easier, but I can't seem to get the array's ioLabel to control the GUI one.

Thanks.

Your code here:

javax.swing.JLabel io0Label;

public GUI()
{
   mux = new Pin[62];

   for (int i = 0; i < mux.length; i++)
   {
      mux[i] = new Pin();
   }

   mux[0].ioLabel = io0Label;
}

You declare the io0Label variable but never assign it a JLabel instance, and so trying to use it will always cause a NPE to be thrown. To show that this is correct, search your posted code for any instance of something = new JLabel(...); , and you will find that it doesn't exist.

A specific narrow answer would be to tell you to always initialize your variables with valid references before trying to use them.

A broader more applicable answer, would require that you describe your problem in a bit more detail and consider creating and posting an sscce .

As an aside, your program design is not to be recommended as you should not have classes directly manipulating the fields of other classes. This increases the connectedness of your classes and can lead to bugs that are very difficult to debug.


You could for example, simply give your Pin class getText() and setText(String text) getter and setter methods as well as a getter method for the JLabel that returns it as a JComponent, and use those guys.

For example, my sscce :

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class ControlLabels {
   private static void createAndShowGui() {
      JFrame frame = new JFrame("ControlLabels");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new Gui());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class Pin {
   private JLabel ioLabel = new JLabel();

   public Pin() {
      ioLabel.setHorizontalAlignment(SwingConstants.CENTER);
   }

   public String getText() {
      return ioLabel.getText();
   }

   public void setText(String text) {
      ioLabel.setText(text);
   }

   public JComponent getIoLabel() {
      return ioLabel;
   }
}

class Gui extends JPanel {
   private static final int LABEL_CNT = 50;
   private static final int PREF_W = 150;
   private static final int PREF_H = 400;
   private static final int TIMER_DELAY = 1000;
   private Pin[] mux = new Pin[LABEL_CNT];

   public Gui() {
      JPanel labelPanel = new JPanel(new GridLayout(0, 1));
      for (int i = 0; i < mux.length; i++) {
         mux[i] = new Pin();
         mux[i].setText("mux " + i);
         labelPanel.add(mux[i].getIoLabel());
      }
      setLayout(new BorderLayout());
      add(new JScrollPane(labelPanel));

      new Timer(TIMER_DELAY, new TimerListener()).start();
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   private class TimerListener implements ActionListener {
      private static final int MAX_COUNT = 20;
      private int count = 0;

      @Override
      public void actionPerformed(ActionEvent arg0) {
         if (count < MAX_COUNT) {
            int multiplier = 2;
            for (int i = 0; i < count; i++) {
               multiplier *= 2;
            }
            for (int i = 0; i < mux.length; i++) {
               mux[i].setText("mux " + (i + multiplier));
            }
            count++;
         } else {
            count = 0;
         }
      }
   }
}

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