[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

URGENT!!!! How to add an image and a TextField Together into a JTable

Hi Experts,


    I am struggling to find this,

   How to add an image and a text into the same cell of a JTable?

   This is something like this when seen in a JTable

    [<image goes here>]  Text                       [image 1 goes here] Text 1
 
    [<image 2 goes here>] Text 2                   [image 3 goes here] Text 3

    [<image 4 goes here>] Text 4                   [image 5 goes here] Text 5


  Please solve this problem as soon as possible....

Thank you in advance,

regards
Triguna M S
0
Triguna
Asked:
Triguna
  • 4
  • 4
1 Solution
 
sciuriwareCommented:
Set the image(s) to be background.
;JOOP!
0
 
TimYatesCommented:
Define your own CellRenderer which uses a JLabel (you can setIcon() on a JLabel, and it will appear on the left of the text)

http://www.javaalmanac.com/egs/javax.swing.table/CustRend.html
0
 
TrigunaAuthor Commented:
Thank you for your comments.

TimYates,

    I will try that and get back to you.

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
TimYatesCommented:
Good luck!

Let us know if you have problems!!

Tim
0
 
TrigunaAuthor Commented:
TimYates,

  I tried to add the ICONS but no result.

  Can u please explain me with a small code?

 thank you.

regads
Triguna M S
0
 
TimYatesCommented:
From that code:

JTable table = new JTable();
    // Add data...
   
    // Install the custom renderer on the first visible column
    int vColIndex = 0;
    TableColumn col = table.getColumnModel().getColumn(vColIndex);
    col.setCellRenderer(new MyTableCellRenderer());
   
    // This renderer extends a component. It is used each time a
    // cell must be displayed.
    public class MyTableCellRenderer extends JLabel implements TableCellRenderer {
        // 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
            setText(value.toString());

            // set the icon for the renderer...
            setIcon( new ImageIcon( this.getClass().getResource( "image.png" ) ) ) ;

            // Set tool tip if desired
            setToolTipText((String)value);
   
            // 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) {}
    }

Should work (assuming you have "image.png" in the same directory as the class)

Tim
0
 
TrigunaAuthor Commented:
Thank you for the comment...

Tim Yates.

I will try that and will get back to you soon,

Sorry for the late reply.
0
 
TrigunaAuthor Commented:
Thank you very much. It worked for me.

Thanks a lot.
0
 
TimYatesCommented:
Yay!  Cool :-)

Good luck with it all :-)

Tim
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now