Results 1 to 5 of 5
  1. #1
    Redefine12 is offline Member
    Join Date
    Feb 2012
    Posts
    66
    Rep Power
    0

    Default JTable Transparency

    I've got a Jtable that I'd like to make transparent. With my current code, the table appears 'greyed-out' vice transparent with the gradient background appearing through. I've tried to make everything opaque(false), and read up on concepts, but there must be some missing syntax here..I'd appreciate some constructive assistance..

    Java Code:
        	final JTable table = new JTable(model){
        			
    //Override JTable Render component for alternate row shading   
        		
    		public Component prepareRenderer(TableCellRenderer renderer,
    				int rowIndex, int vColIndex) {
    			setOpaque(false);
    			Component c = super.prepareRenderer(renderer, rowIndex,
    				vColIndex);
    				if (isCellSelected(rowIndex, vColIndex)) {
    					if (rowIndex % 2 == 0) {
    						c.setBackground(Color.LIGHT_GRAY );
    					} else {
    						c.setBackground(getSelectionBackground());
    					}
    				} else {
    				if (rowIndex % 2 == 0) {
    					//c.setBackground(new Color(198, 217, 227));
    					c.setBackground(new Color(224, 242, 252));
    					} else {
    						c.setBackground(getBackground());
    
    						}
    					};
    					
    					
    				return c;
    				}
    		
    		private float tran= 0.4f;
    
    		@Override
    		protected void paintComponent(Graphics g) {
    			Graphics2D g2 = (Graphics2D) g;
    			g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
    			RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    			AlphaComposite old = (AlphaComposite) g2.getComposite();
    			g2.setComposite(AlphaComposite.SrcOver.derive(getTran()));
    			super.paintComponent(g);
    			g2.setComposite(old);
    		}
    		public float getTran() {
    			return tran;
    		}
    		
    		public void setTran(float tran) {
    			this.tran = tran;
    		}
    		
    		
    		
    };
    		
    		table.setOpaque(false);
    		table.convertRowIndexToView(row);
    		table.setModel(model);
    		table.setRowSorter(sorter);  
    		//table.setAutoCreateRowSorter(true);
        	//table.setDefaultRenderer(String.class, new CustomRenderer());
        	table.setRowSelectionAllowed(true);       
        	table.setColumnSelectionAllowed(true);     
        	table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    	    ListSelectionModel listMod =  table.getSelectionModel();
    	    listMod.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    	    listMod.addListSelectionListener(this);
    
        	return table;   
        	
        	}
    implementing the table inside a jpanel in my separate gui class:

    Java Code:
    	JPanel invS = new JPanel(new BorderLayout());
    	
    	tbInv.add(sP, "North");
    	tbInv.add(invS, "South");
    	tbInv.setOpaque(false);
    	invS.add(st.textField, "North");
    	JScrollPane tableContainer = new JScrollPane(st.table); 
    	st.table.setPreferredScrollableViewportSize(new Dimension(990, 380));
    	invS.add(tableContainer);
    	tableContainer.setOpaque(false);
    	invS.setOpaque(false);
    	st.table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 
    	st.table.setOpaque(false);

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: JTable Transparency

    Get rid of everything that isn't related to the transparency issue and post an SSCCE and I'll take a look.

    What were you trying to accomplish with a call to table.convertRowIndexToView(row) that discards the returned value?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Redefine12 is offline Member
    Join Date
    Feb 2012
    Posts
    66
    Rep Power
    0

    Default Re: JTable Transparency

    Solved by setting the table container background to an alpha composite defined by float 'alpha' and adjusting my override of default table render. I didn't feel it was respectful to post and just not reply, but I've also seen warnings against 'spoon-feeding' and fear that if I post the code it will fall in that category. I want to be a contributor to this forums resources while respecting the rules, so let me know if I'm off base here.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: JTable Transparency

    I don't consider it spoonfeeding if I point out, with code, the one thing still to be done by someine who has evidently already invested considerable time and effort and needs just that one little thing to completely solve an issue. Anyone who posts a decent SSCCE fits the bill.

    I want to be a contributor to this forums resources
    Welcome aboard! We could certainly do with some professional advice in the 2D and GUIs sections (I'm just an amateur hobby programmer).

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Bushman is offline Member
    Join Date
    Apr 2012
    Location
    Spainish TOwn
    Posts
    25
    Rep Power
    0

    Default Re: JTable Transparency

    THERE IS AN EASY AND HARD WAY.
    EASY:
    1.import java.awt.GraphicsDevice.WindowTranslucency;
    2.setOpacity ( float value from 0.0f to 1.0f );
    Done.
    Ensure JDK 7 is installed...or no compilation.
    This uniform translucency will render everything on screen with that alpha level. No need to change the opacity of the table itself.



    OR
    HARDISH:
    You will need to extend the java scroll table on which your table is placed.eg
    Java Code:
    import javax.swing.JViewPort;
    import java.awt.GraidentPaint;
    JScrollPane pane = new pane ( table )
    {
    @Override
    protectect JViewPort createViewport ( )
    {
    return new JViewPort ( )
    {
    @OVerride
    public void paintComponent ( Graphics2D g2d )
    {
    paint = new GradientPaint ( params - look them up lol );
    g2d.setPaint ( paint );
    g2d.fillRect ( 0, 0, table width, table height );
    }
    };
    }
    };
    //setup colour with alpha
    Color colour = new Color ( 0, 0, 0, 0 );
    //adjust table and scroll pane viewport opacities
    your table.setOpaque ( false);
    your table.setBackground ( colour );
    your scroll pane.getViewport ( ).setOpaque ( false);
    your scroll pane.getViewport ( ).setBackground ( colour );

    DONE.
    Last edited by DarrylBurke; 04-17-2012 at 10:31 AM. Reason: Fixed the formatting

Similar Threads

  1. Transparency
    By zirbinator in forum Java 2D
    Replies: 7
    Last Post: 03-28-2012, 05:51 AM
  2. DrawImage with transparency ?
    By mrhid6 in forum AWT / Swing
    Replies: 2
    Last Post: 10-01-2011, 11:58 PM
  3. Transparency problem
    By Lacrim in forum Java Applets
    Replies: 1
    Last Post: 07-27-2011, 01:34 PM
  4. Filling with transparency
    By zirbinator in forum Java 2D
    Replies: 3
    Last Post: 02-09-2011, 02:36 AM
  5. ImageIcon Transparency
    By Lingerz in forum New To Java
    Replies: 2
    Last Post: 06-04-2010, 02:53 PM

Posting Permissions

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