Hi,
How can I use an image (gif or....) as a background for my JTable.
The Buttons on the Jtable must stay visible for sure...
Try all I know....
Oh yeah ... how can I put an image in my MenuItems ?
// Item : Help -> Over
aboutItem = new JMenuItem("About");
aboutItem.setMnemonic('A');
helpMenu.add(aboutItem);
aboutItem.addActionListener(menuHandler);
aboutItem.setActionCommand("aboutItem");
Thx Thx !!!
03-05-2004, 01:01 PM
cjard
you mean an icon next to the menu text?
on the jtable note.. subclass jtable, setOpaque(false) to make it transparent, then override the paint() method, draw your image, then call the original paint ( use super.paint(g) )
or you could simply try using a decent layout manager that allows you to stack components atop each other, and place an image underneath the transparent jtable
on an opinionary note: images in guis, especially for high-information carriers like JTable, are a very lame idea.. :)
03-05-2004, 01:42 PM
Twofuncky
Reply
Thx for the info.....
I found the ImageIcons for MenuItems.....
Now for the background... I want to add a background on a JPanel.... In the same JPanel all my JTables will be dispayed..ABOVE the background... I found on the API I have to use JLayerPane I think... I know it's not to funcky to add a backgound to an JTable...but I was not intened to do so...
Thx anyway..
Regards
Davy
03-08-2004, 01:26 PM
cjard
well... JLayeredPane could possibly be used.. i didnt consider it as i have never used it before...
I knocked this together quickly for you:
TransDemo.java
Code:
/*
* JFrame.java
*
* Created on 08 March 2004, 18:05
*/
/**
*
* @author admin
*/
public class TransDemo extends javax.swing.JFrame {
/** Creates new form JFrame */
public TransDemo() {
initComponents();
// Install the custom renderer on the first visible column
int vColIndex = 0;
javax.swing.table.TableColumn col = jTable1.getColumnModel().getColumn(1);
col.setCellRenderer(new TransparentRenderer());
col = jTable1.getColumnModel().getColumn(2);
col.setCellRenderer(new TransparentRenderer());
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
jTable1 = new javax.swing.JTable();
jButton1 = new javax.swing.JButton();
// This renderer extends a component. It is used each time a
// cell must be displayed.
public class TransparentRenderer extends JLabel implements TableCellRenderer {
public TransparentRenderer(){
setOpaque(false);
}
// This method is called each time a cell in a column
// using this renderer needs to be rendered.
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
// 'value' is value contained in the cell located at
// (rowIndex, vColIndex)
if (isSelected) {
// cell (and perhaps other cells) are selected
}
if (hasFocus) {
// this cell is the anchor and the table has the focus
}
// Configure the component with the specified value
if(value == null){
setText("null!?");
setToolTipText("there is no value");
}else{
setText(value.toString());
setToolTipText((String)value);
}
// Set tool tip if desired
// Since the renderer is a component, return itself
return this;
}
// The following methods override the defaults for performance reasons
public void validate() {}
public void revalidate() {}
protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {}
public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {}
}
run it and you can see the button through the JTable, but you cannot click it...
The renderer was necessary as the default rederer expands ona textfield which is by default, opaque. if you want a sophisticated editor for your jtabel, i recommend subclassing the Default cell renderer and making it setOpaque(false) on itself
the only other thing of note is that you would probably want to override the paint method of a panel, to paint the image, then drop the jtable on the panel.. it is implicit that panels have a low number in the Z-order, if you draw your image on anything else, it may fight your table for on-top supremacy :)