codice:
package prove;
import java.awt.Color;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
public class Table extends javax.swing.JFrame {
public Table() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
combo1 = new javax.swing.JComboBox();
combo2 = new javax.swing.JComboBox();
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
table1 = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
combo1.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"Item 1", "Item 2", "Item 3", "Item 4"}));
combo2.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"A1", "A2", "A3"}));
jButton1.setText("Button");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
table1.setModel(new javax.swing.table.DefaultTableModel(
new Object[][]{
{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"
}));
jScrollPane1.setViewportView(table1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
addGroup(layout.createSequentialGroup().
addGap(22, 22, 22).
addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
addComponent(combo1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).
addComponent(combo2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).
addComponent(jButton1).addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)).
addContainerGap(77, Short.MAX_VALUE)));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
addGroup(layout.createSequentialGroup().
addGap(19, 19, 19).
addComponent(combo1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).
addGap(41, 41, 41).
addComponent(combo2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).
addGap(21, 21, 21).
addComponent(jButton1).addGap(71, 71, 71).
addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE).
addContainerGap(31, Short.MAX_VALUE)));
pack();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String string1 = (String) combo1.getSelectedItem();
String string2 = (String) combo2.getSelectedItem();
//Da qui parte la condzione
//se seleziono la string Item1, allora popola la tabella, colora la riga e incrementa l'indice delle riga
TableColumn tcol;
int index = 0;
if (string1.equals("Item 1")) {
for (int i = 0; i < table1.getRowCount(); i++) {
if (table1.getValueAt(i, 0) == null) {
index = i;
break;
}
}
int B = 5;
int index1 = 0;
for (int i = 0; i < B; i++) {
tcol = table1.getColumnModel().getColumn(index1);
tcol.setCellRenderer(new CustomTableCellRenderer(Color.RED));
index1 = i;
}
table1.setValueAt("pippo1", index, 0);
table1.setValueAt("A", index, 1);
table1.setValueAt("B", index, 2);
table1.setValueAt("C", index, 3);
}
if (string1.equals("Item 2")) {
for (int i = 0; i < table1.getRowCount(); i++) {
if (table1.getValueAt(i, 0) == null) {
index = i;
break;
}
}
int B = 5;
int index1 = 0;
for (int i = 0; i < B; i++) {
tcol = table1.getColumnModel().getColumn(index1);
tcol.setCellRenderer(new CustomTableCellRenderer(Color.RED));
index1 = i;
}
table1.setValueAt("pippo2", index, 0);
table1.setValueAt("A2", index, 1);
table1.setValueAt("B2", index, 2);
table1.setValueAt("C2", index, 3);
}
if (string1.equals("Item 3")) {
for (int i = 0; i < table1.getRowCount(); i++) {
if (table1.getValueAt(i, 0) == null) {
index = i;
break;
}
}
int B = 5;
int index1 = 0;
for (int i = 0; i < B; i++) {
tcol = table1.getColumnModel().getColumn(index1);
tcol.setCellRenderer(new CustomTableCellRenderer(Color.RED));
index1 = i;
}
table1.setValueAt("pippo3", index, 0);
table1.setValueAt("A3", index, 1);
table1.setValueAt("B3", index, 2);
table1.setValueAt("C3", index, 3);
}
}
public static void main(String args[]) {
new Table().setVisible(true);
}
// Variables declaration - do not modify
private javax.swing.JComboBox combo1;
private javax.swing.JComboBox combo2;
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable table1;
// End of variables declaration
public JComboBox getCombo1() {
return combo1;
}
public JComboBox getCombo2() {
return combo1;
}
public void setCombo2(JComboBox combo2) {
this.combo1 = combo2;
}
public JTable getTable1() {
return table1;
}
public void setTable1(JTable table1) {
this.table1 = table1;
}
}
//Class CustomTableCellRenderer.. Gestisce il renderer
package prove;
import com.sun.org.apache.regexp.internal.REDebugCompiler;
import java.awt.Color;
import java.awt.Component;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
public class CustomTableCellRenderer extends DefaultTableCellRenderer {
private Color color;
private Border border;
public CustomTableCellRenderer(Color color) {
super();
this.color = color;
}
@Override
public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, column);
if(table.getValueAt(row, column)==null){
cell.setBackground(null);
} else if(table.getValueAt(row, column)!=null){
cell.setBackground(color);
if (cell instanceof JLabel) {
((JLabel) cell).setOpaque(true);
((JLabel) cell).setBackground(color);
((JLabel) cell).setHorizontalAlignment(CENTER);
((JLabel) cell).setFont(new java.awt.Font("Calibri", 1, 12));
}
}
return cell ;
}
}