Application delay in switching between screens

Asked

Viewed 163 times

1

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

So when I open one screen it appears, when I open another, it disappears and appears.

What I really wanted was for the transition between screens not to be "noticeable".

I’m using the Dispose() method to close one screen and open another, I can’t say for sure, but I think with setVisible(false) it is the same way.

There is a solution to 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;

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());
}
}
  • Need to close one window to open another? Usually desktop applications have multiple windows.

  • close, close no! I need to hide a window to show another

  • A modal window does not solve?

  • i don’t know what a modal window is. the application is all done, it would change a lot, my code is in mvc

  • 1

    If your application is in MVC, then it makes no difference what kind of visualization you choose, right? Modal is a window that sits above the other, preventing interaction with the bottom.

  • 1

    Please present a [mcve] so that we can simulate the problem. Without seeing, it becomes complicated to suggest something.

  • I put an example, too bad it doesn’t go so much to see this change from one to another, because the screen doesn’t have much image, so the exchange is smooth. but my application is built on this model there, the difference is that there are many components on the screen

  • Gifs showing the transition are appreciated

  • I can’t do it! but imagine that when I close a screen, instead of the other already appear quickly, you realize clearly that the previous closed and evc is waiting for the other to open

Show 4 more comments

1 answer

2


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

inserir a descrição da imagem aqui

I don’t think you should worry about that, what’s wrong with one screen being seen being closed while another one is being opened? This is purely visual and aesthetic, and it makes no sense (at least for me) to worry about it. Even more that probably the cause of this supposed "lag" is the computer.

Besides, I don’t see how this can in any way disturb something in any java-swing application, unless the cause is the execution of some method, which at least reading the codes and running them, was not detected anything so complex.

The only thing I can suggest without departing from the scope of the site is that you 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 it. If it still continues, test the jar on another computer. At this link shows how to create a jar in Netbeans and on this other link shows how to do the procedure in Eclipse.

And one final tip is to use JDialog instead of a lot of Jframes, as you may end up getting lost among them when it comes to exchanging information between windows. Here are some links with several 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 single Thread. In this answer better explains the reason for this and any problems that may occur. This other answer shows some ways to start the application within this thread.

  • my application is inside the Event-Dispatch-Thread. that gif I send you, I’m not running inside the ide, it’s from the generated . jar. You can notice that when I click on the button to open the second screen, the desktop appears, see the time it takes in the image of the desktop, to appear the other screen, now imagine that the entire application.... It bothers the user too much.

  • @Manuelacarneiro this has nothing to do with java then. It’s your operating system or your computer and this is outside the scope of the site. Follow the tips I gave in the answer and don’t mind it, there’s nothing wrong with this lag. In your code it does not happen that and if it happens only to you, there is nothing q can be done. Test on a more powerful computer to see if the problem occurs

  • ah right, I thought there might be some way to "improve" my code.

  • @Manuelacarneiro improve what for example? The problem is not in it, it is in the way your computer is running, this has no miracle. Test on another Pc, this problem is not in the application, your code is good.

  • but see, this example of code that I posted, saw Coo it opens fast, you do not notice this lag, but in the gif that I sent, da para perceber, pq?

  • is it not the images that are too heavy? Anyway, I’m trying to find explanations

  • @Manuelacarneiro I’ve already given you the reasons, I don’t understand what others are looking for. It’s not the fault of java or your code. it’s your operating system. Unless you complain about microsoft, you have nothing to do to change that.

  • 1

    right. Thank you!

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.