codice:
Toolkit t = Toolkit.getDefaultToolkit();
Dimension screenSize = t.getScreenSize();
double width = screenSize.getWidth();
double height= screenSize.getHeight();
double frameWidth = width;
double frameHeight = height;
Dimension d2 = new Dimension();
d2.setSize(frameWidth, frameHeight);
this.setPreferredSize(d2);
Dimension dTabella = new Dimension();
double frameWidth2 = frameWidth * 0.20;
double frameHeight2 = frameHeight * 0.50;
dTabella.setSize(frameWidth2, frameHeight2);
Dimension dricetteTabella = new Dimension();
double frameWidth3 = frameWidth * 0.70;
System.out.println("la larghezza della tabella ricetteTabella vale " + frameWidth3);
double frameHeight3 = frameHeight * 0.50;
System.out.println("la altezza della tabella ricetteTabella vale " + frameHeight3);
dricetteTabella.setSize(frameWidth3, frameHeight3);
fontTable = new Font("Arial" ,Font.PLAIN, 20);
//Carico l'intero database all'avvio.
Vector v = db.eseguiQuery( "SELECT Descrizione FROM Ingredienti ORDER BY tipo;" );
Vector vettore = new Vector();//do nome alle colonne
vettore.add(new String("Ingrediente"));
table = new TableExample(v, vettore);
table.setPreferredScrollableViewportSize(dTabella);
table.setRowHeight(25);
table.setFillsViewportHeight(true);
table.setDefaultRenderer(Object.class, new TableCellRenderer() {
//metodo dell'interfaccia
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column){
//dobbiamo rappresentare oggetti testuali, quindi ci serve una JLabel
JLabel label = new JLabel(value.toString());
//appena creata una jlabel non può avere lo sfondo (o meglio, è trasparente);
//per renderlo possibile rendiamo la nostra label opaca
label.setOpaque(true);
//le assegnamo lo sfondo che una cella non selezionata e non colorata dovrà avere
label.setBackground(Color.WHITE);
String tipo = db.eseguiQuery("SELECT tipo FROM ingredienti WHERE descrizione = '"+value.toString()+"';").toString();
tipo = elimina_caratteri(tipo);
if(tipo.equalsIgnoreCase("zucchero")){
label.setBackground(Color.red);
}
if(tipo.equalsIgnoreCase("grasso")){
label.setBackground(Color.green);
}
if(tipo.equalsIgnoreCase("sngl")){
label.setBackground(Color.CYAN);
}
if(tipo.equalsIgnoreCase("altri solidi")){
label.setBackground(Color.magenta);
}
if(hasFocus){
//se la cella è selezionata, sovrascriviamo il suo sfondo
//attuale e rendiamola di colore "focus"
label.setBackground(focus);
}
return label;
}
});
jScrollPane1=new JScrollPane(table);
//preparo la seconda tabella
rows=new Vector();
tableModel = new MyTableModel();
// ricetteTabella = new JTable(new MyTableModel());
ricetteTabella = new JTable(tableModel);
ricetteTabella.setFont(fontTable);
ricetteTabella.setRowHeight(25);
ricetteTabella.setDropMode(DropMode.INSERT_ROWS);
table.setDragEnabled(true);
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
if (SwingUtilities.isLeftMouseButton(me) && me.getClickCount() % 2 == 0) {
System.out.println("Drag and drop");
TableExample t = (TableExample)me.getSource();
int row = t.rowAtPoint(me.getPoint());
int column = t.columnAtPoint(me.getPoint());
Object prova= t.getValueAt(row, column);
System.out.println("l'lemento che andrò a prendere è " +prova);
ricetteTabella.setValueAt(prova,i,0);
// String text = (String)model.getElementAt(0);
// String[] rowData = text.split(",");
// tableModel.insertRow(table.getRowCount(), rowData);
// model.removeAllElements();
model.insertElementAt(getNextString(i++), 0);
}
}
});
ricetteTabella.setPreferredScrollableViewportSize(dricetteTabella);
ricetteTabella.getColumnModel().getColumn(0).setPreferredWidth(190);
ricetteTabella.getColumnModel().getColumn(1).setPreferredWidth(20);
ricetteTabella.getColumnModel().getColumn(2).setPreferredWidth(50);
ricetteTabella.getColumnModel().getColumn(3).setPreferredWidth(50);
ricetteTabella.getColumnModel().getColumn(4).setPreferredWidth(50);
ricetteTabella.getColumnModel().getColumn(5).setPreferredWidth(50);
ricetteTabella.getColumnModel().getColumn(6).setPreferredWidth(50);
ricetteTabella.getColumnModel().getColumn(7).setPreferredWidth(25);
ricetteTabella.getColumnModel().getColumn(8).setPreferredWidth(25);
jScrollPane2= new JScrollPane(ricetteTabella);
//creo il layout
this.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.gridwidth=4;
c.gridheight=2;
c.anchor= GridBagConstraints.NORTHEAST;
this.add(jScrollPane2, c);
//c.ipady=50;
c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.insets.left=20;
c.gridx = 4;
c.gridy = 0;
c.anchor= GridBagConstraints.NORTHWEST;
this.add(jScrollPane1, c);
JLayeredPane layeredPane = new JLayeredPane();
Dimension pane = new Dimension();
pane.setSize(frameWidth3,frameHeight3*0.30);
System.out.println("la larghezza del pane vale " + frameWidth3);
System.out.println("la larghezza della pane vale " + frameHeight3*0.3);
layeredPane.setPreferredSize(pane);
layeredPane.setBorder(BorderFactory.createTitledBorder(
"Riquadro attività"));
layeredPane.setLayout(new GridLayout(4,7,10,10));
c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 3;
c.gridy = 2;
this.add(layeredPane, c);
//dichiarazione delle text
supportoDif = new JFormattedTextField();
supportoDif.setValue(new Double(PercentualeDif));
supportoDif.setBackground(Color.red);
quantitaDif = new JFormattedTextField();
quantitaDif.setValue(new Double(QuantitaDif));
quantitaDif.setBackground(Color.red);
quantitaDif.addPropertyChangeListener("value", this);
zuccheriDif = new JFormattedTextField();
zuccheriDif.setValue(new Double(ZuccheriDif));
zuccheriDif.setBackground(Color.red);
zuccheriDif.addPropertyChangeListener("value", this);
grassiDif = new JFormattedTextField();
grassiDif.setValue(new Double(GrassiDif));
grassiDif.setBackground(Color.red);
grassiDif.addPropertyChangeListener("value", this);
snglDif = new JFormattedTextField();
snglDif.setValue(new Double(SnglDif));
snglDif.setBackground(Color.red);
snglDif.addPropertyChangeListener("value", this);
altriSolidiDif = new JFormattedTextField();
altriSolidiDif.setValue(new Double(AltriSolidiDif));
altriSolidiDif.setBackground(Color.red);
altriSolidiDif.addPropertyChangeListener("value", this);
JLabel labelDifferenza = new JLabel("Quantità Mancanti: ");
JLabel labelSomma = new JLabel("Somma: ");
JLabel labelObiettivo = new JLabel("Obiettivo: ");
supportoSom = new JFormattedTextField();
supportoSom.setValue(new Double(DifferenzaSom));
supportoSom.setBackground(Color.blue);
quantitaSom = new JFormattedTextField();
quantitaSom.setValue(new Double(QuantitaSom));
quantitaSom.setBackground(Color.cyan);
quantitaSom.addPropertyChangeListener("value", this);
zuccheriSom = new JFormattedTextField();
zuccheriSom.setValue(new Double(ZuccheriSom));
zuccheriSom.setBackground(Color.cyan);
zuccheriSom.addPropertyChangeListener("value", this);
grassiSom = new JFormattedTextField();
grassiSom.setValue(new Double(GrassiSom));
grassiSom.setBackground(Color.cyan);
grassiSom.addPropertyChangeListener("value", this);
snglSom = new JFormattedTextField();
snglSom.setValue(new Double(SnglSom));
snglSom.setBackground(Color.cyan);
snglSom.addPropertyChangeListener("value", this);
altriSolidiSom = new JFormattedTextField();
altriSolidiSom.setValue(new Double(AltriSolidiSom));
altriSolidiSom.setBackground(Color.cyan);
altriSolidiSom.addPropertyChangeListener("value", this);
supportoRip = new JFormattedTextField();
supportoRip.setValue(new Double(DifferenzaRip));
supportoRip.setBackground(Color.green);
quantitaRip = new JFormattedTextField();
quantitaRip.setValue(new Double(Quantita));
quantitaRip.setBackground(Color.green);
zuccheriRip = new JFormattedTextField();
zuccheriRip.setValue(new Double(ZuccheriRip));
zuccheriRip.setBackground(Color.green);
grassiRip = new JFormattedTextField();
grassiRip.setPreferredSize(new Dimension(1000,10));
grassiRip.setValue(new Double(GrassiRip));
grassiRip.setBackground(Color.green);
snglRip = new JFormattedTextField();
snglRip.setValue(new Double(SnglRip));
snglRip.setBackground(Color.green);
altriSolidiRip = new JFormattedTextField();
altriSolidiRip.setValue(new Double(AltriSolidiRip));
altriSolidiRip.setBackground(Color.green);
JLabel lperc= new JLabel("in %");
JLabel lqua= new JLabel("Quantità");
JLabel lzuc= new JLabel("Zuccheri");
JLabel lgra= new JLabel("Grassi");
JLabel lsng= new JLabel("Sngl");
JLabel laltr= new JLabel("AltriSolidi");
JLabel lltr= new JLabel("");
layeredPane.add(lltr);
layeredPane.add(lperc);
layeredPane.add(lqua);
layeredPane.add(lzuc);
layeredPane.add(lgra);
layeredPane.add(lsng);
layeredPane.add(laltr);
layeredPane.add(labelDifferenza);
layeredPane.add(supportoDif);
layeredPane.add(quantitaDif);
layeredPane.add(zuccheriDif);
layeredPane.add(grassiDif);
layeredPane.add(snglDif);
layeredPane.add(altriSolidiDif);
layeredPane.add(labelSomma);
layeredPane.add(supportoSom);
layeredPane.add(quantitaSom);
layeredPane.add(zuccheriSom);
layeredPane.add(grassiSom);
layeredPane.add(snglSom);
layeredPane.add(altriSolidiSom);
layeredPane.add(labelObiettivo);
layeredPane.add(supportoRip);
layeredPane.add(quantitaRip);
layeredPane.add(zuccheriRip);
layeredPane.add(grassiRip);
layeredPane.add(snglRip);
layeredPane.add(altriSolidiRip);
JLayeredPane layeredPane2 = new JLayeredPane();
Dimension paneLayered2 = new Dimension();
pane.setSize(frameWidth2,frameHeight2*0.35);
layeredPane2.setPreferredSize(pane);
layeredPane2.setBorder(BorderFactory.createTitledBorder(
"Percentuali Ingredienti"));
layeredPane2.setLayout(new GridLayout(4,2,2,10));
c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 4;
c.insets.left=100;
c.gridy = 2;
this.add(layeredPane2, c);
spero mi possiate dare una mano in questo caso le text le ho inserito in un jlayeredpane con layer boxgrid