Ciao a tutti, ho problemi nel far visualizzare una tabella, vi spiego in dettaglio il mio probleme e copio il codice.
Allora ho un frame(il main) in cui sono presenti:
bottone avvia---->creo oggetto tabella, ma non la faccio visualizzare
bottone inserisci --->chiamo oggetto.inserisci(parametro1, parametro2) per l'inserimento
bottone visualizza-----> setVisible(true); MA OVVIAMENTE LA TABELLA è VUOTA
La tabella viene creata in un altro file dove c'è anche il metoto inserisci.
Il codice del main è:
package main;
public class frame extends javax.swing.JFrame {
Tabella tab;
public frame() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
bInserisci = new javax.swing.JButton();
bVisualizza = new javax.swing.JButton();
bAvvia = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
bInserisci.setText("Inserisci ");
bInserisci.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bInserisciActionPerformed(evt);
}
});
bVisualizza.setText("Visualizza");
bVisualizza.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bVisualizzaActionPerformed(evt);
}
});
bAvvia.setText("Avvia ");
bAvvia.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bAvviaActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(83, 83, 83)
.addComponent(bInserisci)
.addGap(69, 69, 69)
.addComponent(bVisualizza))
.addGroup(layout.createSequentialGroup()
.addGap(158, 158, 158)
.addComponent(bAvvia)))
.addContainerGap(96, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(32, 32, 32)
.addComponent(bAvvia)
.addGap(61, 61, 61)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(bInserisci)
.addComponent(bVisualizza))
.addContainerGap(161, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void bInserisciActionPerformed(java.awt.event.ActionEvent evt) {
String valore1="prova1";
String valore2="prova2";
tab.inserisci(valore1, valore2);
}
private void bAvviaActionPerformed(java.awt.event.ActionEvent evt) {
tab=new Tabella();
tab.setVisible(false);
}
private void bVisualizzaActionPerformed(java.awt.event.ActionEvent evt) {
tab.setVisible(true);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new frame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton bAvvia;
private javax.swing.JButton bInserisci;
private javax.swing.JButton bVisualizza;
// End of variables declaration
}
Il codice dell'altra classe:
package main;
import javax.swing.table.DefaultTableModel;
public class Tabella extends javax.swing.JFrame {
DefaultTableModel modello;
public Tabella() {
initComponents();
this.setLocationRelativeTo(null);
modello=new DefaultTableModel();
tabella.setModel(modello);
};
public void inserisci(String primo, String secondo){
modello.addRow(new Object[]{primo,secondo});
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
scroll = new javax.swing.JScrollPane();
tabella = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
tabella.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
scroll.setViewportView(tabella);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(scroll, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(15, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(105, Short.MAX_VALUE)
.addComponent(scroll, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(87, 87, 87))
);
pack();
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JScrollPane scroll;
private javax.swing.JTable tabella;
// End of variables declaration
}
Ho dei dubbi se l'inserimento con addRow sia corretto, è fatto bene?
Come posso fare per far visualizzare la tabella?
Grazie mille a tutti