-1

I have a JFrame. in the JFrame is a JDesktopPane, JComboBox, several JLabels, and a JProgressBar. I am facing two challenges:

  1. I want to update/change the text in one of the JLabels in the JFrame by clicking a JButton that is on a JInternalFrame (the button does some calculations).

  2. Upon clicking a JButton that is on another JInternalFrame (the button performs a small task), I want to use the JProgressBar (progressbar is in JFrame) to show the progress of work done.

I use SwingUtilities.invokelater() to perform the tasks done by the buttons. am using NetBeans as my IDE.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
hybee
  • 1
  • 1
  • Could you share what you've already tried (code)? – Doron Yakovlev Golani Jan 30 '17 at 16:27
  • 1
    Welcome to Stack Overflow, please take the [tour], and go through the [help] and learn [ask], then post a valid [mcve] or a [Short, Self Contained, Correct Example](http://sscce.org/). If your problem is related to you wanting to change the text from a `JLabel` on button click, then create a simple window with a `JLabel` and a `JButton` and on its `actionPerformed` call `myLabel.setText("Whatever text you want");`. Also please write some capitalized words when needed, it's hard to read everything in lower case. – Frakcool Jan 30 '17 at 16:53
  • [Possible duplicate](http://stackoverflow.com/questions/12020949/jprogressbar-isnt-progressing/12021971#12021971) – MadProgrammer Jan 30 '17 at 20:57
  • thanks to you all for the swift response. every other parts of the program is working fine its just the jLabel and jProgressBar that is giving me issue. what i observe was the "new text" that should be display by the jLabel is not showing but when i do print on the jLabel, it shows the new text. – hybee Jan 31 '17 at 09:12
  • when i want to access any variable in the jFrame from the jInternalFrame, i create a new instance of the jFrame. class name of the jframe is home, so i do " home home1 = new home(); then (jLabel7 is public here) home1.jLabel7.setText("new text") but its not working. i even wrote a method named "AMOUNT" in the jFrame to get the new text then set it as the value of the jLabel. still not updating the jLabel . i even put theAMOUNT() method in the DONE method of a swingworker still not updating the jLabel. – hybee Jan 31 '17 at 09:12
  • please help and you can ask anything about the code. i should have post it but its much and i dont know which part to post. am using netbeans as my IDE. thanks once again – hybee Jan 31 '17 at 09:12
  • @hybee try to write a short example, like deleting everything not needed but still/just showing the problem – user85421 Feb 01 '17 at 08:03

1 Answers1

1

Hard to know what is happening without code, but probably the Event Dispatch Thread (EDT) is being blocked (or terminated?), e.g. by code being called by invokeLater. The EDT is used to update the GUI and should not be used for (slow) non-GUI related calculations. See tutorial The Event Dispatch Thread and subsequent for more details.

Example (without blocking):

import java.awt.*;
import java.awt.event.ActionEvent;
import java.time.LocalTime;

import javax.swing.*;

public class LabelProgress {

    public static void main(String[] args) {
        LabelProgress main = new LabelProgress();
        SwingUtilities.invokeLater(() -> main.showInternal1());
        SwingUtilities.invokeLater(() -> main.showInternal2());
    }

    private JFrame frame;
    private JLabel label;
    private JDesktopPane desktop;
    private JProgressBar bar;

    private int progress = 0;

    private LabelProgress() {
        label = new JLabel("Label: ");

        desktop = new JDesktopPane();

        bar = new JProgressBar(0, 100);
        bar.setStringPainted(true);

        frame = new JFrame();
        frame.setLayout(new BorderLayout());
        frame.add(label, BorderLayout.BEFORE_FIRST_LINE);
        frame.add(desktop, BorderLayout.CENTER);
        frame.add(bar, BorderLayout.AFTER_LAST_LINE);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setSize(600, 400);
        frame.validate();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private void showInternal1() {
        JButton change = new JButton("Change");
        change.addActionListener(this::doChange);

        JInternalFrame internal = new JInternalFrame("Change Label");
        internal.setLayout(new FlowLayout());
        internal.add(change);
        internal.setBounds(20, 20, 200, 150);
        internal.setVisible(true);
        desktop.add(internal);
    }

    private void showInternal2() {
        JButton task = new JButton("Task");
        task.addActionListener(this::doTask);

        JInternalFrame internal = new JInternalFrame("Small Task");
        internal.setLayout(new FlowLayout());
        internal.add(task);
        internal.setBounds(150, 100, 200, 150);
        internal.setVisible(true);
        desktop.add(internal);
    }

    private void doChange(ActionEvent ev) {
        // using a SwingWorker:
        // for demonstration I used an anonymous class, maybe a own class is better
        SwingWorker<LocalTime , Void> worker = new SwingWorker<LocalTime , Void>() {
            @Override
            protected LocalTime doInBackground() throws Exception {
                // not executed on the EDT - just get the current time
                LocalTime someCalculation = LocalTime.now();
                return someCalculation;
            }
            @Override
            protected void done() {
                // executed on EDT
                try {
                    LocalTime resultOfSomeCalculation = get();
                    label.setText("Label: " + resultOfSomeCalculation.toString());

                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        };
        worker.execute();
    }

    private void doTask(ActionEvent ev) {
        // no need to use SwingWorker
        Thread thread = new Thread(this::slowTask);
        thread.start();
    }

    private void slowTask() {
        // not really that slow, just for demonstration
        progress += 10;
        if (progress > 100) progress = 100;

        // and now switching to the EDT
        SwingUtilities.invokeLater(() -> bar.setValue(progress));
    }
}
user85421
  • 28,957
  • 10
  • 64
  • 87