Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Insert an imageIcon in JTable in Java
New Posts  All Forums:Forum Nav:

Insert an imageIcon in JTable in Java

post #1 of 5
Thread Starter 
I'm new to Java and I have created a table and I want to insert an image in the JTable, how would I do that? I did google this all day yesterday and found a lot of solutions but I'm having a hard time getting it work in my table. Here is my code. biggrin.gif
Code:
table = new javax.swing.JTable();

table.setBackground(new java.awt.Color(255, 255, 204));

table.setModel(new javax.swing.table.DefaultTableModel(
    new Object [][] {
        {null, null},
        {null, null},
        {null, null},
    },
    new String [] {
        "Section 1", "Section 2"
    }
) {
    boolean[] canEdit = new boolean [] {
        false, false
    };

    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return canEdit [columnIndex];
    }
});

table.setRowHeight(50);
pane.setViewportView(table);
table.getColumnModel().getColumn(0).setResizable(false);
table.getColumnModel().getColumn(1).setResizable(false);
post #2 of 5
Thread Starter 
Nevermind, I got it to work. How would I go about having a different image in each cell instead of having all the cells with the same image?
post #3 of 5
Quote:
Originally Posted by Capt View Post

Nevermind, I got it to work. How would I go about having a different image in each cell instead of having all the cells with the same image?
try this:
Code:
ImageIcon aboutIcon = new ImageIcon("about16.gif");
ImageIcon addIcon = new ImageIcon("add16.gif");
ImageIcon copyIcon = new ImageIcon("copy16.gif");
       
Object[][] data =
{
            {aboutIcon, "About"},
            {addIcon, "Add"},
            {copyIcon, "Copy"},
};

DefaultTableModel model = new DefaultTableModel(data);
JTable table = new JTable( model );
source
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
post #4 of 5
Thread Starter 
Thanks travesty for your help but I've already tried using that code but somehow it doesn't work on mine. I'm not very experienced in programming so maybe you can help me since you seem to know more about this stuff.This is the code that I'm using now but as you can see, this only changes the images of the columns, not the individual cells. Is there a code to specify the imageIcon in each cell so each cell has a unique image, for example, getColumn(2),getRow(3);?
Code:
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));
        }
  }

I created a thread in Stack Overflow. Link
Edited by Capt - 7/18/12 at 4:43pm
post #5 of 5
Thread Starter 
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
Code:
    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;
                    
}
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Insert an imageIcon in JTable in Java