-3
A teacher asked to develop a project (a CRUD of cars). I’ve done the part of inserting, removing, listing and changing.
But the listing part should be done by a thread (teacher requirement), in which she should list a car every 20 seconds and show in a JTextArea
. My question is how to make the thread do that.
Follows the code:
Class of Thread
to list. Note: I haven’t done anything in this class yet:
public class ThreadListar implements Runnable{
private int tempo;
public ThreadListar(int tempo) {
this.tempo=tempo;
Thread t1=new Thread(this);
t1.start();
}
@Override
public void run() {
try {
Thread.sleep(tempo);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Class CarroDAO
with the list method:
@Override
public ArrayList<Carro> listar() {
ArrayList<Carro> carros= new ArrayList<Carro>();
String sql="select * from carro";
try(Connection con= new ConnectionFactory().getConnection()){
PreparedStatement ps= con.prepareStatement(sql);
ResultSet rs= null;
rs=ps.executeQuery();
while(rs.next()) {
Carro c= new Carro();
c.setId(rs.getInt("id"));
c.setMarca(rs.getString("marca"));
c.setModelo(rs.getString("modelo"));
c.setCor(rs.getString("cor"));
c.setPlaca(rs.getString("placa"));
carros.add(c);
}
ps.close();
rs.close();
}catch(SQLException e){
JOptionPane.showMessageDialog(null, "Erro ao realziar consulta:"+e, "ERROR", JOptionPane.ERROR_MESSAGE);
throw new RuntimeException(e);
}
return carros;
}
Class Tela
(swing) with the button and action of listing and setting the data within the JTextArea
:
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Tela frame = new Tela();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Tela() {
JButton btnListar = new JButton("Listar");
btnListar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
CarroDAO dao1 = new CarroDAO();
ArrayList carros= dao1.listar();
for(Object o: carros) {
Carro c= (Carro) o;
textArea.append(String.valueOf("ID: "+c.getId()+"\n"));
textArea.append("Marca: "+c.getMarca()+"\n");
textArea.append("Modelo: "+c.getModelo()+"\n");
textArea.append("Cor: "+c.getCor()+"\n");
textArea.append("Placa: "+c.getPlaca()+"\n"+"\n");
textArea.append("=================");
}
}
});
btnListar.setBounds(234, 233, 89, 23);
contentPane.add(btnListar);
}
}
}
And that’s not clear
e listar um carro a cada 20 segundos
, and when the list is over, what will happen? Will this list be done after the bank search? Need more details on how this should work.– user28595
Oh so that’s why you didn’t answer straight ? ,why didn’t you say so? We were supposed to ask these questions about the functionalities in the beginning, at least we wouldn’t waste time "arguing". What I want is: when the user clicks on the list button the thread performs the list method (which is a select) of the Carrodao class, and returns(lists) all the cars registered in my bank ,only one every 20 seconds
– Viny_Hidan
You don’t need a thread for that. Unless Thread is required, you can do it with a Timer. And please, let’s just force the question,
– user28595
My teacher demanded that you do it with threads, it’s not a question of "no need for thread", but that it’s mandatory to use the thread in this project
– Viny_Hidan