-2

I have a swing GUI class Foo and in the constructor of Foo class, I load some values from the databases. Based on the returning values from database, I can say whether this GUI should be visible or not. However, when I say, this.setVisible(false); inside the constructor of this GUI class, it has no effect because object creation is not completed.

How can I complete this task without user realizing the transition (disappearing of the GUI)?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
drJava
  • 697
  • 2
  • 9
  • 24
  • Loading database in constructor is bad idea. After invoking this `setVisible(false)`, components are not visible in window. – SatyaTNV May 09 '16 at 12:58
  • setVisible(false) should go before you start reading the database. Thus, it would be not visible till proven otherwise(conditional that responds based on database) – DarkV1 May 09 '16 at 12:58
  • @DarkV1 I tried but It is still visible even if I say, setVisible(false) – drJava May 09 '16 at 13:01
  • @Satya This is a very old system. It is not easy to change the structure of it. Because of this reason, I am forced to load these data in the constructor. Based on these values, the GUI is dynamically created. – drJava May 09 '16 at 13:03
  • 4
    *"It is not easy to change the structure of it."* - And it will be even harder for the next (poor) guy if you do this kind of thing! – Stephen C May 09 '16 at 13:15
  • 1
    I would make the constructor accept a `ResultSet`. In this scenario, other code has already made the decision as to whether to construct the frame. – Andrew Thompson May 09 '16 at 13:19
  • @AndrewThompson There is a main GUI and a Panel. I am now working on this panel and cannot change the main gui. I should make everything within this class without touching the main GUI. – drJava May 09 '16 at 13:49
  • Maybe have a look here: http://stackoverflow.com/questions/4065456/prevent-java-from-repainting-the-content-of-a-jpanel-while-updating Avoid the repaint?... Since it's a panel it will be visible immediately, except when you avoid the repaint... – mambax May 09 '16 at 14:33
  • @StephenC You are right but it is 10 years old software. I cannot save it from drowning or ? – drJava May 09 '16 at 18:49
  • 1
    What I am trying to say is that you should be striving to improve the structure / quality of the code, even if that means a LOT more work for you. If the product is drowning / drowned ... then start again. (But I suspect that you are exaggerating somewhat.) Either way, look up the phrase "technical debt" in Google. – Stephen C May 09 '16 at 22:38
  • @StephenC I read something about "technical debt" and that describes my situation :) I am now junior and I will learn. Thanks again . – drJava May 10 '16 at 06:40
  • @StephenC That costed time but I have paid my "technical debt" and changed the structure. Now, I am getting data from parent and everything is now more stable than before. Thank you for the tip. – drJava May 11 '16 at 08:58

2 Answers2

2

Try invokeLater that is just delaying the execution of a block of codes inside its run method.

SwingUtilities.invokeLater(new Runnable() 
    {
      public void run()
      {
        yourFrame.setVisible(false);
      }
    });
ziLk
  • 3,120
  • 21
  • 45
  • I tried a similar way with "ScheduledExecutorService". I think they are working similarly. However, sometimes the user sees the GUI disappear, which is not nice. – drJava May 09 '16 at 13:46
  • Why would you have to HIDE it in first place? AFAIK it only SHOWS when you set it visible. Maybe the OP has some error in his constructor... :S – mambax May 09 '16 at 14:32
  • Nvm, did see the comment of Panel, which makes JFrame.setVisible obsolete and hence your answer, I guess. – mambax May 09 '16 at 14:34
-1

I solved my problem. I set a timer, which checks every 500 miliseconds if it is still in constructor. I put an flag at the end of the constructor. When the everything is done in constructor, the timer detects it and set the Gui non-visible and the timer works for 15 seconds, which is more than enough for the object creation and stops itself.

drJava
  • 697
  • 2
  • 9
  • 24
  • 1
    Sorry to interrupt here with your own solution. I think the Timer is a little bit out of scope :S Why don't you aim for something more Listener-related? Like adding the panel to the view WHEN is is fully constructed? :S Or broadcast an event instead of a timer? – mambax May 09 '16 at 14:38
  • 1
    @m4mbax The problem with listener was that the user sees the transition. I mean, at the beginning, there was a panel but in less than a second, the panel disappeared. Nevertheless, that did not look good. I know that my solution is not elegant but it solves my problem. – drJava May 09 '16 at 18:45