I tested that code, and it works fine for me. The window is opened for about 2 minutes and everything works as it should. When does lag begin? When clicking buttons? Moving the window?
Java GUI sloppy after two seconds
-
30-06-2023 - |
Question
I am struggling with Eclipse WindowBuilder GUI. I've made a layout and it does work, but it begins to lag after 2-3 seconds and is horribly slow. I've googled it, found the revalidate()
, validate()
, update()
and stuff like this, but it does pretty the same. Can you please point me out where it gets laggy, hence I don't know what causes me this and I'm confused a lot now.
Source code:
package forum;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class ForumView {
private JFrame frame;
private JTextField txtUsr;
private JTextField txtPasswd;
private JSeparator separator;
private JTextField txtUsr_1;
private JTextField txtPasswd_1;
private JButton btnNewButton_1;
private JSeparator separator_1;
private JButton btnNewButton;
private JButton btnNewButton_2;
private JButton btnNewButton_3;
private JButton btnAddPicture;
private JButton btnNewButton_4;
private JButton btnBack;
private JButton btnSortAlphabetically;
private JButton btnNewButton_5;
private JLabel lblNewLabel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ForumView window = new ForumView();
window.frame.setVisible(true);
window.frame.update(window.frame.getGraphics());
window.frame.revalidate();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ForumView() {
initialize();
}
public void redrawMethod() {
frame.repaint();
txtUsr.repaint();
txtPasswd.repaint();
btnNewButton.repaint();
separator_1.repaint();
separator.repaint();
txtUsr_1.repaint();
txtPasswd_1.repaint();
btnNewButton_1.repaint();
separator_1.repaint();
btnNewButton.repaint();
btnNewButton_2.repaint();
btnNewButton_3.repaint();
btnAddPicture.repaint();
btnNewButton_4.repaint();
btnBack.repaint();
btnSortAlphabetically.repaint();
btnNewButton_5.repaint();
lblNewLabel.repaint();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 1318, 814);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
txtUsr = new JTextField();
txtUsr.setBounds(10, 11, 86, 28);
frame.getContentPane().add(txtUsr);
txtUsr.setColumns(10);
txtPasswd = new JTextField();
txtPasswd.setBounds(10, 42, 86, 28);
frame.getContentPane().add(txtPasswd);
txtPasswd.setColumns(10);
btnNewButton = new JButton("Login");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(txtUsr.getText());
}
});
btnNewButton.setBounds(106, 10, 89, 23);
frame.getContentPane().add(btnNewButton);
separator = new JSeparator();
separator.setOrientation(SwingConstants.VERTICAL);
separator.setBounds(205, 11, 2, 51);
frame.getContentPane().add(separator);
txtUsr_1 = new JTextField();
txtUsr_1.setText("usr");
txtUsr_1.setBounds(217, 11, 86, 28);
frame.getContentPane().add(txtUsr_1);
txtUsr_1.setColumns(10);
txtPasswd_1 = new JTextField();
txtPasswd_1.setText("passwd");
txtPasswd_1.setBounds(217, 42, 86, 28);
frame.getContentPane().add(txtPasswd_1);
txtPasswd_1.setColumns(10);
btnNewButton_1 = new JButton("register");
btnNewButton_1.setBounds(313, 10, 89, 23);
frame.getContentPane().add(btnNewButton_1);
separator_1 = new JSeparator();
separator_1.setBounds(10, 73, 1282, 2);
frame.getContentPane().add(separator_1);
btnNewButton_2 = new JButton("Add topic");
btnNewButton_2.setEnabled(false);
btnNewButton_2.setBounds(10, 120, 100, 23);
frame.getContentPane().add(btnNewButton_2);
btnNewButton_3 = new JButton("Passwd change");
btnNewButton_3.setEnabled(false);
btnNewButton_3.setBounds(10, 154, 100, 23);
frame.getContentPane().add(btnNewButton_3);
btnAddPicture = new JButton("Add picture");
btnAddPicture.setEnabled(false);
btnAddPicture.setBounds(10, 186, 98, 26);
frame.getContentPane().add(btnAddPicture);
btnNewButton_4 = new JButton("Logout");
btnNewButton_4.setBounds(106, 43, 89, 26);
frame.getContentPane().add(btnNewButton_4);
btnBack = new JButton("Back");
btnBack.setEnabled(false);
btnBack.setBounds(10, 223, 98, 26);
frame.getContentPane().add(btnBack);
btnSortAlphabetically = new JButton("Sort Alphabetically");
btnSortAlphabetically.setEnabled(false);
btnSortAlphabetically.setBounds(10, 257, 98, 26);
frame.getContentPane().add(btnSortAlphabetically);
btnNewButton_5 = new JButton("Add Comment");
btnNewButton_5.setEnabled(false);
btnNewButton_5.setBounds(10, 294, 98, 26);
frame.getContentPane().add(btnNewButton_5);
lblNewLabel = new JLabel("No Photo");
lblNewLabel.setBounds(420, 11, 55, 51);
frame.getContentPane().add(lblNewLabel);
frame.validate();
txtUsr.validate();
txtPasswd.validate();
separator.validate();
txtUsr_1.validate();
txtPasswd_1.validate();
btnNewButton_1.validate();
separator_1.validate();
btnNewButton.validate();
btnNewButton_2.validate();
btnNewButton_3.validate();
btnAddPicture.validate();
btnNewButton_4.validate();
btnBack.validate();
btnSortAlphabetically.validate();
btnNewButton_5.validate();
lblNewLabel.validate();
}
}
Thanks.
EDIT: Now the "main" GUI works, but everything else is freezing. I used an openfiledialog to open a image file, but that dialog freezes out too. Here is the code I've tried:
btnAddPicture.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.createBufferStrategy(2);
frame.validate();
frame.repaint();
final JFileChooser fc = new JFileChooser();
//RepaintManager.currentManager(fc).setDoubleBufferingEnabled(true);
System.out.println(RepaintManager.currentManager(fc).isDoubleBufferingEnabled());
//returns true
//fc.repaint();
// in response to a button click:
int returnVal = fc.showOpenDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
} else {
System.out.println("Open command cancelled by user.");
}
}
});
EDIT 2: On Linux machine everything works like a charm.
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow