0

Trying science morning.

but no success.

I posted codes that can have problems please help me.

This Is output. Instead of an Image, it shows this text.

enter image description here

TheModel.java javaclass --->(I think the problem is in here).

package junk.yard.management.creative.system;

public class TheModel extends AbstractTableModel {

    private String[] columns;
    private Object[][] rows;

    public TheModel() {
    }

    public TheModel(Object[][] data, String[] columnName) {

        this.rows = data;
        this.columns = columnName;
    }

    @Override
    public Class getColumnClass(int column) {
// 4 is the index of the column image
        if (column == 3) {
            return Icon.class;

        } else {
            return getValueAt(0, column).getClass();
        }
    }

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

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

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {

        return this.rows[rowIndex][columnIndex];
    }

    @Override
    public String getColumnName(int col) {
        return this.columns[col];
    }

}

Code in Jframe -->(I think the problem is in here)........

 public void populateJTable(){
        MyQuery mq = new MyQuery();
        ArrayList<Product2> list = mq.BindTable();
        String[] columnName = {"Sr. No.","Name","Type","Image"};
        Object[][] rows = new Object[list.size()][4];
        for(int i = 0; i < list.size(); i++){      
            if(list.get(i).getMyImage() != null){
                rows[i][0] = list.get(i).getID();
            rows[i][1] = list.get(i).getName();
            rows[i][2] = list.get(i).getType();
             javax.swing.ImageIcon image = new javax.swing.ImageIcon(new javax.swing.ImageIcon(list.get(i).getMyImage()).getImage()
             .getScaledInstance(180, 180, Image.SCALE_SMOOTH) );   
                
            rows[i][3] = image;
            }
            else{
                rows[i][3] = null;
            }
            
        }
        
        TheModel model = new TheModel(rows, columnName);
        jTable1.setModel(model);
        jTable1.setRowHeight(180);
        jTable1.getColumnModel().getColumn(3).setPreferredWidth(180);
    } 

this is my jTable designing code. DarkTable.java javaclass --->

package tabledark;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.util.HashMap;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;

public class TableDark extends JTable {

    private TableDarkHeader header;
    private TableDarkCell cell;

    public TableDark() {
        header = new TableDarkHeader();
        cell = new TableDarkCell();
        getTableHeader().setDefaultRenderer(header);
        getTableHeader().setPreferredSize(new Dimension(0, 35));
        setDefaultRenderer(Object.class, cell);
        setRowHeight(40);
    }

    public void setColumnAlignment(int column, int align) {
        header.setAlignment(column, align);
    }

    public void setCellAlignment(int column, int align) {
        cell.setAlignment(column, align);
    }

    public void setColumnWidth(int column, int width) {
        getColumnModel().getColumn(column).setPreferredWidth(width);
        getColumnModel().getColumn(column).setMinWidth(width);
        getColumnModel().getColumn(column).setMaxWidth(width);
        getColumnModel().getColumn(column).setMinWidth(10);
        getColumnModel().getColumn(column).setMaxWidth(10000);
    }

    public void fixTable(JScrollPane scroll) {
        scroll.setVerticalScrollBar(new ScrollBarCustom());
        JPanel panel = new JPanel();
        panel.setBackground(new Color(30, 30, 30));
        scroll.setCorner(JScrollPane.UPPER_RIGHT_CORNER, panel);
        scroll.getViewport().setBackground(new Color(30, 30, 30));
        scroll.setBorder(BorderFactory.createLineBorder(new Color(60, 60, 60), 2));
    }

    private class TableDarkHeader extends DefaultTableCellRenderer {

        private Map<Integer, Integer> alignment = new HashMap<>();

        public void setAlignment(int column, int align) {
            alignment.put(column, align);
        }

        @Override
        public Component getTableCellRendererComponent(JTable jtable, Object o, boolean bln, boolean bln1, int i, int i1) {
            Component com = super.getTableCellRendererComponent(jtable, o, bln, bln1, i, i1);
            com.setBackground(new Color(30, 30, 30));
            com.setForeground(new Color(200, 200, 200));
            com.setFont(com.getFont().deriveFont(Font.BOLD, 25));
            if (alignment.containsKey(i1)) {
                setHorizontalAlignment(alignment.get(i1));
            } else {
                setHorizontalAlignment(JLabel.LEFT);
            }
            return com;
        }
    }

    private class TableDarkCell extends DefaultTableCellRenderer {

        private Map<Integer, Integer> alignment = new HashMap<>();

        public void setAlignment(int column, int align) {
            alignment.put(column, align);
        }

        @Override
        public Component getTableCellRendererComponent(JTable jtable, Object o, boolean bln, boolean bln1, int row, int column) {
            Component com = super.getTableCellRendererComponent(jtable, o, bln, bln1, row, column);
            if (isCellSelected(row, column)) {
                if (row % 2 == 0) {
                    com.setBackground(new Color(33, 103, 153));
                } else {
                    com.setBackground(new Color(29, 86, 127));
                }
            } else {
                if (row % 2 == 0) {
                    com.setBackground(new Color(50, 50, 50));
                } else {
                    com.setBackground(new Color(30, 30, 30));
                }
            }
            com.setForeground(new Color(200, 200, 200));
            setBorder(new EmptyBorder(0, 5, 0, 5));
            if (alignment.containsKey(column)) {
                setHorizontalAlignment(alignment.get(column));
            } else {
                setHorizontalAlignment(JLabel.LEFT);
            }
            return com;
        }
    }
}

I think the problem is in here please look at it

 @Override
        public Component getTableCellRendererComponent(JTable jtable, Object o, boolean bln, boolean bln1, int row, int column) {
            Component com = super.getTableCellRendererComponent(jtable, o, bln, bln1, row, column);
            if (isCellSelected(row, column)) {
                if (row % 2 == 0) {
                    com.setBackground(new Color(33, 103, 153));
                } else {
                    com.setBackground(new Color(29, 86, 127));
                }
            } else {
                if (row % 2 == 0) {
                    com.setBackground(new Color(50, 50, 50));
                } else {
                    com.setBackground(new Color(30, 30, 30));
                }
            }
            com.setForeground(new Color(200, 200, 200));
            setBorder(new EmptyBorder(0, 5, 0, 5));
            if (alignment.containsKey(column)) {
                setHorizontalAlignment(alignment.get(column));
            } else {
                setHorizontalAlignment(JLabel.LEFT);
            }
            return com;
        }
    }

  • [This](https://stackoverflow.com/questions/4941372/how-to-insert-image-into-jtable-cell) maybe – g00se Apr 16 '23 at 16:42
  • this is confusing – luci_the_coder Apr 16 '23 at 16:45
  • 1
    @g00se: his model seems to return the correct class type for the image column though. OP, a [mre] would be better, if possible. – Hovercraft Full Of Eels Apr 16 '23 at 16:45
  • I have to submit my project on the 20th of April. – luci_the_coder Apr 16 '23 at 16:48
  • @user16320675 can you please look into my code and tell me how to implement – luci_the_coder Apr 16 '23 at 17:54
  • May be i have to do something in jtable designing code and TheModel.java code – luci_the_coder Apr 16 '23 at 18:00
  • 1) What happens when you invoke `getColumnClass(...)` on the table? Do you get "Icon" or "Object"? 2) What happens when you invoke `getDefaultRenderer(...)` on the table? Do you get your custom renderer of the default Icon renderer. You need to isolate where the problem is. You haven't provided an MRE so we can only guess 3) Why not just extend DefaultTableModel and override the `getColumnClass(...)` method? Get this working without any customs renderers. Then you can try implements your custom model if you want and retest. When this works then replaces the renderer. Do one step at a time. – camickr Apr 16 '23 at 18:04
  • You were given a simple MRE in the link of the first comment. So the point is to start with that code to get it working. Then you can try your full custom model and get it working. Then try using your simple custom renderer. You don't need the full logic of the renderer, only basic logic to prove your renderer is being used. If it stops working at any time, then you know what you just changed and you have an [mre] to post. The Product class is irrelevant to your problem. The data in the database is irrelevant to your problem. Frist get it working with hard coded data. – camickr Apr 16 '23 at 18:10
  • It works with normal jtable. But when i add designer file of jtable it doesn't show image – luci_the_coder Apr 16 '23 at 18:22
  • *when i add designer file of jtable* - no idea what that means. First of all there is no need to extend JTable. You only extend a class when you add functionality. Invoking setter methods is not adding functionality. If you feel you do need to extend JTable, then you do it one step at a time. Then test as you make each change. Isolated which change is causing the problem. This is how you do basic debugging. – camickr Apr 16 '23 at 19:02
  • @camickr I have another java class file DarkTable.java which contains custom designing code for my table which is in JFrame. what I meant to say was after removing DarkTable.java from JFrame I am able to get Image in my JTable. But it has no design .. it's just a normal Jtable. But I want to get an Image in my Jtable with the DarkTable.java file present in JFrame,which gives custom designing to my Jtable. – luci_the_coder Apr 16 '23 at 19:21
  • @camickr can you Look at my code TheModel.java and DarkTable.java? And Jframe code as well – luci_the_coder Apr 16 '23 at 19:30
  • No I can't because you haven't posted an MRE. I have given you suggestions on how to solve the problem yourself. – camickr Apr 16 '23 at 20:03
  • what is an MRE? – luci_the_coder Apr 16 '23 at 20:10
  • @kingrishabdugar – luci_the_coder Apr 16 '23 at 20:40
  • Twice you have been given a link read up on the "Minimal Reproducible Example". Did you not read it? – camickr Apr 16 '23 at 21:17
  • I found the problem and the problem is in here. @Override public Component getTableCellRendererComponent(JTable jtable, Object o, boolean bln, boolean bln1, int row, int column) { Component com = super.getTableCellRendererComponent(jtable, o, bln, bln1, row, column); – luci_the_coder Apr 17 '23 at 15:13

0 Answers0