PDA

View Full Version : Problema JTable[java]


peppepayens
18-06-2010, 01:34
raga devo popolare un JTable a run time in un ActionPERFORMED
come faccio vi faccio vedere il codice

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;

public class RicercaCD extends JPanel implements ActionListener {

private GridBagConstraints ca = new GridBagConstraints();
private JLabel lab;
private JLabel titoloCD;
private JLabel autoreCD;
private JButton searchButton;
private JTextField ricercaTitolo;
private JTextField ricercaAutore;
private JRadioButton radioAction1;
private JRadioButton radioAction2;
private JRadioButton radioAction3;
private JTable tabellaCD;
private ArrayList<Archivio> arr;
private JPanel pannello=new JPanel();
private JPanel pannello2 = new JPanel();
private ButtonGroup bg = new ButtonGroup();
private Object[] columnNames = {"Titolo Album",
"Autore",
"#Tracce",
"#Tempo_Esec.",
"Prestito",
"Commento"};
public RicercaCD() {

setLab(new JLabel("RICERCA CD NEL DATABASE"));
setTitoloCD(new JLabel("Titolo Album"));
setAutoreCD(new JLabel("Autore"));
setSearchButton(new JButton("Ricerca"));
setRicercaTitolo(new JTextField(20));
setRicercaAutore(new JTextField(20));
setRadioAction1(new JRadioButton("Tutti"));
setRadioAction2(new JRadioButton("Disponibile"));
setRadioAction3(new JRadioButton("In Prestito"));


this.setLayout(new GridBagLayout());
// ca.insets=new Insets(10,10,50,0);
// this.setMaximumSize(getMaximumSize());
pannello.setLayout(new GridBagLayout());


ca.fill = GridBagConstraints.HORIZONTAL;
ca.insets=new Insets(10,75,10,0);
ca.weightx = 0.5;
ca.gridx = 3;
ca.gridy = 0;
pannello.add(getLab(),ca);

ca.fill = GridBagConstraints.HORIZONTAL;
ca.weightx = 0.5;
ca.gridx = 5;
ca.gridy = 2;
pannello.add(getRadioAction1(),ca);

ca.fill = GridBagConstraints.HORIZONTAL;
ca.weightx = 0.5;
ca.gridx = 5;
ca.gridy = 3;
pannello.add(getRadioAction2(),ca);

ca.fill = GridBagConstraints.HORIZONTAL;
ca.weightx = 0.5;
ca.gridx = 5;
ca.gridy = 4;
pannello.add(getRadioAction3(),ca);

ca.fill = GridBagConstraints.HORIZONTAL;
ca.ipadx=0;
ca.weightx = 0.5;
ca.gridx = 2;
ca.gridy = 3;
pannello.add(getTitoloCD(),ca);

ca.fill = GridBagConstraints.HORIZONTAL;
ca.ipady=5;
ca.weightx = 0.5;
ca.gridx = 3;
ca.gridy = 3;
pannello.add(getRicercaTitolo(),ca);

ca.fill = GridBagConstraints.HORIZONTAL;
ca.weightx = 0.5;
ca.gridx = 2;
ca.gridy = 4;
pannello.add(getAutoreCD(),ca);


ca.fill = GridBagConstraints.HORIZONTAL;
ca.ipady=5;
ca.weightx = 0.5;
ca.gridx = 3;
ca.gridy = 4;
pannello.add(getRicercaAutore(),ca);


ca.fill = GridBagConstraints.SOUTH;
ca.weightx = 0.5;
ca.gridx = 3;
ca.gridy = 5;
pannello.add(getSearchButton(),ca);


ca.fill = GridBagConstraints.NORTH;
ca.weightx = 0.5;
ca.gridx = 0;
ca.gridy = 0;
this.add(pannello,ca);
/*
* pannello2
* */
pannello2.setLayout(new BorderLayout());
pannello2.setMaximumSize(getMaximumSize());





Object[][] rowData={{"","", "","","",""},
{"","", "","","",""},

};
//
setTabellaCD(new JTable(rowData,getColumnNames()));

tabellaCD.setBackground(Color.WHITE);
tabellaCD.setPreferredScrollableViewportSize(new Dimension(500, 70));
tabellaCD.setFillsViewportHeight(true);
tabellaCD.setEnabled(false);
tabellaCD.setGridColor(Color.BLACK);
// tabellaCD.setTableHeader(Grid);
// tabellaCD.
// tabellaCD.setForeground(Color.BLACK);
tabellaCD.setFont(new Font("Baseline", Font.CENTER_BASELINE,14));
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(tabellaCD);
pannello2.add(scrollPane,BorderLayout.CENTER );
//
pannello2.add(tabellaCD.getTableHeader(), BorderLayout.NORTH);
//
ca.fill = GridBagConstraints.LAST_LINE_END;
ca.weightx = 0.5;
ca.gridx = 3;
ca.gridy = 8;
pannello2.add(tabellaCD, BorderLayout.CENTER);
// JScrollPane scrollPane = new JScrollPane(tabellaCD);
// tabellaCD.setsetVisible(true);//setFillsViewportHeight(true);
//
ca.fill = GridBagConstraints.SOUTH;
ca.weightx = 0.5;
ca.gridx = 0;
ca.gridy = 1;
this.add(pannello2,ca);


bg.add(radioAction1);
bg.add(radioAction2);
bg.add(radioAction3);
radioAction1.setSelected(true);
searchButton.addActionListener(this);


}

public void actionPerformed(ActionEvent e) {
String comando=e.getActionCommand();
if(comando.equalsIgnoreCase("Ricerca")) {
System.out.println("Ricerca informazioni nel Database");
String categoria ="CD";
String text1 = ricercaTitolo.getText();
String text2 = ricercaAutore.getText();
Ricerca search = new Ricerca(categoria);
search.setTipologia("RicercaUtente");
search.setAutore(text2);
search.setTitolo(text1);
if(radioAction1.isSelected()){
System.out.println("Tutti");
search.setPossesso("Tutti");
Client.RicercaCD(search);
ArrayList<Archivio> arr = new ArrayList<Archivio>();
arr=Client.RicercaCD(search);
it(arr);
// tabellaCD.remove(pannello2);
// JTable tabel = new JTable(it(arr),getColumnNames());




// tabel.add(pannello2,BorderLayout.SOUTH);
// tabel.setBackground(Color.WHITE);
// tabel.setPreferredScrollableViewportSize(new Dimension(500, 70));
// tabel.setFillsViewportHeight(true);
// tabel.setEnabled(false);
// tabel.setGridColor(Color.BLACK);
// this.getTabellaCD()
}
if(radioAction2.isSelected()){
System.out.println("Disponibile");
search.setPossesso("Disponibile");
Client.RicercaCD(search);
ArrayList<Archivio> arr = new ArrayList<Archivio>();
arr=Client.RicercaCD(search);
it(arr);
}
if(radioAction3.isSelected()){
System.out.println("In Prestito");
search.setPossesso("In Prestito");
Client.RicercaCD(search);
ArrayList<Archivio> arr = new ArrayList<Archivio>();
arr=Client.RicercaCD(search);
it(arr);
}
// System.out.println(text2+text1+search.getPossesso());
}

}
public void it(ArrayList<Archivio> arr){
CD cd;
Iterator iter = arr.iterator();
int i=0;
while(iter.hasNext()) {
cd=(CD) iter.next();
tabellaCD.getModel().setValueAt(cd.getTitolo(), i, 0);
tabellaCD.getModel().setValueAt(cd.getAutore(), i, 1);
tabellaCD.getModel().setValueAt(cd.getNumeroTracce(), i, 2);
tabellaCD.getModel().setValueAt(cd.getTempoEsecuzione(), i, 3);
tabellaCD.getModel().setValueAt(new Checkbox("",cd.getPrestito()), i, 4);
tabellaCD.getModel().setValueAt(cd.getCommento(), i, 5);
i++;
}

}

private void setTitoloCD(JLabel titoloCD) {
this.titoloCD = titoloCD;
}

private JLabel getTitoloCD() {
return titoloCD;
}

private void setAutoreCD(JLabel autoreCD) {
this.autoreCD = autoreCD;
}

private JLabel getAutoreCD() {
return autoreCD;
}

private void setSearchButton(JButton searchButton) {
this.searchButton = searchButton;
}

private JButton getSearchButton() {
return searchButton;
}

private void setLab(JLabel lab) {
this.lab = lab;
}

private JLabel getLab() {
return lab;
}

private void setRicercaAutore(JTextField ricercaAutore) {
this.ricercaAutore = ricercaAutore;
}

private JTextField getRicercaAutore() {
return ricercaAutore;
}

private void setRicercaTitolo(JTextField ricercaTitolo) {
this.ricercaTitolo = ricercaTitolo;
}

private JTextField getRicercaTitolo() {
return ricercaTitolo;
}

private void setRadioAction1(JRadioButton radioAction1) {
this.radioAction1 = radioAction1;
}

private JRadioButton getRadioAction1() {
return radioAction1;
}

private void setRadioAction2(JRadioButton radioAction2) {
this.radioAction2 = radioAction2;
}

private JRadioButton getRadioAction2() {
return radioAction2;
}

private void setRadioAction3(JRadioButton radioAction3) {
this.radioAction3 = radioAction3;
}

private JRadioButton getRadioAction3() {
return radioAction3;
}

public void setTabellaCD(JTable tabellaCD) {
this.tabellaCD = tabellaCD;
}

public JTable getTabellaCD() {
return tabellaCD;
}

public void setColumnNames(Object[] columnNames) {
this.columnNames = columnNames;
}

public Object[] getColumnNames() {
return columnNames;
}

}

peppepayens
18-06-2010, 01:36
devo fare una prova per il radio action 1 che è tutti
allora se notate mi arriva dal client una array che io devo leggere e mettere nelle colonne giuste di quel JTable
aiutatemi vi prego è l'ultima cosa che mi manca per il mio progetto
sto uscendo pazzo con i JTable....

peppepayens
18-06-2010, 11:30
raga mi potete aiutare per favore

peppepayens
18-06-2010, 11:51
a quale santo devo rivolgermi