package displayresult;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
public class DisplayResult extends JFrame {
public void getTableCellRendererComponent(CustomCellRenderer getTableCellRendererComponent) {
}
public String pippo;
public String setBg;
public Object getValueAt;
public static void main(String[] args){
DisplayResult frame = new DisplayResult();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize(500, 150);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public DisplayResult()
{
super("tu surela");
ArrayList columnNames = new ArrayList();
ArrayList data = new ArrayList();
int columns = 4;
// Get column names
String A="";
for (int i = 1; i <= columns; i++) {
String Ii = String.valueOf(i);
String columnVal = A+Ii;
columnNames.add(columnVal );
}
// Get row data
for (int zz = 1; zz <= 5; zz++)
{
ArrayList row = new ArrayList(columns);
for (int i = 1; i <= columns; i++)
{
String Zz = String.valueOf(zz);
String Ii = String.valueOf(i * 3.5);
pippo = Ii+Zz;
row.add(pippo);
}
data.add( row );
}
// Create Vectors and copy over elements from ArrayLists to them
// Vector is deprecated but I am using them in this example to keep
// things simple - the best practice would be to create a custom defined
// class which inherits from the AbstractTableModel class
Vector columnNamesVector = new Vector();
Vector dataVector = new Vector();
for (int i = 0; i < data.size(); i++)
{
ArrayList subArray = (ArrayList)data.get(i);
Vector subVector = new Vector();
for (int j = 0; j < subArray.size(); j++)
{
subVector.add(subArray.get(j));
}
dataVector.add(subVector);
}
for (int i = 0; i < columnNames.size(); i++ ) {
columnNamesVector.add(columnNames.get(i));
}
JTable table = new JTable(dataVector, columnNamesVector);
table.setFont(new Font("Verdana", Font.PLAIN, 11));
table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
for (int column = 0; column < table.getColumnCount(); column++) {
TableColumn tableColumn = table.getColumnModel().getColumn(column);
int preferredWidth = tableColumn.getMinWidth();
int maxWidth = tableColumn.getMaxWidth();
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
for (int row = 0; row < table.getRowCount(); row++) {
TableCellRenderer cellRenderer = table.getCellRenderer(row, column);
Object value = table.getValueAt(row, column);
Component cell = table.prepareRenderer(cellRenderer, row, column);
//TheClass.theMethod();
//CustomCellRenderer.getTableCellRendererComponent(table, value, true, true, row, column); <-------------------------
Color mycolor = new Color(0, 0, 255);
cell.setBackground(mycolor);
int width = cell.getPreferredSize().width + table.getIntercellSpacing().width;
preferredWidth = Math.max(preferredWidth, width);
// We've exceeded the maximum width, no need to check other rows
if (preferredWidth >= maxWidth) {
preferredWidth = maxWidth;
break;
}
}
tableColumn.setPreferredWidth( preferredWidth );
for (int row = 0; row < table.getColumnCount(); row++) {
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment(JLabel.CENTER);
table.getColumnModel().getColumn(row).setCellRenderer(centerRenderer);
}
}
JScrollPane scrollPane = new JScrollPane( table);
//JScrollPane sscrollPane =
new JScrollPane(scrollPane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
Component add = getContentPane().add( scrollPane );
JPanel buttonPanel = new JPanel();
getContentPane().add( buttonPanel, BorderLayout.NORTH );
}
public class CustomCellRenderer extends JLabel implements TableCellRenderer {
public TableCellRenderer defaultCellRenderer;
public CustomCellRenderer(TableCellRenderer defaultCellRenderer) {
this.defaultCellRenderer = defaultCellRenderer;
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
String val = (String) value;
Component c = defaultCellRenderer.getTableCellRendererComponent(table, value, true, true, row, column);
if (c != null) {
if (val.equals("0h")) {
c.setForeground(Color.RED);
} else {
c.setForeground(table.getForeground());
}
return c;
} else return null;
}
}
}