how to make a cell have two colors?

Hey all,just wondering if it's possible to make a cell to have two colors.split 50-50.thanks
[121 byte] By [avdzma] at [2007-11-26 21:11:08]
# 1
1) Learn how to ask a proper question. When you ask such a vague question you will not get any valid answers2) Learn to respond to previous questions when you recieve a reply. That way people know whether the suggestion you have been given was helpfull or not.
camickra at 2007-7-10 2:48:28 > top of Java-index,Desktop,Core GUI APIs...
# 2

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

avdzma at 2007-7-10 2:48:28 > top of Java-index,Desktop,Core GUI APIs...
# 3

> 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 .

sabre150a at 2007-7-10 2:48:28 > top of Java-index,Desktop,Core GUI APIs...
# 4

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);

}

}

}

Rodney_McKaya at 2007-7-10 2:48:28 > top of Java-index,Desktop,Core GUI APIs...
# 5
I know that the cell renderer can paint the cells with single colors,but can it print 2 colors?
avdzma at 2007-7-10 2:48:28 > top of Java-index,Desktop,Core GUI APIs...
# 6
thanks Rodney_McKay,i'll give it a go and let u know,once i got it working.
avdzma at 2007-7-10 2:48:28 > top of Java-index,Desktop,Core GUI APIs...