ecco di seguito il codice
codice:
public class GridBagLayoutDemo {
static Database db;
static JTable table;
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
private static final int AUTO_RESIZE_OFF = 0;
JTextArea output;
JScrollPane scrollPane;
static JFrame frame;
public static Component addComponentsToPane(Container pane) {
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
JScrollPane jScrollPane1,jScrollPane2;
JLabel labelSpazio;
JLabel labelCodiceLocalita;
final JTextField textCodiceLocalita ;
JLabel labelOraPartenze;
final JTextField textOraPartenze;
JLabel labelCodice1Nota;
final JTextField textCodice1Nota;
JLabel labelCodice2Nota;
final JTextField textCodice2Nota;
JLabel labelOraArrivoDa;
final JTextField textOraArrivoDa;
JLabel labelOraPartenzaPer;
final JTextField textOraPartenzaPer;
JLabel labelCodice1NotaBis;
final JTextField textCodice1NotaBis;
JLabel labelCodice2NotaBis;
final JTextField textCodice2NotaBis;
JLabel labelOraArrivoPer;
final JTextField textOraArrivoPer;
JButton buttonInserisci;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}
inizializzaDatabase();
Font myFont = new Font("Verdana", Font.PLAIN, 15);
//devo aggiungere una tabella
Vector v = db.eseguiQuery( "SELECT * FROM archiviopartenzeferiali ;" );
Vector vettore = new Vector();
vettore.add(new String("CODICE LOCALITA"));
vettore.add(new String("ORA PARTENZA"));
vettore.add(new String("NOTA"));
vettore.add(new String("NOTA BIS"));
vettore.add(new String("ORA ARRIVO"));
vettore.add(new String("ORA PARTENZA PER"));
vettore.add(new String("NOTA"));
vettore.add(new String("NOTA BIS"));
vettore.add(new String("ORA DI ARRIVO PER"));
table = new TableExample(v, vettore);
//////codice..............
questo è il codice di tableexample che è copiato sempre in questa classe
class TableExample extends JTable {
private class MyMouseAdapter extends MouseAdapter {
public void mouseClicked(MouseEvent me) {
TableExample t = (TableExample)me.getSource();
if (me.getClickCount() == 2) {
// facciamo qualcosa... tu potresti ricavare il dato selezionato,
// e lanciare una nuova query.
int row = t.rowAtPoint(me.getPoint());
int column = t.columnAtPoint(me.getPoint());
Object prova= t.getValueAt(row, column);
elementiRicetta = db.eseguiQuery( "SELECT * FROM Ingredienti WHERE Descrizione = '"+prova+"' ;" );
System.out.println(prova);
addRow();
selectCell(i,0);
ricetteTabella.setValueAt(prova,i,0);
i++;
}
}
}
// serve solo ad inibire la funzione "principe" del doppio click su una JTable, ovver
// editare la cella target del doppio click.
public boolean isCellEditable(int row, int column) {
return false;
}
public TableExample(Vector v, Vector n) {
super(v, n);
this.addMouseListener(new MyMouseAdapter());
}
public void selectCell(int row,int col)
{
if(row!=-1 && col !=-1)
{
table.setRowSelectionInterval(row,row);
table.setColumnSelectionInterval(col,col);
}
}
public void addRow() //Add Row
{
Vector r=new Vector();
r=createBlankElement();
rows.addElement(r);
ricetteTabella.addNotify();
}
public Vector createBlankElement()
{
Vector t = new Vector();
t.addElement((String) " ");
t.addElement((String) " ");
t.addElement((String) " ");
t.addElement((String) " ");
t.addElement((String) " ");
t.addElement((String) " ");
t.addElement((String) " ");
return t;
}
}