Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on How to put text next to my ImageIcon on a cell in a JTable

Parent

How to put text next to my ImageIcon on a cell in a JTable

+1
−0

I have an application that works with a JTable, and in this JTable, I want each cell to have both an ImageIcon and some text.

I see at this link, JTable supports ImageIcon OR text, but seemingly not both at the same time.

I also see that on the bullet for ImageIcon, it says rendered by a centered label.

Well, I know for a fact that I can put both an ImageIcon and text together on the same JLabel -- I am doing that already for a different part of my application.

How do I get access to that JLabel, so that I can put some text alongside that icon?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+2
−0

I have been informed that my question is actually a duplicate. Here is a link to the original question.

https://stackoverflow.com/a/52065145/131872

But I can streamline the answer a little bit.

In my case, I want to store both an ImageIcon and some text. So, the first thing I should do is create a data type that contains both of these elements.

record TextIcon(String text, ImageIcon icon) {}

Next, I need to create a renderer that can render this data type. To do that, I will extend the DefaultTableCellRenderer.

   
      final class MyTableCellRenderer extends DefaultTableCellRenderer
      {
      
         @Override
         public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
         {
         
            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
         
            if (value instanceof TextIcon textIcon)
            {
            
               this.setText(textIcon.text());
               this.setIcon(textIcon.icon());
            
            }
         
            return this;
         
         }
      }
   

Doing it this way is nice because, now, I don't need to specify which column this applies to. I can apply this renderer to all of my columns, and then if they are not a TextIcon, they will just do the default rendering they would have gotten.

Alternatively, if I need to create another data type for my table, I can just add another if statement to this method. This strategy is a powerful way to add custom rendering to your table.

Now that I have my renderer, I will add it to all of my columns. Here is the easiest way to do it.

table.setDefaultRenderer(Object.class, new MyTableCellRenderer());

I use Object.class because I want this renderer to be used for all columns. Since Object is a parent to all reference data types, then doing this essentially means that I am applying this renderer to all of my columns.

After that, I just continue as is, and things just work!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Works for me (1 comment)
Works for me
davidalayachew‭ wrote over 1 year ago

No explanation needed, but it's my own