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 5 of 5

Thread: Display icon/image in a JTable when reading in from MySQL

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    22
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Display icon/image in a JTable when reading in from MySQL

    I am making a database search GUI for a CCG. I have a large GUI with JTextFields, JComboBoxes, JButtons, etc. When the user presses search, the program queries the MySQL database based on input from the user and displays the results in a JTable. The search is working great and returning the correct information. However, the final column of the database is a String that contains the file name of an image (png) for each record. I am having trouble getting the table to display the last column as an Image.

    try {
                Class.forName("com.mysql.jdbc.Driver");
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + dbName, dbUsername, dbPassword);
                Statement sqlState = conn.createStatement();
                String selectStuff = "SELECT * FROM ...";
                rs = sqlState.executeQuery(selectStuff);
     
                Object[] tempRow;
     
     
    // --------------------------------------------------------------
    //             ImageIcon testIcon = new ImageIcon("test.png");
    // --------------------------------------------------------------
     
                while(rs.next()){
                    tempRow = new Object[] {rs.getString(3), rs.getString(4), rs.getString(10), rs.getString(6),
                        rs.getString(5), rs.getString(7), rs.getString(8), rs.getString(9), rs.getString(11),
                        rs.getString(12), rs.getString(13), rs.getString(14), rs.getString(15), rs.getString(16),
                        rs.getString(17), rs.getString(18), rs.getString(19),  rs.getString(42) };
                    dTableModel.addRow(tempRow);
     
                }
    }

    The final column "rs.getString(42)" is the String that I want to turn into an ImageIcon.

    Even when I try to manually set the value as an Icon by adding...
    ImageIcon testIcon = new ImageIcon("test.png");
    ...and changing...
    rs.getString(42)
     
    to
     
    testIcon
    ...I still can't get it to work. The last column of the table just spits out "test.png".

    I've searched all over the internet and tried overriding the getColumnClass, but I had no luck there either.

    I'm able to read in the String of the filename from MySQL. How do I get Java to recognize/convert this to an ImageIcon?


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Display icon/image in a JTable when reading in from MySQL

    Quote Originally Posted by Psyclone625 View Post
    ...I still can't get it to work. The last column of the table just spits out "test.png".

    I've searched all over the internet and tried overriding the getColumnClass, but I had no luck there either.
    Cells in JTable are managed (displayed/edited) through two entities called "renderers" and "editors". JTable has a predefined renderer for icons. To pick this renderer for a column, the table model, more precisely its getColumnClass should return the class literal Icon.class (or even more specifically ImageIcon.class) for that column.

    So your first question should be: how/where to put this getColumnClass method? Well, it depends on the table model you have used. I suppose you are using the predefined DefaultTableModel. Its getColumnClass always return Object.class which is appropriate for common objects but not for icons.

    If this is your case, simply extend DefaultTableModel and override the getColumnClass method so that returns Icon.class for the column you want. If the table model will contain an ImageIcon object (with a correctly loaded image) in that column, you will see the image.

    And remember that the height of rows in JTable does not depend on the cell's content. If your icon is 40 pixel height, that row will not automatically resize in height!

    --- Update ---

    Quote Originally Posted by Psyclone625 View Post
    Thanks for the quick response andbin...

    I tried to override the getColumnClass method, but couldn't get it to work. It's the 18th column in my table.

                if (column == 18) {
    Column indexes are 0-based. So column index 18 means the 19th column!

    Quote Originally Posted by Psyclone625 View Post
    As for the row height, I already have that taken care of.

    table.setRowHeight(table.getRowHeight()+15);
    Ok.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    22
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Display icon/image in a JTable when reading in from MySQL

    Thanks for the quick response andmin...

    I was trying that and made some progress, but still can't figure out how to get the image to show. I think I finally have it recognizing the column as an ImageIcon, but am having trouble getting an image to display still.

    Right now I'm trying to get it to display any image. I stripped almost everything out of the code and the table is still displaying the data correctly except for that image.

    public class temp {
     
        static String dbUsername = "...";
        static String dbPassword = "...";
        static String dbName = "...";
     
        static Object[][] databaseInfo;
        static Object[] columns = {"Type", "Homeland", "Cost", "Set"};
        static ResultSet rs;
        static ResultSetMetaData metaData;
     
        static DefaultTableModel dTableModel = new DefaultTableModel(databaseInfo, columns) {
            public Class getColumnClass(int column) {
                Class returnValue = String.class;
     
                if (column == 3) {
                    returnValue = ImageIcon.class; }
     
                return returnValue;
              }
            };
     
        public static void main(String[] args){
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Connection conn = null;
     
            try {
                Class.forName("com.mysql.jdbc.Driver");
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + dbName, dbUsername, dbPassword);
                Statement sqlState = conn.createStatement();
                String selectStuff = "SELECT * FROM swdbtest.spiritdatabase2 WHERE ActivationCost > 10";
                rs = sqlState.executeQuery(selectStuff);
     
                Object[] tempRow;
     
                ImageIcon testIcon = new ImageIcon("AgeOfAlchemy.png");
     
                while(rs.next()){
                    tempRow = new Object[] {rs.getString(3), rs.getString(4), rs.getString(42), testIcon };
                    dTableModel.addRow(tempRow);
                }
            }
            catch (SQLException ex) {
                System.out.println("SQLException: " + ex.getMessage());
                System.out.println("VendorError: " + ex.getErrorCode());
            }
            catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
     
            JTable table = new JTable(dTableModel);
     
            JScrollPane scrollPane = new JScrollPane(table);
            frame.add(scrollPane, BorderLayout.CENTER);
            frame.setSize(1200, 700);
            frame.setVisible(true);
        }
     
    }

  4. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Display icon/image in a JTable when reading in from MySQL

    Quote Originally Posted by Psyclone625 View Post
                ImageIcon testIcon = new ImageIcon("AgeOfAlchemy.png");
    This is a specification on the local file-system and it's a relative specification, relative to the "current" directory. Which is your current directory? I can't know. It depends on how/from where you launch the application.

    I suspect this is not what you want!
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    22
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Display icon/image in a JTable when reading in from MySQL

    Quote Originally Posted by andbin View Post
    This is a specification on the local file-system and it's a relative specification, relative to the "current" directory. Which is your current directory? I can't know. It depends on how/from where you launch the application.

    I suspect this is not what you want!
    I have it in the same directory as my code. I just have a couple images there for testing purposes, which has worked so far for all my coding. After I get this working, I'm going to have to Photoshop about 25 images, so I didn't bother putting it in another folder and using a url. Photoshoping will be the easy part.

    --- Update ---

    I just saw you're in Italy. I love Italy and have been there twice.

Similar Threads

  1. Problem with JTable and MySQL
    By mija in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 17th, 2012, 04:29 PM
  2. Image Doesn't Display with Qualified Image Path????
    By swaginator in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 31st, 2012, 01:29 AM
  3. how to access mysql table using java and display it?
    By vivin09 in forum Member Introductions
    Replies: 2
    Last Post: August 10th, 2011, 03:41 PM
  4. Replies: 0
    Last Post: February 14th, 2011, 06:10 AM
  5. Stupid thing can't find image icon right in front of it!!!
    By javapenguin in forum What's Wrong With My Code?
    Replies: 14
    Last Post: July 9th, 2010, 02:02 AM