I have an application that after successfull login (on a JFrame
), starts to create the main frame (class MainUI
that extends from JFrame
). That MainUI
class contains a JTabbedPane
(which each tab is a class that extends from JPanel
) and, on setVisible
method, creates and shows each tab.
I want to add on the login form, after successfull login, a Spinner image to indicate that the MainUI
is being created.
After display the Spinner image, I invoke the creation of the MainUI
and call the setVisible
method on EventQueue.invokeLater();
but the Spinner image is not updated. If I use new Thread(runner).start();
is updated, but I get a lot of Component creation must be done on Event Dispatch Thread
Some code of Login.java:
buttonLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
login();
}
});
private void login()
{
//check DB and permissions
//if all is ok
lMsj.setIcon(spinner);
new Thread(new Runnable() {
public void run() {
showMainUI(usr);
}
}).start();
}
private void showMainUI(final Usuario usr)
{
Runnable runner = new Runnable() {
public void run() {
final MainUI mui = new MainUI();
mui.setVisible(true);
dispose();
}
};
EventQueue.invokeLater(runner);
}
and some code of MainUI.java
public MainUI()
{
SwingUtilities.invokeLater(new Runnable() {
@Override public void run() {
setMinimumSize(new Dimension(1280, 960));
createComponents();
}
});
}
private void initComponents()
{
//..
// menuItem = new ...
// ...
}
@Override
public void setVisible(boolean value)
{
//..
if (Security.get().isAllowed("tab1")){
addTab1();
}
//..
}
private void addTab1(){
//..
getTabbedPane().addTab("Tab1", new Tab1());
//..
}
How I can fix this, so that the image is updated and the user interface is created in the "background"?