0

I am having an issue with SWING GUI or at least I think it is the swing gui.

Here is my main code file:

/**
 * 
 */
package com.tda.t2.ctas.slasher;

import javax.swing.SwingUtilities;

import com.tda.t2.ctas.slasher.gui.mainFrame;
import com.tda.t2.ctas.slasher.utils.MyCloseListener;



public class SLASHer {

    public SLASHer () {
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        //EventQueue.invokeLater(new Runnable() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                ConfigData myconfig = new ConfigData();
                try {
                    //TdaUrlHelper window = new TdaUrlHelper();
                    //window.tdaFrame.setVisible(true);
                    mainFrame tdaFrame = new mainFrame();
                    tdaFrame.setVisible(true);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

}

Simple call to create the frame and open it. There are other files that I did not put here for space. But the problem that I have (and I have tried on several machines and operation systems) is that the buttons on the window seem to hang. I can select the window and click on the buttons and they highlight like they were hit but nothing happens. I have a tabbed plane and clicking on the other tabs also does nothing. Some times this last for about 15 seconds and other times it lasts several minutes. But it always eventually comes back and will respond to new input (ie it does not remember all the click around I did before it came back). The application overall is simple in that it sits waiting until a user does something before it does something so I am confused on why it seems to hang.

Any help would be appreciated.

Thanks

Patrick Aquilone
  • 584
  • 2
  • 11
  • 28
  • 2
    Your problem is in code not shown. Likely you're not obeying Swing threading rules quite possibly in the ActionListeners for your JButtons, but who knows til we see the offending code. – Hovercraft Full Of Eels Nov 28 '12 at 16:22
  • Sounds like either the GUI thread is printing a stack trace ( :\. ) or you have multithreaded something incorrectly. – Zéychin Nov 28 '12 at 16:23
  • 2
    Note that the mainFrame class should be renamed MainFrame. All class names should begin with an upper case letter. This is important if you want others (like us!) to be able to understand your code and help you. – Hovercraft Full Of Eels Nov 28 '12 at 16:24
  • Posted code is not related to the issue - you have to inspect your app to find out where the bottleneck lies: use a debugger, or go it the dirty way by trying to print out stuff on every layer of the app, going further every time. – moonwave99 Nov 28 '12 at 16:24
  • Sounds like an `ActionListener` attached to the button that starts a heavy task on the EDT iso using a worker thread. Check the [concurrency in swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html) tutorial – Robin Nov 28 '12 at 16:24
  • The problem is that I have 81 buttons on this panel (and more on another tabbed panel) and ALL of them get hung. When I click on the window and nothing is working NOTHING is working. Not a single button will work until it becomes unhung. – Patrick Aquilone Nov 28 '12 at 16:30
  • Of course. When the EDT is busy, your whole UI is frozen. That is why long running tasks should not happen on the EDT, as explained in the tutorial I linked in my previous comment – Robin Nov 28 '12 at 16:31
  • You should try to isolate the error in a small program. Then please feel free to post the offending code any time you need help further help. Otherwise all we can do is discuss theoretical possibilities, and so far that doesn't seem to be helping you much. – Hovercraft Full Of Eels Nov 28 '12 at 16:31

1 Answers1

1

What is the offending code attach to the button that hangs? Check your console for exceptions, and put some System.out.println() statements at the top and bottom of that code. See if you see those print statements print out. Watch how long it takes for the one at the top to print and bottom one to print. If you see both statements then you know that whole block is executing, but if it takes a while to show the last statement you know you are hanging up the Swing thread (also known as EDT - event dispatch thread). Rule number one in Swing the UI can't repaint while it's executing your ActionListener.

In order to make a responsive UI you have to see the first and last statement appear on the console in under 10-100ms (visually almost instantaneous). If you really want to get fancy you can use System.currentTimeMillis() at the stop and bottom. Subtract the two values and println() it. That'll tell you exactly how long that listener ran for. If it's greater than 100ms you need to restructure your code by either improving your algorithm or off loading the long calculation on a thread (see this SwingWorker tutorial).

public void actionPerformed(ActionEvent event) {
    System.out.println("Starting SomeProcess");
    long start = System.currentTimeMillis();

    // all your code belongs here

    long duration = System.currentTimeMillis() - start;
    System.out.printf("SomeProcess took %,d ms%n", duration );
}
chubbsondubs
  • 37,646
  • 24
  • 106
  • 138
  • I didn't add the button code because there are a total of 81 buttons on the panel. And on that, it doesn't matter what button I hit. When it is hung all of the buttons do not work until it becomes unhung. It is almost like it didn't realize that it has focus now which is why I posted what I did. – Patrick Aquilone Nov 28 '12 at 16:29
  • 1
    Well it's up to you how much help you want. You can do what I suggest and figure out the specifics of your problem on your own, or just post code for one of those buttons that is hanging up the UI. You are right I'm not going to look through 81 button listeners, but I will look at 1 button listener. I think if you do what I suggested you'll have a clue as to what the problem is. If you can't figure it out post some more code. – chubbsondubs Nov 28 '12 at 16:35
  • I agree. For some reason @JesterHawk doesn't seem to want to post any listener code. Til he does, all we can do is wonder what is going on. Likely just one listener will be sufficient. – Hovercraft Full Of Eels Nov 28 '12 at 16:36
  • 2
    Well we all know what he's doing at a high level. He's hanging up the EDT thread with a long running calculation that should be offloaded on a thread. Most likely some sort of network service call directly from the EDT thread, but until he posts code we really can't tell him exactly what's wrong. :-) – chubbsondubs Nov 28 '12 at 16:40
  • I am rather surprised at my answer's simplicity dare I say proud. :-) It's all too easy to get into the weeds when talking about threads, but this kept the bar low while teaching debug trick for performance. @JesterHawks I updated my answer a bit with some links that should help you. – chubbsondubs Nov 28 '12 at 16:54
  • 1
    I agree; you're answer is decent. 1+ up-vote. I'm not so sure about the timer though in that the time may be short during program creation and test runs but long in the real world. There are certain situations where a background thread should be required regardless of the time in your test program. File I/O and database methods are one such. – Hovercraft Full Of Eels Nov 28 '12 at 16:56