Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Trying to Render a JTable Cell for an Image

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Location
    Norwich, England
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Trying to Render a JTable Cell for an Image

    I have a scrolling Jtable in which one of the columns will either be blank or contain an Image. I can get the column to show the Image Location as a string, but when I try and render the cell as an Image Icon then the program crashes when trying to paint the table.

    My code is as follows, any advice would be appreciated.

    package Collection.Print;

    import Collection.DataAccess;

    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.AbstractTableModel;
    import javax.swing.table.DefaultTableCellRenderer;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.util.List;
    import javax.swing.ImageIcon;
    import javax.swing.JLabel;

    public class PrintList extends JPanel {

    private List<String[]> PrintData ;
    private String ImageLoc ;

    public PrintList(List<String[]> ImpData) {

    setPrintData(ImpData) ;

    JTable table = new JTable(new MyTableModel());

    table.setPreferredScrollableViewportSize(new Dimension(900, 350));
    table.setFillsViewportHeight(true);

    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    //Set up column and cell rendering.
    initRender(table);

    //Add the scroll pane to this panel.
    add(scrollPane);
    }

    private void setPrintData (List<String[]> inPrintData) {
    PrintData = inPrintData ;
    }

    private List<String[]> getPrintData () {
    return PrintData ;
    }

    private void setCurrImage (String inImageloc) {
    ImageLoc = inImageloc ;
    }

    private String getCurrImage () {
    return ImageLoc ;
    }

    private void initRender(JTable table) {
    MyTableModel model = (MyTableModel)table.getModel();

    TextAreaRenderer textAreaRenderer = new TextAreaRenderer();

    DefaultTableCellRenderer Rightrenderer = new DefaultTableCellRenderer() ;
    Rightrenderer.setHorizontalAlignment(JLabel.RIGHT) ;

    DefaultTableCellRenderer Leftrenderer = new DefaultTableCellRenderer() ;
    Leftrenderer.setHorizontalAlignment(JLabel.LEFT);

    DefaultTableCellRenderer Centrerenderer = new DefaultTableCellRenderer() ;
    Centrerenderer.setHorizontalAlignment(JLabel.CENTE R);

    for (int j = 0; j < PrintData.size(); j++) {

    Object image = model.getValueAt(j, 2);
    setCurrImage ((String)image) ;

    table.getColumnModel().getColumn(2).setCellRendere r(new ImageRenderer());

    image = model.getValueAt(j, 3);
    setCurrImage ((String)image) ;
    table.getColumnModel().getColumn(3).setCellRendere r(new ImageRenderer());

    }

    table.getColumnModel().getColumn(0).setPreferredWi dth(70);
    table.getColumnModel().getColumn(1).setPreferredWi dth(280);
    table.getColumnModel().getColumn(2).setPreferredWi dth(80);
    table.getColumnModel().getColumn(3).setPreferredWi dth(80);
    table.getColumnModel().getColumn(4).setPreferredWi dth(100);
    table.getColumnModel().getColumn(5).setPreferredWi dth(100);
    table.getColumnModel().getColumn(6).setPreferredWi dth(85);
    table.getColumnModel().getColumn(7).setPreferredWi dth(50);

    table.getColumnModel().getColumn(7).setCellRendere r(Rightrenderer);
    table.getColumnModel().getColumn(1).setCellRendere r(textAreaRenderer);
    }


    class ImageRenderer extends DefaultTableCellRenderer {
    JLabel lbl ;

    public Component getTableCellRendererComponent( JTable table,
    Object value,
    boolean isSelected,
    boolean hasFocus,
    int row,
    int column){
    String currImage = getCurrImage() ;
    lbl = new JLabel();

    lbl.setHorizontalAlignment(JLabel.CENTER);
    lbl.setText((String) value);
    if (value != null ||
    value.toString().compareToIgnoreCase("") != 0) {
    lbl.setIcon(new ImageIcon(getClass().getResource(currImage)));
    }
    return lbl;
    }
    }

    class MyTableModel extends AbstractTableModel {
    private String[] columnNames = {"Date",
    "Description",
    "Image",
    "",
    "Format",
    "State",
    "Country",
    "Value"};

    private Object[][] data = setData();

    //populate the data for Table
    private Object[][] setData() {

    List printdata = getPrintData() ;

    int pds = printdata.size() ;

    Object[][] data1 = new Object[pds][8] ;

    for (int j = 0; j < pds; j++) {
    String[] printline = new String[10] ;
    printline = (String[])printdata.get(j);

    data1[j][0] = printline[6] ;
    if (printline[10].compareToIgnoreCase("") == 0 ) {
    data1[j][1] = printline[1] ;
    } else {
    data1[j][1] = printline[1] + ", " + printline[10];
    }

    //Get the full path to the Images
    try {
    if (Integer.parseInt(printline[7]) != 0 ) {
    data1[j][2] = DataAccess.getImageLoc(Integer.parseInt(printline[7]));
    } else {
    data1[j][2] = null ;
    }

    if (Integer.parseInt(printline[8]) != 0 ) {
    data1[j][3] = DataAccess.getImageLoc(Integer.parseInt(printline[8]));
    } else {
    data1[j][3] = null ;
    }
    } catch (Exception e) {
    System.out.println("Exception " +
    e.getStackTrace());
    }

    data1[j][4] = printline[2] ;
    data1[j][5] = printline[3] ;
    data1[j][6] = printline[4] ;
    data1[j][7] = printline[9] ;
    }

    return data1 ;
    }

    public int getColumnCount() {
    return columnNames.length;
    }

    public int getRowCount() {
    return data.length;
    }

    public String getColumnName(int col) {
    return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
    return data[row][col];
    }

    public Class getColumnClass(int b, int c) {
    return getValueAt(b, c).getClass();
    }
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trying to Render a JTable Cell for an Image

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.


    the program crashes
    Copy the full text of the error message and post it here.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. JTable edit cell when focus is gained
    By TheQuickBrownFox in forum AWT / Java Swing
    Replies: 0
    Last Post: December 7th, 2012, 09:24 PM
  2. JTable with JButton in a cell
    By kpat in forum AWT / Java Swing
    Replies: 0
    Last Post: April 9th, 2012, 12:17 PM
  3. [SOLVED] Why is object reference showing in JTable when editing cell?
    By eewill in forum AWT / Java Swing
    Replies: 3
    Last Post: January 17th, 2012, 05:20 PM
  4. JTable - multiple lines in a cell AND cells not editable?
    By friday in forum AWT / Java Swing
    Replies: 3
    Last Post: January 10th, 2011, 08:44 AM
  5. JTable prepareRenderer changes all cell colors
    By BrettStuart in forum AWT / Java Swing
    Replies: 9
    Last Post: July 21st, 2010, 02:51 AM

Tags for this Thread