Article View: borland.public.jbuilder.java.swing
Article #12828Re: Changin cell color in JTable
From: Alberto Robles
Date: Wed, 04 Nov 1998 00:00
Date: Wed, 04 Nov 1998 00:00
115 lines
3904 bytes
3904 bytes
Sorry, there is a little piece of code missing:
private void setUpColorRenderer(JTable table) {
table.setDefaultRenderer(TextWithBckg.class,
new ColorRenderer(true));
}
Alberto Robles wrote:
>
> Thanks Matt!
> I've succeed in changing JTable behaviour. This is an extract of my
> solution:
>
> public class Frame1 extends JFrame {
>
> TableModel1 myModel = new TableModel1();
> JTable jTable1 = new JTable(myModel); // Create the table with your
> own model.
> [...]
>
> public Frame1() {
> setUpColorRenderer(jTable1); // Change the Table render for Labels
> with Color.
> [...]
> }
>
> [...]
>
> class TableModel1 extends AbstractTableModel {
> Object[][] data = null; // This are the labels with different
> colors.
> [...]
>
> public TableModel1(){
> [...]
> data = new Object[Max][1];
> for(int i=1;i<= Max;i++){
> TextWithBckg text= new TextWithBckg(" "+i+"
> ",Color.lightGray);
> data[i-1][0]=(Object)text;
> }
> }
> [...]
> }
>
> class ColorRenderer extends JLabel implements TableCellRenderer {
> Border unselectedBorder = null;
> Border selectedBorder = null;
> boolean isBordered = true;
>
> public ColorRenderer(boolean isBordered) {
> super();
> this.isBordered = isBordered;
> setOpaque(true); //MUST do this for background to show up.
> }
>
> public Component getTableCellRendererComponent(
> JTable table, Object b,
> boolean isSelected, boolean hasFocus,
> int row, int column) {
> if (b!=null){
> TextWithBckg data=(TextWithBckg)b;
> setText(data.text);
> setHorizontalAlignment(com.sun.java.swing.SwingConstants.RIGHT);
> setBackground(data.color);
> }
> if (isBordered) {
> if (isSelected) {
> if (selectedBorder == null) {
> selectedBorder = BorderFactory.createMatteBorder(2,5,2,5,
>
> table.getSelectionBackground());
> }
> setBorder(selectedBorder);
> } else {
> if (unselectedBorder == null) {
> unselectedBorder = BorderFactory.createMatteBorder(2,5,2,5,
>
> table.getBackground());
> }
> setBorder(unselectedBorder);
> }
> }
>
> return this;
> }
> }
>
> }
>
> class TextWithBckg {
>
> String text;
> Color color;
>
> public TextoConfondo(String t, Color c){
> text=t;
> color=c;
> }
> }
>
> And that's all necessary changes. Please, note that this is just an
> extract and must be completed with all necessary methods. The URL
> http://java.sun.com/docs/books/tutorial/ui/swing/table.html contains all
> hints to do it.
>
> Alberto.
--
--------------------------------------------------------------------
Alberto Robles | Contrary to popular belief, Unix is user friendly.
Tecsidel,S.A. | It just happens to be selective about who it makes
+34-93-2922110 | friends with.
Technical Details
Message-ID:
<36409466.C0AEC344@tecsidel.es>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<363F2A8D.D2D20E56@tecsidel.es> <71nrtf$8iv2@forums.borland.com> <36408CAB.9A68B62C@tecsidel.es>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,dbd640fd7c418ab6 X-Google-Attributes: gid111d45,public From: Alberto Robles <alberto.robles@tecsidel.es> Subject: Re: Changin cell color in JTable Date: 1998/11/04 Message-ID: <36409466.C0AEC344@tecsidel.es> X-Deja-AN: 408361807 Content-Transfer-Encoding: 7bit References: <363F2A8D.D2D20E56@tecsidel.es> <71nrtf$8iv2@forums.borland.com> <36408CAB.9A68B62C@tecsidel.es> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii Organization: TECSIDEL Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.swing