0

I have a JTable that was created in Netbeans's design mode. In my code, I opted for placing the following line of code on startup:

model = new TableModel();
tbShares.setAutoCreateColumnsFromModel(false);
tbShares.setModel(model);

tbShares is my JTable object.

The JFrame that contains my JTable is shown below:

share manager

The first column was set as an Object type (the other two are Strings), so it can show an image. I have this code that I typed up for it to load an image, but it's not working (the list.add part, the rest is to give you an insight on how my code is structured). This is the extension of an AbstractTableModel.

public void addRegister(String status, String name, String clients){
        ImageIcon activeStatus = new ImageIcon(CleanSheets.class.getResource("res/img/active.png"));
        ImageIcon inactiveStatus = new ImageIcon(CleanSheets.class.getResource("res/img/inactive.png"));
        list.add(new Register((status.equals("true") ? activeStatus : inactiveStatus), name, clients));
        this.fireTableDataChanged();
    }

    class Register{
        Object status;
        String name;
        String clients;

        public Register(Object status, String name, String clients) {
            this.status = status;
            this.name = name;
            this.clients = clients;
        }
    }

Supposedly, it grabs the images from the folder I indicated, but then it's just outputting text in that column instead of the actual image. How do I get it to show the correct image? Thank you.

swiftcode
  • 3,039
  • 9
  • 39
  • 64

2 Answers2

2

You should re-implement TableCellRenderer try to read this short article

Sergii Zagriichuk
  • 5,389
  • 5
  • 28
  • 45
1

ImageIcon activeStatus = new ImageIcon(CleanSheets.class.getResource("res/img/active.png")); ImageIcon inactiveStatus = new ImageIcon(CleanSheets.class.getResource("res/img/inactive.png"));

  • prepare Icon/ImageIcon as local variable, because Renderer recreating these Objects on each of Mouse or Key events, there are a bunch events in crazy period

this.fireTableDataChanged();

  • is correct notifier for add / remove whole JTable contens, for TableCell there are fireTableCellXxx()

  • in this context is (add only one row to the TableModel) about fireTableRowsInserted()

How do I get it to show the correct image?

  • as I see there you add a new row with, then add Icon / ImageIcon to the TableModel directly, no more conversion, declare required, no issue, JTable has implemented Icon / ImageIcon in the API

  • don't use Renderer if Icon / ImageIcon aren't changed form Mouse or Key events

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Alright, I've put those as local variables. fireTableRowsInserted() asks for a row and column, though. How do I get the current row and column that was inserted? And as for ImageIcon, I'm lost on that part. The image changes when a share is enabled/disabled, because the share's icon has to be updated. – swiftcode Jun 07 '12 at 12:16
  • did you read API, not isn't it [this.fireTableRowsInserted(data.size() - 1, data.size() - 1);](http://stackoverflow.com/a/6901508/714968) – mKorbel Jun 07 '12 at 12:23
  • Yes, that implementation uses Vectors to store data, that has nothing to do with mine. I did it with this.fireTableRowsInserted(tbShares.getRowCount(), tbShares.getColumnCount()); However, that is not my main goal - I just want to get the image showing. – swiftcode Jun 07 '12 at 12:42
  • huuuuh your're respectful man ???? this is about AbstractTableModel, not about your concrete structure????, better would be to check [JTable tutorial about TableModel and Renderer too](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data) – mKorbel Jun 07 '12 at 12:49
  • Okay...? That is why I asked a specific question on Stack Overflow, so people with more expertise would not judge me and try to help me out instead of criticizing me. I have explained many times that I only wish to get the image working. But since you keep referring to documentation that I have already read, I will no require no further 'help' from you. Have a good day, sir. – swiftcode Jun 07 '12 at 12:57