Skip to main content
gamedev.net
🔒 Locked

[java] JTable cell rendering

Started by gcs584 0 replies 2.6k views
Original Post
gcs584
gcs584
How is it going? So, I agreed to tackle this project for a family friend of mine. I thought it would give me beneficial programming experience plus I'll be getting paid some mula if I get it completed. That's a big IF. I'm currently about to give up. I've been at this JTable for a few hours now and no success. I haven't had much trouble with anything else except JTable. I haven't done Java programming for over a year, so this is probaly part of the reason for my struggles as well. I've been trying to use Java's tutorials, but I haven't had much success. The problem you might ask? I have a toolbar which is supposed to have various methods of formatting each individual cell of the JTable. I have a bold, underline, and italics button including some justify buttons to set differnt cells to different specifications at the users desire. I'm using a JInternalFrame obviously because I'll have several JTables. For now I have only constructed a JTable in the constructor (this will change). So I'm trying to figure out which interfaces or classes to implement to change the properties of an individual cell, and how to go about doing it. This question might be a bit involved; if you happen to have queries or this makes no sense....let me know. As said, I wanting to change the print and justification of an INDIVIDUAL cell. I do know how to get the (row, column) values of the JTable, but I don't know what to do with these coordinates. Any help would be much appreciated.


//extended JInternalFrame class that just constructs a JTable.

import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.*;
import java.awt.*;

public class custInternalFrame extends JInternalFrame
{

    static final int xOffset = 30, yOffset = 30;
    private JTable table;
    private String columnNames[] = {"A","B","C","D","E"};

    public custInternalFrame() {

        super("Document #1", true,true,true,true);

	Object[][] data = {
		{"Mary", "Campione",
		 "Snowboarding", new Integer(5), new Boolean(false)},
		{"Alison", "Huml",
		 "Rowing", new Integer(3), new Boolean(true)},
		{"Kathy", "Walrath",
		 "Knitting", new Integer(2), new Boolean(false)},
		{"Sharon", "Zakhour",
		 "Speed reading", new Integer(20), new Boolean(true)},
		{"Philip", "Milne",
		 "Pool", new Integer(10), new Boolean(false)}
        };

        table = new JTable(data, columnNames);

        //I figured out how to set the cells of a whole column but not INDIVIDUAL
	table.getColumnModel().getColumn(2).setCellRenderer(new cellRenderer());

	JScrollPane scrollPane = new JScrollPane(table);
	table.setPreferredScrollableViewportSize(new Dimension(500, 70));

	System.out.println( table.toString() );

        setSize(300,500);

        Container c = getContentPane();
	c.add( scrollPane, BorderLayout.WEST );
        setLocation(xOffset, yOffset);
        setVisible(true);
    }

}


And cellRenderer class:


import javax.swing.table.*;
import javax.swing.*;
import java.awt.*;

public class cellRenderer extends DefaultTableCellRenderer {

	public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected,boolean hasFocus,int row,int column) {


    	Component c = super.getTableCellRendererComponent(table, value,isSelected, hasFocus,row, column);

       	c.setFont( new Font("Times New Roman",Font.BOLD,14 ) );

        //c.setForeground(/*special foreground color*/);
       	//c.setBackground(/*special background color*/);

	    return c;
  	}

}


Thanks again.... GCS584 [EDIT:] Made some changes to my explanation.
gcs584
gcs584
Yup...I got this one solved. I might have a few more upcoming questions :)

Later,

GCS584

The java dev room definetly needs more posters...java rules!!

[EDIT:] Will post code & explanation tomorrow for future gamedevers.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.