JLabel is transparent while opening file

56 views Asked by At


I've programmed a tool that opens and parses files. As this is taking some time, I wanted to make a little status window, by now it's just consisting of a JLable with the text "Import running, please wait...".
However, during runtime it looks like this:
Transparent JLabel


After the file import is finished, the window is fine, showing the text and everything. I tried programming it without Threads at first, then I thought maybe making it multithreaded will help. So I made the window open in it's own Thread, the file import in it's own Thread and finally both in their own Thread. The result still was the same. Here is the code I'm using:

JLabel label = new JLabel("Importing file, please wait...");
JFrame frame = new JFrame();
frame.setSize(100, 100);
frame.add((new JPanel()).add(label));
//no difference to frame.add(label);
frame.setVisible(true);
//Parse File
openParseFile();
//Close status window after parsing complete
frame.dispose();

Does anybody have any ideas how to solve this? I tried googleing it, but the results only showed how to make a JLapel transparent, not the other way around.

Any help is appreciated,
Thanks
Sverre

1

There are 1 answers

0
SverreN On

Here's the solution, thanks to the comment of MadProgrammer and this tutorial: Javacreed SwingWorker Examle

JLabel label = new JLabel("Importing file, please wait...");
status  = new JFrame();
status.setLayout(new GridBagLayout());
status.setSize(100, 100);
status.add((new JPanel()).add(label));
status.setVisible(true);
SwingWorker sw = new SwingWorker() {

        @Override
        protected Object doInBackground() throws Exception {

            openTableReader();


            return null;
        }

    };
    sw.addPropertyChangeListener(new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent event) {
            if (event.getPropertyName().equals("state") && ((StateValue)event.getNewValue()).equals(StateValue.DONE)) {
                processResult();
                status.dispose();
            }
        }

    }) ;
    sw.execute();