Acabei de ler sobre Threads (não sabia nada sobre isso até agora há pouco) e vi para que servem, mas não sei exatamente como e quando usá-las. Aqui, eu tenho uma classe que serve para realizar um login.
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import model.User;
import control.DatabaseHandler;
@SuppressWarnings("serial")
public class LoginWindow extends JFrame {
private Container contentPane = getContentPane();
private JButton btnNewButton, btnNewButton_1, btnNewButton_2;
private JTextField textField;
private JPasswordField passwordField;
private DatabaseHandler dbh = new DatabaseHandler();
private GridBagConstraints gbc = new GridBagConstraints();
public void addComp(JButton btn, int gridx, int gridy) {
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = 1;
contentPane.add(btn, gbc);
}
public void addComp(JLabel lbl, int gridx, int gridy) {
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = 1;
contentPane.add(lbl, gbc);
}
public void addComp(JTextField txt, int gridx, int gridy) {
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = 2;
contentPane.add(txt, gbc);
}
public void addComp(JPasswordField pw, int gridx, int gridy) {
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = 2;
contentPane.add(pw, gbc);
}
private ActionListener al = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Thread() {
public void run() {
if(textField.getText().length() != 0 && passwordField.getPassword().toString().length() != 0) {
User user = new User(textField.getText(), new String(passwordField.getPassword()));
btnNewButton.setEnabled(false);
btnNewButton_1.setEnabled(false);
btnNewButton_2.setEnabled(false);
try {
if(dbh.verifyLogin(user)) {
JOptionPane.showMessageDialog(null, "Bem-vindo!", "", JOptionPane.INFORMATION_MESSAGE);
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
PersonalWindow frame = new PersonalWindow(user);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
dispose();
} else {
JOptionPane.showMessageDialog(null, "**USUÁRIO OU SENHA INCORRETOS**", "ERRO", JOptionPane.ERROR_MESSAGE);
btnNewButton.setEnabled(true);
btnNewButton_1.setEnabled(true);
btnNewButton_2.setEnabled(true);
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getStackTrace(), "ERRO", JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(null, "**INVÁLIDO**", "ERRO", JOptionPane.WARNING_MESSAGE);
btnNewButton.setEnabled(true);
btnNewButton_1.setEnabled(true);
btnNewButton_2.setEnabled(true);
}
}
}.start();
}
};
/**
* Create the frame.
*/
public LoginWindow() {
setTitle("Login");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(10, 10, 10, 10);
contentPane.setLayout(new GridBagLayout());
JLabel lblNewLabel = new JLabel("Login: ");
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 24));
addComp(lblNewLabel, 0, 0);
textField = new JTextField();
textField.setToolTipText("Inserir nome de usu\u00E1rio");
textField.setFont(new Font("Tahoma", Font.PLAIN, 24));
textField.setColumns(10);
textField.addActionListener(al);
addComp(textField, 1, 0);
JLabel lblNewLabel_1 = new JLabel("Senha: ");
lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 24));
lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
addComp(lblNewLabel_1, 0, 1);
passwordField = new JPasswordField();
passwordField.setToolTipText("Inserir senha");
passwordField.setFont(new Font("Tahoma", Font.PLAIN, 24));
passwordField.addActionListener(al);
addComp(passwordField, 1, 1);
btnNewButton = new JButton("Login");
btnNewButton.addActionListener(al);
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 18));
addComp(btnNewButton, 0, 2);
btnNewButton_1 = new JButton("Cadastrar");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
RegisterWindow frame = new RegisterWindow();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
dispose();
}
});
btnNewButton_1.setFont(new Font("Tahoma", Font.PLAIN, 18));
addComp(btnNewButton_1, 1, 2);
btnNewButton_2 = new JButton("Cancelar");
btnNewButton_2.setToolTipText("Voltar \u00E0 tela principal");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWindow frame = new MainWindow();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
dispose();
}
});
btnNewButton_2.setFont(new Font("Tahoma", Font.PLAIN, 18));
addComp(btnNewButton_2, 2, 2);
}
}
Primeiramente, gostaria de sugestões para melhorar o meu código. Depois, gostaria de saber se nesse caso a Thread está colocada corretamente.
Por último: a operação de carregar a PersonalWindow através desse ActionListener demora um pouco (cerca de 5 a 10 segundos), e por isso, gostaria de colocar uma JProgressBar indicando o processo de efetuação de login. Como faço para implementar essa JProgressBar, pois na internet não achei muito sobre ela? É necessário ter essa Thread que eu coloquei para fazer a JProgressBar?