Well my bad,
thought the question was understandable.
I'll rephrase it,
I'm wondering if i could make a cell to have two different background colors, split 50-50, meaning that they're evenly divided.
My table is like an agenda
and i need to know if there are two events falling at the same time.
I hope this is more understandable
avdzm
> Well my bad,
> thought the question was understandable.
> I'll rephrase it,
The only context you gave was implicit in your posting in the Swing forum.
>
> I'm wondering if i could make a cell to have two
> different background colors, split 50-50, meaning
> that they're evenly divided.
So again no explicit context but I suppose you must mean a JTable but you could be referring to a JList cell or a JComboBox cell.
>
> My table is like an agenda
> and i need to know if there are two events falling at
> the same time.
Regardless of which type of cell you are referring to, the answer is to write a CellRenderer of the appropriate type. Since I bet you mean a JTable the you will need to write a TableCellRenderer - http://java.sun.com/docs/books/tutorial/uiswing/components/table.html .
This is just an example.
You will need to decide what you want to do when the cell is selected, or you will not see the text clearly when a row is selected.
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
public class TwoColorsTableCellTest {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
DefaultTableModel model = new DefaultTableModel(new String[][] {{"1", "2"}, {"3", "4"}}, new String[]{"col1", "col2"});
JTable table = new JTable(model);
table.setPreferredScrollableViewportSize(new Dimension(300, 300));
table.setDefaultRenderer(Object.class, new TwoColorsTableCellRendrer(Color.YELLOW, Color.BLUE));
JScrollPane pane = new JScrollPane(table);
frame.getContentPane().add(pane);
frame.pack();
frame.setVisible(true);
} catch (Exception e) {e.printStackTrace();}
}
private static class TwoColorsTableCellRendrer extends DefaultTableCellRenderer {
Color color1, color2;
public TwoColorsTableCellRendrer(Color c1, Color c2) {
color1 = c1;
color2 = c2;
setOpaque(false);
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
JComponent comp = (JComponent) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
return comp;
}
protected void paintComponent(Graphics g) {
g.setColor(color1);
g.fillRect(0, 0, getWidth()/2, getHeight());
g.setColor(color2);
g.fillRect(getWidth()/2, 0, getWidth()/2+1, getHeight());
super.paintComponent(g);
}
}
}