Results 1 to 6 of 6
- 12-09-2010, 05:44 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 73
- Rep Power
- 0
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":
However, a cellRenderer does not have a setOpaque(boolean opaque) method.Java Code:tblInboxDisplay .getCellRenderer(0, 0) .getTableCellRendererComponent(tblInboxDisplay, null, false, false, 0, 0) .setBackground(cellColor);
So is there a simple (or advanced) way to do this?Last edited by Muskar; 12-09-2010 at 05:56 PM.
- 12-09-2010, 06:32 PM #2
No, but DefaultTableCellRenderer does. If you haven't used a custom renderer you can cast the value returned by getCellRenderer() and call the method.a cellRenderer does not have a setOpaque(boolean opaque) method.
To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.
db
- 12-09-2010, 08:11 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 73
- Rep Power
- 0
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.
- 12-09-2010, 08:49 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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)
- 12-09-2010, 09:29 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 73
- Rep Power
- 0
The following code gives a syntax error:
"DefaultTableCellRenderer cannot be resolved as a variable".Java Code:(DefaultTableCellRenderer)tblInboxDisplay.getCellRenderer(0, 0);
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.
- 12-09-2010, 09:50 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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
-
fill table with cells
By BigBear in forum AWT / SwingReplies: 3Last Post: 01-26-2010, 09:22 PM -
Coloring JTable cells
By ProgrammingPup in forum Advanced JavaReplies: 2Last Post: 11-04-2009, 10:57 PM -
making JTextField transparent to DND-Actions?
By geronimo013 in forum AWT / SwingReplies: 0Last Post: 03-16-2009, 05:34 PM -
Making an Image transparent?
By audinue in forum Java 2DReplies: 3Last Post: 01-03-2009, 07:32 PM -
Multi-line Tooltip inside a JTable's Cell
By stevenc49 in forum AWT / SwingReplies: 2Last Post: 06-29-2008, 04:15 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks