andreamarz
22-04-2012, 22:46
Salve a tutti,
purtroppo ho qualche problema con il refresh delle jtalbe in tempo reale o per meglio dire dopo la pressione di un bottone.
Ho cominciato da nemmeno un mese e mi trovo un po' spaesato nel leggere le varie guide qualcuno potrebbe darmi una mano dandomi delle dritte, non mi serve necessariamente il codice, se c'è schifo non mi fa, ma una luce per venir fuori dal tunnel...
Vi posto sotto qualche stralcio di codice e spero qualcuno possa aiutarmi
Test (classe principale):
package videom;
import java.sql.SQLException;
public class VideoM {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
GUI frame = new GUI();
frame.Crea();
}
}
GUI
package test;
import java.awt.Container;
import java.awt.GridLayout;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
public class GUI extends JFrame {
public JTextField JTextFieldAggiungi;
public static JPanel panel2;
public void Crea() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
Container c = getContentPane();
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
[...]
panel2 = new JPanel();
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
instance = new Elenco();
panel2.add(instance.Elenco());
// Area di testo per inserire il punteggio
JTextFieldAggiungi = new JTextField(20);
JTextFieldAggiungi.setEditable(true);
panel3.add(JTextFieldAggiungi);
// Bottone inserimento punteggio
JButton buttonAggiungi = new JButton("Aggiungi");
buttonAggiungi.addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
try {
buttonAggiungiMouseClicked(evt);
} catch (ClassNotFoundException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
panel3.add(buttonAggiungi);
[...]
private void buttonAggiungiMouseClicked(java.awt.event.MouseEvent evt) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
Inserisci.insert(JTextFieldAggiungi.getText());
}
Elenco (file con l'SQL)
class Elenco extends JFrame{
public static TableModel tableModel;
public static JTable table;
private JScrollPane scrollPane;
public JScrollPane Elenco() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
ArrayList<Punti> listaPunti = new ArrayList<Punti>();
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/punti;user=andrea;password=andrea");
final java.sql.Statement stmt = conn.createStatement();
ResultSet results = stmt.executeQuery("SELECT * FROM punti");
while(results.next())
{
int a = results.getInt(1);
int b = results.getInt(2);
String c = results.getString(3);
String e = "1";
boolean d;
if (c.equals(e)) {
d = true;
} else {
d = false;
}
listaPunti.add(new Punti(a, b, d));
}
tableModel = new TableModel(listaPunti);
table = new JTable(tableModel);
scrollPane = new JScrollPane(table);
return scrollPane;
}
}
Ovviamente ci sono anche i file TableModel ed Inserisci ma non penso possano esservi utili al fine della risoluzione del problema.
A chiuque volesse darmi una mano gliene sarei grato
purtroppo ho qualche problema con il refresh delle jtalbe in tempo reale o per meglio dire dopo la pressione di un bottone.
Ho cominciato da nemmeno un mese e mi trovo un po' spaesato nel leggere le varie guide qualcuno potrebbe darmi una mano dandomi delle dritte, non mi serve necessariamente il codice, se c'è schifo non mi fa, ma una luce per venir fuori dal tunnel...
Vi posto sotto qualche stralcio di codice e spero qualcuno possa aiutarmi
Test (classe principale):
package videom;
import java.sql.SQLException;
public class VideoM {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
GUI frame = new GUI();
frame.Crea();
}
}
GUI
package test;
import java.awt.Container;
import java.awt.GridLayout;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
public class GUI extends JFrame {
public JTextField JTextFieldAggiungi;
public static JPanel panel2;
public void Crea() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
Container c = getContentPane();
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
[...]
panel2 = new JPanel();
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
instance = new Elenco();
panel2.add(instance.Elenco());
// Area di testo per inserire il punteggio
JTextFieldAggiungi = new JTextField(20);
JTextFieldAggiungi.setEditable(true);
panel3.add(JTextFieldAggiungi);
// Bottone inserimento punteggio
JButton buttonAggiungi = new JButton("Aggiungi");
buttonAggiungi.addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
try {
buttonAggiungiMouseClicked(evt);
} catch (ClassNotFoundException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
panel3.add(buttonAggiungi);
[...]
private void buttonAggiungiMouseClicked(java.awt.event.MouseEvent evt) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
Inserisci.insert(JTextFieldAggiungi.getText());
}
Elenco (file con l'SQL)
class Elenco extends JFrame{
public static TableModel tableModel;
public static JTable table;
private JScrollPane scrollPane;
public JScrollPane Elenco() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
ArrayList<Punti> listaPunti = new ArrayList<Punti>();
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/punti;user=andrea;password=andrea");
final java.sql.Statement stmt = conn.createStatement();
ResultSet results = stmt.executeQuery("SELECT * FROM punti");
while(results.next())
{
int a = results.getInt(1);
int b = results.getInt(2);
String c = results.getString(3);
String e = "1";
boolean d;
if (c.equals(e)) {
d = true;
} else {
d = false;
}
listaPunti.add(new Punti(a, b, d));
}
tableModel = new TableModel(listaPunti);
table = new JTable(tableModel);
scrollPane = new JScrollPane(table);
return scrollPane;
}
}
Ovviamente ci sono anche i file TableModel ed Inserisci ma non penso possano esservi utili al fine della risoluzione del problema.
A chiuque volesse darmi una mano gliene sarei grato