2014-01-06 1 views

답변

1

렌더러 클래스와 같은 위치에 Darryls 'Stretch Icon 아이콘이 사용할 수있는 공간을 채우기 위해 스트레칭 할 수 있습니다 때문이다. 나는 테이블에서 그것을 시도한 적이 없지만 그것이 작동하지 않을 이유가 없습니다.

+0

궁극적으로 내 해결책을 이끌어 냈습니다. ImageIcon을 확장하는 클래스를 만들었습니다. 그 이유는 이미지가 변경되고 항상 종횡비를 유지해야하는 경우 이미지를 다시 렌더링해야하기 때문입니다. 필자는 paintIcon 메서드의 Stretch Icon 소스 코드를 사용하여 제 경우에 적합한 솔루션을 제시했습니다. –

+0

'변경되었지만 항상 종횡비를 유지해야하는 경우 .' - StretchIcon은 기본적으로이 작업을 수행합니다. – camickr

1

"사용자가 이미지 열의 열 너비를 변경하면 어떻게 바운드 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(); 
    } 
} 

이 또한 내가

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; 
    } 
} 

그리고 여기가 년대 ImageViewer 클래스를 사용하는이 사용자 정의 셀 렌더러를 사용 늘어 때, 자동으로 크기가 조정됩니다 당신이 프레임을 확장하면 때이 렌더러 클래스를

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); 
    } 
} 

결과를 사용하는 샘플 프로그램이 이미지 셀이며, 이미지가 기지를 확장됩니다 시간

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를 호출합니다. 그냥 있는지 확인 파일이 체크 아웃