Results 1 to 6 of 6
  1. #1
    Muskar is offline Member
    Join Date
    Nov 2010
    Posts
    73
    Rep Power
    0

    Default Problems making a JTable's cells transparent

    Hey, I'm trying to make the cells in my JTable transparent.

    Now, I only have one column, and no header.
    tblInboxDisplay is a 'JTable' and cellColor is a 'Color'.
    The following piece of code sets the background colour of the cell in (0,0) to "cellColor":
    Java Code:
    tblInboxDisplay
        .getCellRenderer(0, 0)
        .getTableCellRendererComponent(tblInboxDisplay, null,
                false, false, 0, 0)
        .setBackground(cellColor);
    However, a cellRenderer does not have a setOpaque(boolean opaque) method.

    So is there a simple (or advanced) way to do this?
    Last edited by Muskar; 12-09-2010 at 05:56 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,416
    Rep Power
    20

    Default

    a cellRenderer does not have a setOpaque(boolean opaque) method.
    No, but DefaultTableCellRenderer does. If you haven't used a custom renderer you can cast the value returned by getCellRenderer() and call the method.

    To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.

    db

  3. #3
    Muskar is offline Member
    Join Date
    Nov 2010
    Posts
    73
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    No, but DefaultTableCellRenderer does. If you haven't used a custom renderer you can cast the value returned by getCellRenderer() and call the method.

    To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.

    db
    Yeah sorry, I'm just dizzy from all the hours of programming and lack of sleep - I usually post by SSCCE.

    I'm not thinking straight atm but I think the below code should be SSCCE (except it's missing a main method - although I'm not sure exactly how to make one that runs with a static object of the class it's in when it has a constructor):
    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableCellRenderer;
    import javax.swing.table.DefaultTableModel;
    
    public class Test extends JFrame{
    	private JTable tblInboxDisplay;
    	private DefaultTableModel inboxTableModel;
    	private DefaultTableCellRenderer inboxTableCellRenderer = new DefaultTableCellRenderer();
    	public Test() {
    		this.setSize(250, 270);
    		this.setLocation(100, 100);
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setLayout(null);
    		
    		tblInboxDisplay = new JTable();
    		tblInboxDisplay.setSize(205, 220);
    		tblInboxDisplay.setLocation(5, 5);
    		tblInboxDisplay.setOpaque(false);
    		
    		//Sets the JTable to have one row and one column, so one cell total,
    		//with a String in that cell.
    		String[][] startMessage = {{"No Messages!"}};
    		inboxTableModel = new DefaultTableModel(startMessage, new String[] {""});
    		tblInboxDisplay.setModel(inboxTableModel);
    		
    			[color="#3F7F5F"]//From this and below is the question[/COLOR]
    		//Is "getClass()" correct? I don't understand what exactly the API says it uses that class for.
    		tblInboxDisplay.setDefaultRenderer(getClass(), inboxTableCellRenderer);
    		
    		//This doesn't make the cells transparent
    		inboxTableCellRenderer.setOpaque(false);
    		
    		//I've also tried getting a specific cell - but this was all I could find:
    		inboxTableCellRenderer.getTableCellRendererComponent(tblInboxDisplay, null, false, false, 0, 0);
    		
    		this.setVisible(true);
    	}
    }

    Btw, if anyone know it would be even better if I could put an ImageIcon as background on my cells, if possible. So if you guys know anything about that, I'd be delighted if you could guide me in the right direction.
    Last edited by Muskar; 12-09-2010 at 08:26 PM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I'm not sure exactly how to make one that runs with a static object of the class it's in when it has a constructor

    Java Code:
    public static void main(String[] args) {
        new Test();
    }

    As written you are not actually adding the table to the frame, so nothing will appear on the screen.

    Have you tried Darryl's suggestion (get the cell renderer, cast it to a DefaultTableCellRenderer and call setOpaque())?

    As for using an icon you might want to have a look at Using Custom Renderers in How to Use Tables from Oracle's Tutorial. The general plan of the application they use there - main()/createAndShowGui()/constructor within a class extending JPanel - is a good model to follow. (See, eg SimpleTableDemo.java)

  5. #5
    Muskar is offline Member
    Join Date
    Nov 2010
    Posts
    73
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    [code]Have you tried Darryl's suggestion (get the cell renderer, cast it to a DefaultTableCellRenderer and call setOpaque())?
    The following code gives a syntax error:
    Java Code:
    (DefaultTableCellRenderer)tblInboxDisplay.getCellRenderer(0, 0);
    "DefaultTableCellRenderer cannot be resolved as a variable".

    So I cannot call methods on it - although I might have misunderstood his suggestion then.

    Alternatively, it works if I cast it as my own "inboxTableCellRenderer", so I'm not sure what's wrong.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by Muskar View Post
    The following code gives a syntax error:
    Java Code:
    (DefaultTableCellRenderer)tblInboxDisplay.getCellRenderer(0, 0);
    "DefaultTableCellRenderer cannot be resolved as a variable".

    It's hard to say what caused that message without actually seeing the code. Perhaps you could copy and post the entire compiler message, and the code that gave rise to it.

    As written it looks OK. You must, of course, assign it to some suitably declared variable to be able to call the setOpaque() method.

    [Edit] I've just read the bit about if I cast it as my own "inboxTableCellRenderer". You cast to a type (like DefaultTableCellRenderer) not a variable. You can assign the result of the line you posted to inboxTableCellRenderer if you like (because it is the right type) and call setOpaque() on that. In that case inboxTableCellRenderer need not be an instance variable: you can declare it in the constructor since it isn't being used anywhere else.
    Last edited by pbrockway2; 12-09-2010 at 09:55 PM.

Similar Threads

  1. fill table with cells
    By BigBear in forum AWT / Swing
    Replies: 3
    Last Post: 01-26-2010, 09:22 PM
  2. Coloring JTable cells
    By ProgrammingPup in forum Advanced Java
    Replies: 2
    Last Post: 11-04-2009, 10:57 PM
  3. making JTextField transparent to DND-Actions?
    By geronimo013 in forum AWT / Swing
    Replies: 0
    Last Post: 03-16-2009, 05:34 PM
  4. Making an Image transparent?
    By audinue in forum Java 2D
    Replies: 3
    Last Post: 01-03-2009, 07:32 PM
  5. Multi-line Tooltip inside a JTable's Cell
    By stevenc49 in forum AWT / Swing
    Replies: 2
    Last Post: 06-29-2008, 04:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •