Hello,
I have a problem with my code and can't see what the difference can be.
I am trying to change my JLabel text via a timer using .setText

I have 2 small apps to demonstrate my problem.
1 - I have a JLabel within a JPanel within a JFrame, and the code below works.
2 - I have a JLabel within a JPanel within a JSplitPane within a JTabbedPane within a JApplet within a JFrame, and the code below does not work.

public void updateMyPanel()
    {
      m_d += 0.1;
    }
  
    double m_d = 0.0;
    protected void redrawPanel()
    {
        if( SwingUtilities.isEventDispatchThread() )
        {
            label.setText( "" + m_d );
        }
        else
        {
            Runnable run = new Runnable()
            {
                public void run()
                {
                  label.setText( "" + m_d );
                }
            };
            try
            {
                SwingUtilities.invokeLater(run);
            }
            catch( InterruptedException ex ) {}
        }
    }

    ...

    // Tester for JLabel within a JPanel within a JFrame
    //
    public static void main( String args[] ) throws Exception
    {
        JFrame  oFrame = new JFrame( "Test Panel " );
        oFrame.setLayout( new BorderLayout() );
        oFrame.setPreferredSize( new Dimension( 200, 200 ) );
        oFrame.setVisible( true );
        
        JMyPanel oMyPanel = new JMyPanel();
        oFrame.add( oMyPanel, BorderLayout.CENTER );
        oFrame.pack();

        double d = 0.0;
        while( true )
        {
            Thread.sleep( 1000 );

            oMyPanel.updateMyPanel();
        }
    }

If anyone can someone show me the way, it would be most appreciated.

Thanks, David

Recommended Answers

All 3 Replies

Don't know if you are looking at this for itself, or as an instance of something more general, but JLabel setText(...), unlike most Swing code, is threadsafe, so you don't need the worker thread in this specific example. You do, however, have exactly the right code structure for the general GUI update case. I did this so often that I packaged it as

void dispatchOnSwingThread(Runnable r) {
	if (SwingUtilities.isEventDispatchThread()) {
		r.run();
	} else {
		try {
			SwingUtilities.invokeLater(r);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}	
}

the code below does not work.

That really doesn't help us to help you. You need to be very specific about what the problem is, eg: what exception is thrown at what line? or what is the difference between the expected and the actual behaviour? You also don't seem to have shown us the version that fails!

Kindly declare double m_d = 0.0; above the updateMyPanel() method.

Thanks guys,
Thanks for the response. I went back to my declarations of my code and found I was debugging an invisible frame.
The .setText() for the JLabel worked successfully.
Happy coding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.