2014-01-06 82 views

回答

1

退房Darryls' Stretch Icon如將允許圖標來拉伸以填充可用空間。我從來沒有在桌上試過它,但我沒有看到爲什麼它不起作用。

+0

接受此作爲答案,因爲它最終導致我的解決方案。我創建了我自己的擴展ImageIcon的類。原因是我需要重新渲染圖像,如果它發生了變化,它必須始終保持縱橫比。我使用paintIcon方法的Stretch Icon源代碼來提出適合我的案例的解決方案。 –

+0

'如果它改變了,它必須保持高寬比。' - StretchIcon默認是這樣做的。 – camickr

1

「如何讓JTable在用戶更改圖像列的列寬時自動調整綁定ImageIcons的大小?」

這是我用來拉伸圖像的助手類它只是一個擴展JPanel繪製圖像。你需要通過一個Image

它基本上是把圖像繪製到面板的寬度和高度。所以,當你在你的渲染後使用它,它會自動在顯示區域被拉伸

import java.awt.*; 
import javax.swing.*; 

public class ImageViewer extends JPanel { 

    private java.awt.Image image; 
    private int xCoordinate; 
    private boolean stretched = true; 
    private int yCoordinate; 

    public ImageViewer() { 
    } 

    public ImageViewer(Image image) { 
     this.image = image; 
    } 

    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 

     if (image != null) { 
      if (isStretched()) { 
       g.drawImage(image, xCoordinate, yCoordinate, 
         getSize().width, getSize().height, this); 
      } else { 
       g.drawImage(image, xCoordinate, yCoordinate, this); 
      } 
     } 
    } 

    public java.awt.Image getImage() { 
     return image; 
    } 

    public void setImage(java.awt.Image image) { 
     this.image = image; 
     repaint(); 
    } 

    public boolean isStretched() { 
     return stretched; 
    } 

    public void setStretched(boolean stretched) { 
     this.stretched = stretched; 
     repaint(); 
    } 

    public int getXCoordinate() { 
     return xCoordinate; 
    } 

    public void setXCoordinate(int xCoordinate) { 
     this.xCoordinate = xCoordinate; 
     repaint(); 
    } 

    public int getYCoordinate() { 
     return yCoordinate; 
    } 

    public void setYCoordinate(int yCoordinate) { 
     this.yCoordinate = yCoordinate; 
     repaint(); 
    } 
} 

而且我用,我使用ImageViewer

import bookclasses.ImageViewer; 
import javax.swing.*; 
import javax.swing.table.*; 
import java.awt.*; 

public class MyImageCellRenderer extends DefaultTableCellRenderer { 

    public Component getTableCellRendererComponent(JTable table, Object value, 
      boolean isSelected, boolean isFocused, int row, int column) { 
     Image image = ((ImageIcon) value).getImage(); 
     ImageViewer imageViewer = new ImageViewer(image); 
     return imageViewer; 
    } 
} 

而這裏的這個自定義單元格渲染器調整其中I使用渲染器類

import java.awt.BorderLayout; 
import java.util.GregorianCalendar; 
import javax.swing.ImageIcon; 
import javax.swing.JApplet; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 

public class TestTableCellRendererDemo extends JApplet { 

    private String[] columnNames 
      = {"Title", "Copies Needed", "Publisher", "Date Published", 
       "In-stock", "Book Photo"}; 

    private ImageIcon intro1eImageIcon = new ImageIcon("icons/stacko/stackoverflow5.png"); 
    private ImageIcon intro2eImageIcon = new ImageIcon("icons/stacko/stackoverflow5.png"); 
    private ImageIcon intro3eImageIcon = new ImageIcon("icons/stacko/stackoverflow5.png"); 

    private Object[][] rowData = { 
     {"Introduction to Java Programming", 120, 
      "Que Education & Training", 
      new GregorianCalendar(1998, 1 - 1, 6).getTime(), 
      false, intro1eImageIcon}, 
     {"Introduction to Java Programming, 2E", 220, 
      "Que Education & Training", 
      new GregorianCalendar(1999, 1 - 1, 6).getTime(), 
      false, intro2eImageIcon}, 
     {"Introduction to Java Programming, 3E", 220, 
      "Prentice Hall", 
      new GregorianCalendar(2000, 12 - 1, 0).getTime(), 
      true, intro3eImageIcon},}; 

    private MyTableModel tableModel = new MyTableModel(
      rowData, columnNames); 


    private JTable jTable1 = new JTable(tableModel); 

    public TestTableCellRendererDemo() { 
     jTable1.setDefaultRenderer(jTable1.getColumnClass(5), 
       new MyImageCellRenderer()); 
     jTable1.setRowHeight(60); 
     add(new JScrollPane(jTable1), BorderLayout.CENTER); 
    } 
} 

結果的示例程序是具有圖像的電池中,當如果展開所述框架,所述圖像將擴大機智h拍攝

enter image description here enter image description here


所以,基本上

  1. 我的自定義單元格渲染器設置爲列你想

    jTable1.setDefaultRenderer(jTable1.getColumnClass(5), 
         new MyImageCellRenderer()); 
    jTable1.setRowHeight(60); 
    
  2. 確保在表中的列是ImageIcon

    private ImageIcon intro1eImageIcon = new ImageIcon("icons/stacko/stackoverflow5.png"); 
    Object[] row = { 
          "Introduction to Java Programming", 
          120, 
          "Que Education & Training", 
          new GregorianCalendar(1998, 1 - 1, 6).getTime(), 
          false, 
          intro1eImageIcon 
    }; 
    
  3. 渲染器已經調用ImageViewer,這樣你就不會需要明確自己的任何地方調用它。只要確保該文件是在同一位置作爲渲染器類