Results 1 to 3 of 3
  1. #1
    petec is offline Member
    Join Date
    Feb 2014
    Location
    Near Buffalo NY
    Posts
    33
    Rep Power
    0

    Default Changing a JLabel Text From Anywhere In My Program

    I'm using NetBeans.
    Here are all the gory details.
    I have a JFrame with four JPanels, SensorLL, SensorLC, SensorCR and SensorRR.
    They are instances of the class SensorUI.
    Each has a JLabel called Sensor.
    The class SensorUI has a method writeSensor that writes to the JLabel "Sensor".
    I will have four temperature sensors.
    I have not started the code to read the temperature sensors yet.
    I will have a timer that will run once a second.
    When it fires I will read the sensors and display the result in the
    JLabel "Sensor" in each jPanel.

    I use MyMain to call FrameDemo and create the JFrame and a method
    in FrameDemo to add the JPanels using addObject.
    I can use SensorLL.writeSensor will write text to JLabel Sensor while in Mymain.
    Then I start the timer ReadSensor.
    When it times out it calls SampleSensors
    For an experiment, while in SampleSensors I try to write to the JLabel in the instance SensorLL with SensorLL.writeSensor("xx_");
    I can't because it says it can't find the symbol variable SensorLL in SampleSensors class.
    How can I "get to" that JLabel text when ever I need to?

    ********************************************
    Java Code:
    public class MyMain {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
              //SensorUI SensorLL = null;
              System.out.println("Start of main");
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
                    FrameDemo.createAndShowGUI();
                    SensorUI SensorLL=new SensorUI("SensorLL");
                    FrameDemo.addObject(SensorLL);
                    SensorUI SensorLC=new SensorUI("SensorLC");
                    FrameDemo.addObject(SensorLC);
                    SensorUI SensorCR=new SensorUI("SensorCR");
                    FrameDemo.addObject(SensorCR);
                    SensorUI SensorRR=new SensorUI("SensorRR");
                    FrameDemo.addObject(SensorRR);
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
        
            }
            }
            );
    
                SensorLL.writeSensor("sss"); // This works
                ReadSensor reminderBeep = new ReadSensor(5);
    
        }
        
    }
                       
    ************************************************
    import java.awt.*;
    import javax.swing.*;
    
    public class FrameDemo {
    
        static        JFrame frame = new JFrame("BrewTool");
        
        
        public FrameDemo(){
                
        }
        static void createAndShowGUI() {
            //Create and set up the window.
             System.out.println("At Start of CreateAndShow");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new FlowLayout(FlowLayout.CENTER));
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setSize(1200, 500);
            frame.setVisible(true);       
        }
        static void addObject(SensorUI Sensor){
            System.out.println("At Add Object");
            frame.add(Sensor);
        }
        }
    **************************************************
    import javax.swing.border.Border;
    
    /**
     *
     * @author Pete
     */
    public class SensorUI extends javax.swing.JPanel {
    
        /**
         * Creates new form SensorUI
         */
        public SensorUI(String title) {
            initComponents();
            Temperature.setBorder(javax.swing.BorderFactory.createTitledBorder(title));
        }
    
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
      
    [[[[**** Lots of NetBeans Generated Code deleted for this posting****]]]]                       
    
                                             
    
        public void setSensor(String sensorValue){
          Sensor.setText("Stuff");  
        }
    
        public void writeSensor(String value){
            System.out.println(value );
            Sensor.setText(value);
        }
    
    ****************************************************
    import java.util.Timer;
    public class ReadSensor {
     
          Timer timer;
     
         public ReadSensor(int delaySeconds) {
            timer = new Timer();  //At this line a new Thread will be created
            timer.schedule(new SampleSensors(), delaySeconds*1000, 1000); //delay in milliseconds
            
        }
    
    }
    
    *******************************************************
    import java.util.TimerTask;
    public  class SampleSensors extends TimerTask {
        
            
          @Override
          public void run() {
                System.out.println("ReminderTask is completed by Java timer");
      // Pretend I read a sensor and return a value
                SensorLL.writeSensor("xx_");//ERROR can't find the symbol variable SensorLL in SampleSensors class.
     
                
          }
        }
    Last edited by petec; 02-18-2014 at 07:08 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Changing a JLabel Text From Anywhere In My Program

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    petec is offline Member
    Join Date
    Feb 2014
    Location
    Near Buffalo NY
    Posts
    33
    Rep Power
    0

    Default Re: Changing a JLabel Text From Anywhere In My Program

    Sorry,
    I learned something today.
    Wish I could every day.

    Thanks.
    Pete

Similar Threads

  1. Updating a the text in a jlabel?
    By Snake006 in forum AWT / Swing
    Replies: 3
    Last Post: 08-10-2013, 04:52 PM
  2. JLabel Align Text
    By OllyHal in forum AWT / Swing
    Replies: 1
    Last Post: 02-18-2012, 02:03 AM
  3. Cant get text to align in JLabel
    By spect4cle in forum AWT / Swing
    Replies: 1
    Last Post: 02-12-2012, 09:19 PM
  4. Replies: 12
    Last Post: 08-16-2011, 10:25 AM
  5. Make a text in Jlabel down to next Line
    By hungleon88 in forum AWT / Swing
    Replies: 2
    Last Post: 12-01-2008, 11:10 PM

Tags for this Thread

Posting Permissions

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