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 7896847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:59:55+00:00 2026-06-03T07:59:55+00:00

I am making a JTable where the first two columns contain strings and the

  • 0

I am making a JTable where the first two columns contain strings and the rest of the columns contain icons (specifically, objects of the class ImageIcon). I know how to do either, but how do I mix both in 1 table such that some columns return strings while others return icons?

–EDIT–
explanation for code: data is a 2D string array. For the first two columns, i want them to be displayed as-it-is in the table. For all the rest of the columns, there are only two possible values, “Y” or “N”. Now i want an ImageIcon to be displayed if there is a “Y”, otherwise just leave it blank if there is a “N”.

(in case it helps to know, i am drawing a comparison table where i want a tick mark icon to be displayed if the value is “Y” otherwise just leave the cell empty if the value is “N”)

right now the output is like this:
value of PATH_TO_ICON (“//home//….”) in case of “Y”
“javax.swing.ImageIcon@288e509b” in case of “N”

class MyTableModel extends AbstractTableModel {

    private Object[][] data;
    private String[] headers;

    public MyTableModel(String[][] data, String[] headers) {
        super();
        this.data = data;
        this.headers = headers;
    }

    @Override
    public int getColumnCount() {
        return headers.length;
    }

    @Override
    public int getRowCount() {
        return data.length;
    }

    @Override
    public Object getValueAt(int row, int col) {
        if (col < 2) {
            return data[row][col];
        } else {
            if (data[row][col].equals("Y")) {
                return new ImageIcon(PATH_TO_ICON);
            } else if(data[row][col].equals("N")) {
                return new ImageIcon();
                            } else return null;
        }
    }

    @Override
    public Class<?> getColumnClass(int col) {
        if (col < 2) {
            return String.class;
        } else {
            return ImageIcon.class;
        }
    }
}
  • 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-06-03T07:59:57+00:00Added an answer on June 3, 2026 at 7:59 am

    Set the column class (for each column) as needed. As mentioned by @mKorbel, see also How to Use Tables – Concepts: Editors and Renderers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Do you know any way of making a JTable resize itself up to a
Long story short, I'm making a custom Swing component that's basically a JTable with
Making my first steps in RIA Services (VS2010Beta2) and i encountered this problem: created
While making some final tests of a class-library that I'm writing for Windows Mobile
Im making a login/logout class that logs users in, sets cookies based on user's
Im making slide show with jquery How i can set interval between two function
Does making a class field volatile prevent all memory visibility issues with it in
I am making a java project and i am using JTable . The thing
making a multi-language site with codeginiter. I have created two folders. One for french
Making a new class in VS10 gives me using System; using System.Collections.Generic; using System.Linq;

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.