1

I think I got the imageIcon to show up differently in each cell but for some reason when I compile it, the images don't show up. It displays the name of the image but the image itself doesn't show up. Here is an image. http://i49.tinypic.com/r9ibrn.jpg

public class movies extends JFrame {

    public movies() {
    initComponents();

 }      

private void initComponents() {

    panel = new JPanel();
    logo = new JLabel();
    pane = new JScrollPane();


    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setBackground(new Color(255, 255, 204));
    setResizable(false);

    panel.setBackground(new Color(51, 51, 51));
    panel.setPreferredSize(new Dimension(290, 75));

    logo.setIcon(new ImageIcon(getClass().getResource("logo.png"))); 
    logo.setName("logo");
    logo.setRequestFocusEnabled(false);
    logo.setVerifyInputWhenFocusTarget(false);
    logo.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));


    ImageIcon icon1 = new ImageIcon("1.jpg");
    ImageIcon icon2 = new ImageIcon("2.jpg");
    ImageIcon icon3 = new ImageIcon("3.jpg");

    String[] columnNames = {"Section 1", "Section 2"};
    Object[][] data =
    {
        {icon1 + " Music", icon2 + " News"},
        {icon2 + " Movies"},
        {icon3 + " Games"},
    };

    DefaultTableModel model = new DefaultTableModel(data, columnNames);
    JTable table = new JTable( model )

    {
            public Class getColumnClass(int column)
        {
            return getValueAt(0, column).getClass();
        }
    };


    table.setPreferredScrollableViewportSize(table.getPreferredSize());

    table.setBackground(new Color(255, 255, 204));
    JScrollPane scrollPane = new JScrollPane( table );
    getContentPane().add( scrollPane );

    table.setRowHeight(50);
    pane.setViewportView(table);
    table.getColumnModel().getColumn(0).setResizable(false);
    table.getColumnModel().getColumn(1).setResizable(false);
}


public static void main(String args[]) {


        public void run() {
            new movies().setVisible(true);

        }
    });
}

private JLabel logo;
private JScrollPane pane;
private JPanel panel;

}

Radu Murzea
  • 10,724
  • 10
  • 47
  • 69
Henry Pe
  • 47
  • 2
  • 8
  • I'm not motivated enough right now to decipher you code to see exactly what's wrong, but I will say that If I were you, this behavior would draw my attention toward how reference types are being passed around and set. – Sam I am says Reinstate Monica Jul 17 '12 at 17:16
  • 1
    Using `getClass().getResource("1.jpg")` obviously you'll ever get the same image on the cells. You need to do some dynamic! – elias Jul 17 '12 at 17:59
  • I'm new to Java so I might need a little bit more help than that. :D – Henry Pe Jul 17 '12 at 18:09
  • Please, I really need some help here. I don't know where to start to fix this problem. – Henry Pe Jul 18 '12 at 15:57

1 Answers1

1

You can pass in the name of the image when you call the new ImageRenderer constructor (read this).

public class Movies extends javax.swing.JFrame {
    public Movies() {
        initComponents();
        table.getColumnModel().getColumn(1).setCellRenderer(new ImageRenderer("1.jpg"));
        table.getColumnModel().getColumn(0).setCellRenderer(new ImageRenderer("2.jpg"));
    }
}

class ImageRenderer extends DefaultTableCellRenderer {
    ImageIcon icon = null;    

    ImageRenderer(String iconName) {
        icon = new ImageIcon(getClass().getResource(iconName));
    }
}
Jamie
  • 3,890
  • 3
  • 26
  • 35
Garrett Hall
  • 29,524
  • 10
  • 61
  • 76
  • Thanks Garrett Hall for your help! I tried passing in the name of the image in the new ImageRenderer but that only changed the image of the second column. Here is an image of what changed when I added the code that you told me. http://oi48.tinypic.com/2d1o506.jpg Is there a way to specify the imageIcon of the cells in the JTable, for example, Column(2),Row(3)? Maybe I'm missing something here that you can help me with since I'm not very experienced in programming. I basically want each cell with a unique image. – Henry Pe Jul 18 '12 at 23:39
  • You will have to do what is described [here](http://objectmix.com/java/73143-setcellrenderer-specific-cell-jtable.html) for that. You can also upvote/accept answers if they helped you :) – Garrett Hall Jul 19 '12 at 16:25
  • Thanks for the link, I will look at it when I get home. I totally forgot to accept your answer above so here it is. :D – Henry Pe Jul 20 '12 at 00:59
  • I think I got the imageIcon to show up differently in each cell but for some reason when I compile it, the images don't show up. I have posted my new code in the first post so please look at the first post. It displays the name of the image but the image itself doesn't show up. Here is an image that shows what I mean. http://i49.tinypic.com/r9ibrn.jpg Again, the new code is updated on the first post. – Henry Pe Jul 20 '12 at 21:15
  • When you do `icon2 + " Movies"` that returns the String `2.jpg Movies`. You need to use a table cell renderer to display the cell as a JLabel not as text, see [this](http://stackoverflow.com/questions/1291948/adding-an-icon-to-jtable-by-overriding-defaulttablecellrenderer). Please read through the [Swing table documentation](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html) if this doesn't make sense. – Garrett Hall Jul 21 '12 at 13:40