Results 1 to 10 of 10
  1. #1
    Paul279 is offline Member
    Join Date
    Oct 2012
    Posts
    28
    Rep Power
    0

    Default (cleared) JTabbed pane: How to load in background?

    Hello,

    i have made an jtabbedPane mit 10 tabs.

    Java Code:
    JComponent panel1 = makeTextPanel("Panel #1");
    tabbedPane.addTab("Tab 1", icon, panel1,
                      "Does nothing");
    When i start the program each tab will be added, but not loaded. I mean before i can use this program i have to click on each of this ten tabs, because every tab has many things to do in the background, before the user can work with the program.

    So how can i add jTabbedPanes and start the constructor of each tab.

    Thanks
    Last edited by Paul279; 03-28-2014 at 09:35 PM. Reason: closed

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    926
    Rep Power
    2

    Default Re: JTabbed pane: How to load in background?

    Take a look at the SwingWorker class.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  3. #3
    Paul279 is offline Member
    Join Date
    Oct 2012
    Posts
    28
    Rep Power
    0

    Default Re: JTabbed pane: How to load in background?

    Should i put every jTabbedPane into one SwingWorker? Or one SwingWorker for all Tabs?

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    926
    Rep Power
    2

    Default Re: JTabbed pane: How to load in background?

    My best guess is to have each tab load with a SwingWorker of its own.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  5. #5
    Paul279 is offline Member
    Join Date
    Oct 2012
    Posts
    28
    Rep Power
    0

    Default Re: JTabbed pane: How to load in background?

    Thank you

    Is it right in my thougt only to put this code:
    Java Code:
    JComponent panel1 = makeTextPanel("Panel #1");
    tabbedPane.addTab("Tab 1", icon, panel1,
                      "Does nothing");
    in a SwingWorker and that`s it?

    Or should I anything concern?

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    926
    Rep Power
    2

    Default Re: JTabbed pane: How to load in background?

    Give it a try and see what it does. If it works for you then it's good code :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  7. #7
    Paul279 is offline Member
    Join Date
    Oct 2012
    Posts
    28
    Rep Power
    0

    Default Re: JTabbed pane: How to load in background?

    Thank you

  8. #8
    Paul279 is offline Member
    Join Date
    Oct 2012
    Posts
    28
    Rep Power
    0

    Default Re: JTabbed pane: How to load in background?

    Long time ago I wrote this chapter, but only tried now.
    And it isn`t working :-(

    Java Code:
            
            //Initialisierung aller vorhandenen Tabs
            juroren = new Juroren(controller);
            punktelimit = new Punktelimit(controller);
            reihungUeberblick = new ReihungUeberblick(controller);
            reihunggenau = new ReihungGenau(controller);
            druckeinstellung = new Druckeinstellung(controller);
            meisterschaftstitel = new Meisterschaftstitel(controller);
            reihungUeberblick = new ReihungUeberblick(controller);
            tanzSchulverwaltung = new TanzSchulverwaltung(controller);
            tanzaenderung = new Tanzaenderung(controller);
            wirklicheRunde = new WirklicheRunde(controller);
            serverBereit = new ServerBereit(controllerH);
            verbundeneClients = new VerbundeneClients(controllerH);
            sonstiges = new Sonstiges(controller);
            about = new About();
    
    EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                    jTabbedPane_Einstellung.add("Juror",juroren);
                    jTabbedPane_Einstellung.add("Punktelimit",punktelimit);
                    jTabbedPane_Einstellung.add("Reihung-Überblick",reihungUeberblick);
                    jTabbedPane_Einstellung.add("Reihung-Genau",reihunggenau);
                    jTabbedPane_Einstellung.add("Druckeinstellung",druckeinstellung);
                    jTabbedPane_Einstellung.add("Meisterschaftstitel",meisterschaftstitel);
                    jTabbedPane_Einstellung.add("Tänzer/Schulverwaltung",tanzSchulverwaltung);
                    jTabbedPane_Einstellung.add("Tanzänderung",tanzaenderung);
                    jTabbedPane_Einstellung.add("WirklicheRunde",wirklicheRunde);
                    jTabbedPane_Einstellung.add("Server bereit",serverBereit);
                    jTabbedPane_Einstellung.add("Verbundene Clients",verbundeneClients);
                    jTabbedPane_Einstellung.add("Sonstiges",sonstiges);
                    jTabbedPane_Einstellung.add("About",about);
                    } catch (Exception e) {
                        controller.error_ausgeben(e);
                    }
                }
            });
    Also tried to set EventQueue.invokeLater before all new initialisations.
    EventQueue.invoke.later....
    {
    juroren = new Juroren(controller);
    .....
    jTabbedPane_Einstellunge.add("Juroren",juroren);
    ...
    }

    But then I have later in my code a Nullpointer Reference.
    I think because the Runnable is not finished, when the code in the main thread is coming to the code-part, which is throwing the NullPointer-Exception.

    I would like to have, that the JTabbedPane loads and after successfully loading the main Thread should go on.

    Thank you

  9. #9
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    926
    Rep Power
    2

    Default Re: JTabbed pane: How to load in background?

    To keep the main thread waiting is generally not a good idea. The UI will freeze and the user will experience it as slow. You could try to introduce a boolean indicating that the UI is still loading. Then later, don't access your components until loading is completed:

    Java Code:
    boolean isLoading = true;
    
    EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                    jTabbedPane_Einstellung.add("Juror",juroren);
                    jTabbedPane_Einstellung.add("Punktelimit",punktelimit);
                    jTabbedPane_Einstellung.add("Reihung-Überblick",reihungUeberblick);
                    jTabbedPane_Einstellung.add("Reihung-Genau",reihunggenau);
                    jTabbedPane_Einstellung.add("Druckeinstellung",druckeinstellung);
                    jTabbedPane_Einstellung.add("Meisterschaftstitel",meisterschaftstitel);
                    jTabbedPane_Einstellung.add("Tänzer/Schulverwaltung",tanzSchulverwaltung);
                    jTabbedPane_Einstellung.add("Tanzänderung",tanzaenderung);
                    jTabbedPane_Einstellung.add("WirklicheRunde",wirklicheRunde);
                    jTabbedPane_Einstellung.add("Server bereit",serverBereit);
                    jTabbedPane_Einstellung.add("Verbundene Clients",verbundeneClients);
                    jTabbedPane_Einstellung.add("Sonstiges",sonstiges);
                    jTabbedPane_Einstellung.add("About",about);
                    }
                    catch (Exception e) {
                        controller.error_ausgeben(e);
                    }
                    finally() {
                        isLoading = false;
                    }   
                }
            });
    
    ...
    
    if ( ! isLoading ) {
            //access component here
    
    }
    Which brings me to the next question: when creating the views like this: juroren = new Juroren(controller); is that taking a long time? If that is the case, you might want to move that into the SwingUtilities.invokelater() call (Or use SwingWorkers).
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  10. #10
    jp19800101 is offline Member
    Join Date
    Apr 2014
    Posts
    1
    Rep Power
    0

    Default Re: JTabbed pane: How to load in background?

    put every jTabbedPane into one SwingWorker

Similar Threads

  1. Jtabbed Pane not taking entire space
    By Wnt2bsleepin in forum AWT / Swing
    Replies: 2
    Last Post: 08-27-2012, 10:15 PM
  2. Adding jmenubar inside jtabbed pane
    By suresh_m5a5@yahoo.co.in in forum AWT / Swing
    Replies: 1
    Last Post: 02-25-2011, 02:10 PM
  3. get component from jtabbed pane
    By anilkumar_vist in forum New To Java
    Replies: 1
    Last Post: 12-27-2009, 07:34 PM
  4. [SOLVED] Mouse event in JTabbed Pane
    By javanewbie in forum AWT / Swing
    Replies: 6
    Last Post: 06-10-2009, 08:50 AM
  5. [SOLVED] Jtabbed pane hide a tab...?
    By prabhurangan in forum AWT / Swing
    Replies: 5
    Last Post: 08-26-2008, 08:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •