Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6229423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:33:46+00:00 2026-05-24T09:33:46+00:00

I have a JTable and to set a picture as background in JTable and

  • 0

I have a JTable and to set a picture as background in JTable and other properties i used this code.

tblMainView= new JTable(dtModel){
        public Component prepareRenderer(TableCellRenderer renderer, int row, 
                   int column) 
        {
        Component c = super.prepareRenderer( renderer, row, column);
        // We want renderer component to be transparent so background image 
        // is visible
        if( c instanceof JComponent )
        ((JComponent)c).setOpaque(false);
        return c;
        }
        ImageIcon image = new ImageIcon( "images/watermark.png" );          
          public void paint( Graphics g )
        {
        // First draw the background image - tiled 
        Dimension d = getSize();
        for( int x = 0; x < d.width; x += image.getIconWidth() )
        for( int y = 0; y < d.height; y += image.getIconHeight() )
        g.drawImage( image.getImage(), x, y, null, null );
        // Now let the regular paint code do it's work
        super.paint(g);
        }       

        public boolean isCellEditable(int rowIndex, int colIndex) {
          return false;
        }
        public Class getColumnClass(int col){
            if (col == 0)  
            {  
            return Icon.class;  
            }else if(col==7){
                return String.class;
            } else
            return String.class; 
        }   
        public boolean getScrollableTracksViewportWidth() {
            if (autoResizeMode != AUTO_RESIZE_OFF) {
                if (getParent() instanceof JViewport) {
                return (((JViewport)getParent()).getWidth() > getPreferredSize().width);
                }
            } 
            return false;
            }

    };

    tblMainView.setOpaque(false);

Every thing is working correctly. But when i select a row, the row data hides.it shows my row like this

i want the result same like this,enter image description here
dtModel is the deafultTableModel for my JTable named tblMainView

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:33:46+00:00Added an answer on May 24, 2026 at 9:33 am

    Overriding prepareRenderer() is a recommended way to do custom rendering for an entire table row, but you can’t make it do everything. In particular, the default renderer for Icon should do what you want, and there’s no reason to override paint(), at all.

    Addendum: Looking closer, your selected field appears empty because setOpaque(false) interferes with the optimization mentioned in the DefaultTableCellRenderer API. The example you copied won’t work.

    For reference, the example below overrides the getColumnClass() of DefaultTableModel to obtain the default renderer for types Icon and Date.

    Java GUI

    import java.awt.Color;
    import java.awt.Component;
    import java.awt.EventQueue;
    import java.util.Calendar;
    import java.util.Date;
    import javax.swing.Icon;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTable;
    import javax.swing.UIManager;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableCellRenderer;
    
    /** @see https://stackoverflow.com/questions/6873665 */
    public class JavaGUI extends JPanel {
    
        private static final int ICON_COL = 0;
        private static final int DATE_COL = 1;
        private static final Icon icon = UIManager.getIcon("Tree.closedIcon");
        private final Calendar calendar = Calendar.getInstance();
    
        public JavaGUI() {
            CustomModel model = new CustomModel();
            JTable table = new JTable(model) {
    
                @Override
                public Component prepareRenderer(
                        TableCellRenderer renderer, int row, int column) {
                    Component c = super.prepareRenderer(renderer, row, column);
                    if (isRowSelected(row)) {
                        c.setBackground(Color.blue);
                    } else {
                        c.setBackground(Color.white);
                    }
                    return c;
                }
            };
            for (int i = 1; i <= 16; i++) {
                model.addRow(newRow(i));
            }
            this.add(table);
        }
    
        private Object[] newRow(int i) {
            calendar.add(Calendar.DAY_OF_YEAR, 1);
            return new Object[]{icon, calendar.getTime()};
        }
    
        private static class CustomModel extends DefaultTableModel {
    
            private final String[] columnNames = {"Icon", "Date"};
    
            @Override
            public Class<?> getColumnClass(int col) {
                if (col == ICON_COL) {
                    return Icon.class;
                } else if (col == DATE_COL) {
                    return Date.class;
                }
                return super.getColumnClass(col);
            }
    
            @Override
            public int getColumnCount() {
                return columnNames.length;
            }
    
            @Override
            public String getColumnName(int col) {
                return columnNames[col];
            }
    
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        }
    
        private void display() {
            JFrame f = new JFrame("JavaGUI");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(this);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new JavaGUI().display();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code puts a JTable into a JFrame (sole component of the whole UI):
I have empty TableModel. When I set this model to JTable it hasn't rows.
I have a custom cell renderer set in JTable and it works but instead
I have a JTable with two columns and both are JComboBox, for this purpose
I have a Jtable on which I called the method table1.setAutoCreateRowSorter(true); . So this
I have this JTable on my Swing app with the autoCreateRowSorter enabled. My table
I have a JTable with a Renderer written by myself. When I set the
I have implemented a JProgressBar in a JTable. I used renderer for the ProgressBar
Alright this is a follow-up to my last question: JTable: Changing cell background when
I have a JTable where i enter some values. This is the listener function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.