2015-01-03 56 views
1

我有一個jTable,我已經創建這些jTable來在名稱旁邊的單元格中顯示國家名稱和標誌圖標。如何使用ImageIcon類在同一列中顯示不同的圖標而不顯示字符串?將圖標添加到jTable的不同行中

下面是我自己寫的,顯示在JTable的某一個地方的法國國旗圖標,但它只是顯示

ImageIcon icon = new ImageIcon("http://www.stoma.fr/assets/images/French_Flag_Small_Icon.jpg"); 
    jTable.setValueAt(icon, 1, 2); 
+0

看到編輯答案 –

回答

1

這是通過簡單地確保迎刃而解字符串,對於列圖標返回表格模型的getColumnClass(...)方法中的Icon.class。幸運的是,JTables知道如何在沒有任何額外工作的情況下顯示圖標。 For example

我的代碼從那裏使用該表模型的修改:

DefaultTableModel model = new DefaultTableModel(COL_NAMES, 0) { 
    @Override 
    public Class<?> getColumnClass(int column) { 
     if (getRowCount() > 0) { 
      Object value = getValueAt(0, column); 
      if (value != null) { 
       return getValueAt(0, column).getClass(); 
      } 
     } 

     return super.getColumnClass(column); 
    } 
    }; 

整個程序:

import java.awt.image.BufferedImage; 
import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URL; 

import javax.imageio.ImageIO; 
import javax.swing.*; 
import javax.swing.table.DefaultTableModel; 

public class ImageColumnTest2 { 
    public static final String IMAGE_SHEET_PATH = "http://speckycdn.sdm.netdna-cdn.com/" 
     + "wp-content/uploads/2010/08/flag_icons_04.jpg"; 
    public static final String[] COUNTRIES = { 
     "Denmark", "China", "Chile", "Canada", "Belgium", "Austria", 
     "Argentina", "France", "Malaysina", "Lebanon", "Korea", "Japan", 
     "Italy", "Ireland", "India", "Hong Kong", "Greece", "Germany" 
    }; 
    public static final int COLS = 6; 
    public static final int ROWS = 3; 
    private static final String[] COL_NAMES = {"Country", "Flag"}; 

    private JTable table = new JTable(); 
    private JScrollPane mainPane = new JScrollPane(table); 

    public ImageColumnTest2() throws IOException { 
     DefaultTableModel model = new DefaultTableModel(COL_NAMES, 0) { 
     @Override 
     public Class<?> getColumnClass(int column) { 
      if (getRowCount() > 0) { 
       Object value = getValueAt(0, column); 
       if (value != null) { 
        return getValueAt(0, column).getClass(); 
       } 
      } 

      return super.getColumnClass(column); 
     } 
     }; 
     URL url = new URL(IMAGE_SHEET_PATH); 
     BufferedImage img = ImageIO.read(url); 
     int x1 = 15; // sorry about the magic numbers 
     img = img.getSubimage(x1, 0, img.getWidth() - 2 * x1, img.getHeight()); 

     int y1 = 20 ; // ditto! 
     int w = img.getWidth()/COLS; 
     int h = img.getHeight()/ROWS; 
     for (int row = 0; row < ROWS; row++) { 
     int y = (row * img.getHeight())/ROWS; 
     for (int col = 0; col < COLS; col++) { 
      int x = (col * img.getWidth())/COLS; 
      BufferedImage subImg = img.getSubimage(x, y, w, h); 

      subImg = subImg.getSubimage(x1, 0, subImg.getWidth() - 2 * x1, subImg.getHeight() - y1); 

      ImageIcon icon = new ImageIcon(subImg); 
      String country = COUNTRIES[col + row * COLS]; 
      Object[] rowData = {country, icon}; 
      model.addRow(rowData); 
     } 
     } 


     table.setModel(model); 
     table.setRowHeight(((ImageIcon)model.getValueAt(0, 1)).getIconHeight()); 
    } 

    public JComponent getMainComponent() { 
     return mainPane; 
    } 

    private static void createAndShowGui() { 
     ImageColumnTest2 imgColumnTest = null; 
     try { 
     imgColumnTest = new ImageColumnTest2(); 
     } catch (MalformedURLException e) { 
     e.printStackTrace(); 
     System.exit(-1); 
     } catch (IOException e) { 
     e.printStackTrace(); 
     System.exit(-1); 
     } 

     JFrame frame = new JFrame("ImageColumnTest"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(imgColumnTest.getMainComponent()); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 

其中爲顯示:

enter image description here

+0

感謝您的回答,但我想問問而不是問所有的國家形象在一個形象的道路上,你怎麼能這樣做,讓你有一個單獨的道路爲每個國家? – batussi

+0

@batussi:這一切取決於如何設置文件。但我敢打賭,你足夠聰明地弄清楚這個小細節,因爲它不是編程練習,而是更簡單的邏輯練習。祝你好運! –