Delayed inter-screen application

1

When I change the screen, there is always a delay from one screen to another and this is annoying. You see clearly that one screen closes to open another. do you know that java symbol that stays when you open an application?

Then when I open a screen it appears, when I open another, it disappears and appears.

What I really wanted was that the transition between screens was not "noticeable."

I'm using the dispose () method to close a screen and open another, I do not say for sure, but I think with setVisible (false) it stays the same.

Is there a solution for this?

package visao;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

public class TelaInicialUm extends JFrame {

private JPanel contentPane;
private JButton btnOutraTela;

public TelaInicialUm() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    btnOutraTela = new JButton("outra tela");
    btnOutraTela.setBounds(135, 67, 148, 93);
    contentPane.add(btnOutraTela);
    setLocationRelativeTo(null);
}

public JButton getBtnOutraTela() {
    return btnOutraTela;
}

}

package view;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

public class TelaDois extends JFrame {

private JPanel contentPane;
private JButton btnTelaTres;

public TelaDois() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    btnTelaTres = new JButton("Tela tres");
    btnTelaTres.setBounds(200, 136, 89, 23);
    contentPane.add(btnTelaTres);
    setLocationRelativeTo(null);
}

public JButton getBtnTelaTres() {
    return btnTelaTres;
}
}

package control;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import visao.TelaDois;
import visao.TelaInicialUm;

public class ControleTelaInicialUm implements ActionListener{

private TelaInicialUm tiu;

public ControleTelaInicialUm(TelaInicialUm tiu) {

    this.tiu = tiu;     
    this.tiu.getBtnOutraTela().addActionListener(this);

    tiu.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource()==tiu.getBtnOutraTela()){
        this.tiu.dispose();
        new ControleTelaDois(new TelaDois());
    }
}
}

package control;

import visao.TelaDois;

public class ControleTelaDois {

private TelaDois td;
public ControleTelaDois(TelaDois td) {
    this.td = td;

    td.setVisible(true);
}
}

package control;

import visao.TelaInicialUm;

public class MainTelas {

public static void main(String[] args) {
    new ControleTelaInicialUm(new TelaInicialUm());
}
}
    
asked by anonymous 30.01.2018 / 21:17

1 answer

2

I tested the code here and this problem did not occur, as can be seen in the gif:

Idonotthinkyoushouldworryaboutit,what'stheproblemofonescreenbeingseenbeingclosedwhileanotherisbeingopened?Thisispurelyvisualandaesthetic,anditdoesnotmakesense(atleastforme)toworryaboutit.Evenmorethan,probably,thecauseofthissupposed"lag" is the computer.

In addition, I do not see how this can in any way disrupt something in any java-swing application, unless the cause is the execution of some method, which at least reading the codes and executing them, does not nothing so complex was detected.

The only thing I can suggest without running away from the site scope is to reinstall or update your JDK and recompile your application, if you continue with the alleged problem, create a jar and try to run directly from it without involving IDE in that. If you still continue, test the jar on another computer. This link shows you how to create a jar in Netbeans and in this other link shows how to do the procedure in Eclipse.

And a final tip is to use JDialog instead of a lot of JFrames, because you could end up getting lost among them when doing some information exchange between the windows. Here are some links to various examples of how to create JDialogs :

And when programming in java-swing, you should always start the screen inside the Event- Dispatch-Thread , because swing is not Thread-Safe , and the entire GUI needs to start within this unique Thread. In this answer better explains the reason for this and any problems that may occur. This other answer shows you some ways to start the application within this Thread.

    
31.01.2018 / 10:19