Background in JTable


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Background in JTable

  1. #1
    Join Date
    Feb 2004
    Posts
    13

    Question Background in JTable

    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 !!!

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    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..
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  3. #3
    Join Date
    Feb 2004
    Posts
    13

    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

  4. #4
    Join Date
    Feb 2004
    Posts
    808
    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();
    
            getContentPane().setLayout(new java.awt.GridBagLayout());
    
            addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowClosing(java.awt.event.WindowEvent evt) {
                    exitForm(evt);
                }
            });
    
            jTable1.setModel(new javax.swing.table.DefaultTableModel(
                new Object [][] {
                    {null, null, null, null},
                    {null, null, null, null},
                    {null, null, null, null},
                    {null, null, null, null}
                },
                new String [] {
                    "Title 1", "Title 2", "Title 3", "Title 4"
                }
            ));
            jTable1.setOpaque(false);
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 1;
            gridBagConstraints.gridy = 1;
            gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
            gridBagConstraints.weightx = 1.0;
            gridBagConstraints.weighty = 1.0;
            getContentPane().add(jTable1, gridBagConstraints);
    
            jButton1.setBackground(new java.awt.Color(255, 255, 0));
            jButton1.setText("jButton1");
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 1;
            gridBagConstraints.gridy = 1;
            gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
            gridBagConstraints.weightx = 1.0;
            gridBagConstraints.weighty = 1.0;
            getContentPane().add(jButton1, gridBagConstraints);
    
            pack();
        }
        
        /** Exit the Application */
        private void exitForm(java.awt.event.WindowEvent evt) {
            System.exit(0);
        }
        
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            new TransDemo().show();
        }
        
        
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private javax.swing.JTable jTable1;
        // End of variables declaration
        
    }
    TransparentRenderer.java
    Code:
    import javax.swing.*;
    import javax.swing.table.*;
    import java.awt.Component;
    
    // 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
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center