il codice che io ho postato è completo nel senso che tutto quello che io ho inserito è quello che serve cmq visto che me lo hai fatto notare, inserisco l'intera classe così come sta scritta.
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) {
c.fill = GridBagConstraints.HORIZONTAL;
}
inizializzaDatabase();
Font myFont = new Font("Verdana", Font.PLAIN, 15);
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);
System.out.println("Sono qui");
jScrollPane1 = new javax.swing.JScrollPane();
System.out.println("Sono qui");
jScrollPane1.setName("jScrollPane1");
System.out.println("Sono qui");
table.getColumnModel().getColumn(0).setPreferredWidth(100);
table.getColumnModel().getColumn(1).setPreferredWidth(180);
table.getColumnModel().getColumn(2).setPreferredWidth(50);
table.getColumnModel().getColumn(3).setPreferredWidth(50);
table.getColumnModel().getColumn(4).setPreferredWidth(180);
table.getColumnModel().getColumn(5).setPreferredWidth(180);
table.getColumnModel().getColumn(6).setPreferredWidth(50);
table.getColumnModel().getColumn(7).setPreferredWidth(50);
table.getColumnModel().getColumn(8).setPreferredWidth(180);
DefaultTableCellRenderer renderer_archivi = new DefaultTableCellRenderer();
renderer_archivi.setHorizontalAlignment(SwingConstants.CENTER);
table.getColumnModel().getColumn(1).setCellRenderer(renderer_archivi);
table.getColumnModel().getColumn(4).setCellRenderer(renderer_archivi);
table.getColumnModel().getColumn(5).setCellRenderer(renderer_archivi);
table.getColumnModel().getColumn(8).setCellRenderer(renderer_archivi);
jScrollPane1.getViewport().add(table);
jScrollPane1.setVisible(true);
c.ipady=82;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(jScrollPane1, c);
labelCodiceLocalita = new JLabel("Codice Località: ");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady=10;
c.gridx = 0;
c.gridy = 1;
labelCodiceLocalita.setFont(myFont);
pane.add(labelCodiceLocalita, c);
final JComboBox comboBox = new JComboBox();
comboBox.addItem("Snowboarding");
comboBox.addItem("Rowing");
comboBox.addItem("Knitting");
comboBox.addItem("Speed reading");
comboBox.addItem("Pool");
comboBox.addItem("None of the above");
c.weightx = 0.5;
Object[] localita = db.eseguiQuery("SELECT LOCALITA FROM archiviolocalita;").toArray();
for(int i=0; i<localita.length; i++){
String temp = localita[i].toString();
temp = elimina_caratteri(temp);
localita[i] = temp;
}
comboBox.setModel(new javax.swing.DefaultComboBoxModel(localita));
if (shouldWeightX) {
c.weightx = 0.5;
}
c.ipady=1;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
pane.add(comboBox, c);
labelOraPartenze = new JLabel("Ora Partenza: ");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 3;
labelOraPartenze.setFont(myFont);
pane.add(labelOraPartenze, c);
textOraPartenze = new JTextField();
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 1;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 4;
pane.add(textOraPartenze, c);
labelCodice1Nota = new JLabel("Codice 1 nota");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 5;
labelCodice1Nota.setFont(myFont);
pane.add(labelCodice1Nota, c);
textCodice1Nota = new JTextField();
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 1;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 6;
pane.add(textCodice1Nota, c);
labelCodice2Nota = new JLabel("Codice 2 Nota: ");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 7;
labelCodice2Nota.setFont(myFont);
pane.add(labelCodice2Nota, c);
textCodice2Nota = new JTextField();
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 1;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 8;
pane.add(textCodice2Nota, c);
labelOraArrivoDa = new JLabel("Ora arrivo da: ");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 9;
labelOraArrivoDa.setFont(myFont);
pane.add(labelOraArrivoDa, c);
textOraArrivoDa = new JTextField();
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 1;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 10;
pane.add(textOraArrivoDa, c);
labelOraPartenzaPer = new JLabel("Ora partenza per: ");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20; //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 11;
labelOraPartenzaPer.setFont(myFont);
pane.add(labelOraPartenzaPer, c);
textOraPartenzaPer = new JTextField();
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 1; //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 12;
pane.add(textOraPartenzaPer, c);
labelCodice1NotaBis = new JLabel("Codice 1° nota bis: ");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20; //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 13;
labelCodice1NotaBis.setFont(myFont);
pane.add(labelCodice1NotaBis, c);
textCodice1NotaBis = new JTextField();
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 1; //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 14;
pane.add(textCodice1NotaBis, c);
labelCodice2NotaBis = new JLabel("Codice 2° nota bis: ");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20; //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 15;
labelCodice2NotaBis.setFont(myFont);
pane.add(labelCodice2NotaBis, c);
textCodice2NotaBis = new JTextField();
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 1; //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 16;
pane.add(textCodice2NotaBis, c);
labelOraArrivoPer = new JLabel("Ora Arrivo per: ");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20; //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 17;
labelOraArrivoPer.setFont(myFont);
pane.add(labelOraArrivoPer, c);
textOraArrivoPer = new JTextField();
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 1;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 18;
pane.add(textOraArrivoPer, c);
labelSpazio = new JLabel("");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 10; //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 19;
pane.add(labelSpazio, c);
buttonInserisci = new JButton();
buttonInserisci.setText("Salva");
c.fill = GridBagConstraints.VERTICAL;
c.ipady = 10; //make this component tall
c.weightx = 0.0;
c.gridheight = 10;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 20;
pane.add(buttonInserisci, c);
buttonInserisci.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
String localita = comboBox.getSelectedItem().toString();
String codice =db.eseguiQuery("SELECT CODICE FROM archiviolocalita WHERE localita = '"+localita+"' ;").toString();
codice = elimina_caratteri(codice);
int codice2 = Integer.parseInt(codice);
String oraPartenza = textOraPartenze.getText();
int codice1Nota = Integer.parseInt(textCodice1Nota.getText());
int codice2Nota = Integer.parseInt(textCodice2Nota.getText());
String oraArrivoDa = textOraArrivoDa.getText();
String oraPartenzaPer = textOraPartenzaPer.getText();
int codice1NotaBis = Integer.parseInt(textCodice1NotaBis.getText());
int codice2NotaBis = Integer.parseInt(textCodice2NotaBis.getText());
String oraArrivoPer = textOraArrivoPer.getText();
db.eseguiAggiornamento("INSERT INTO archiviopartenzeferiali VALUES ('"+codice2+"', '"+oraPartenza+"', '"+codice1Nota+"', '"+codice2Nota+"', '"+oraArrivoDa+"', '"+oraPartenzaPer+"', '"+codice1NotaBis+"', '"+codice2NotaBis+"', '"+oraArrivoPer+"') ;");
}
});
JTextField textOraArrivoper2 = new JTextField();
textOraArrivoper2.setVisible(false);
return textOraArrivoper2;
}
private static String elimina_caratteri(String numeroPartenzeFeriali) {
numeroPartenzeFeriali = numeroPartenzeFeriali.replace("]", "");
numeroPartenzeFeriali = numeroPartenzeFeriali.replace("[", "");
return numeroPartenzeFeriali;
}
private static void inizializzaDatabase() {
// TODO Auto-generated method stub
db = new Database("partenzearrivi");
if ( !db.connetti() ) {
System.out.println("Errore durante la connessione al database :(");
System.out.println( db.getErrore() );
System.exit(0);
}
else{
System.out.println("Mi sono connesso");
}
}//fine inizializzaDatabase
static void createAndShowGUI() {
frame = new JFrame("GridBagLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public Container createContentPane() {
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setOpaque(true);
output = new JTextArea(5, 30);
output.setEditable(false);
scrollPane = new JScrollPane(output);
addComponentsToPane(frame.getContentPane());
return contentPane;
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
volevo solo evitare di fare perdere tempo a chi mi vuole aiutare di perdersi tra il codice