Results 1 to 3 of 3
  1. #1
    Psyclone625 is offline Member
    Join Date
    Dec 2013
    Rep Power

    Default AbstractTableModel Rendering Issue

    I'm creating a database search for a collectible card/board game. I built a GUI that searches a MySQL database and returns the results to a JTable. The GUI, search, and results returned and displayed in the JTable are working perfectly. Now I'm trying to add rendering to the JTable and am running into some issues.

    2 of the 21 columns in the table are ImageIcons, so I am using an AbstractTableModel. Initially, the ImageIcons are displaying properly. (See screenshot #1)

    When I try to change the background and foreground of each row based on the value of column 1 ("Homeland"), the background and foreground change properly, but the ImageIcons no longer display as images, but instead display their path. (See screenshot #2)

    I added an Enumeration-loop for the cell rendering. Without the loop, I get the results from the first screenshot above where the images properly appear. With the Enumeration-loop, the foreground and backgrounds properly change based on the value of column 1 ("Homeland") but the images don't appear, just their path. The column classes are still ImageIcons.

    Java Code:
            public void searchSpiritDatabase(String search) {
                    try {
                            tableSize = 0;
                            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + dbName, dbUsername, dbPassword);
                            statement = connection.createStatement();
                            resultset = statement.executeQuery(search);                        
                                    sqlSearchResultsTable.add(new SpiritData(sqlType, sqlHomeland, sqlActivationCost, sqlName,
                                                                  sqlAttackPower, sqlAttackRange, sqlDefense, sqlMovementRange,
                                                                  sqlMovementCost, sqlMaintenance, sqlVision, sqlBlockingPower,
                                                                  sqlBlockingRange, sqlNature, sqlClass, sqlFamily, setAttribute,
                                                                  sqlAbilityText, sqlAbilityCost, sqlMaxPerDungeon, setIcon));
                            model = new SpiritTableModel(sqlSearchResultsTable);
                            setTableProperties();                    // row height, auto resize off, sorting, column selection, column width
                            Enumeration<TableColumn> en = table.getColumnModel().getColumns();
                            while (en.hasMoreElements()) {
                                TableColumn tc = en.nextElement();
                                tc.setCellRenderer(new MyTableCellRenderer());
                    catch (SQLException ex) {
                            System.out.println("SQLException: " + ex.getMessage());
                            System.out.println("VendorError: " + ex.getErrorCode()); }
                    catch (ClassNotFoundException e) {
    Here's the cell renderer...

    Java Code:
            private void setAllComboBoxProperties() {
                int i = 0;
                while(i < comboboxList.length)
                    comboboxList[i].setFont(new java.awt.Font("Cooper", 1, 14));
            class MyTableCellRenderer extends DefaultTableCellRenderer implements TableCellRenderer {
                    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, 
                                                           boolean hasFocus, int row, int col) {
                        Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
                        int VALIDATION_COLUMN = 1;
                        String s =  table.getModel().getValueAt(row,VALIDATION_COLUMN).toString();
                        if(s.equalsIgnoreCase("Flatlands")) {
                            comp.setBackground(new Color(255,255,255));
                            comp.setForeground(new Color(0,0,0)); }
                        else if(s.equalsIgnoreCase("Drylands")) {
                            comp.setBackground(new Color(255,255,0));
                            comp.setForeground(new Color(0,0,0)); }
                        else if(s.equalsIgnoreCase("Woodlands")) {
                            comp.setBackground(new Color(0,153,0));
                            comp.setForeground(new Color(255,255,255)); }
                        else if(s.equalsIgnoreCase("Wetlands")) {
                            comp.setBackground(new Color(0,102,255));
                            comp.setForeground(new Color(255,255,255)); }
                        else if(s.equalsIgnoreCase("Highlands")) {
                            comp.setBackground(new Color(129,39,0));
                            comp.setForeground(new Color(255,255,255)); }
                        else if(s.equalsIgnoreCase("Darklands")) {
                            comp.setBackground(new Color(0,0,0));
                            comp.setForeground(new Color(255,255,255));
                        if(col == 3 || col == 17) {
                            setHorizontalAlignment(JLabel.LEFT); } 
                        else {
                        if(col == 17) {
                            setFont(new java.awt.Font("Cooper", 1, 10));
                        System.out.println("OBJECT_VALUE: "+value+"\t\tCOLUMN_CLASS: "+table.getModel().getColumnClass(col)+"\t\tROW: "+row+"\t\tCOLUMN"+col );
    I used system.out.println to verify that the class wasn't being changed to a string or something else. It is still showing the class of columns 16 ("Attributes") and 20 ("RS") as ImageIcon.

    I'm stuck and can't figure out why the images are no longer displaying when I change the foreground and background colors.

    Where am I going wrong? Why aren't the images displaying anymore?
    Attached Thumbnails Attached Thumbnails AbstractTableModel Rendering Issue-search1.png   AbstractTableModel Rendering Issue-search2.png  
    Last edited by Psyclone625; 12-17-2013 at 12:33 AM.

  2. #2
    Psyclone625 is offline Member
    Join Date
    Dec 2013
    Rep Power

    Default Re: AbstractTableModel Rendering Issue

    After 3 days, I figured it out finally. Although I'm not sure why I had to do this in the first place.

    Java Code:
    if(col == 16 || col == 20) {

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Rep Power

    Default Re: AbstractTableModel Rendering Issue

    I figured it out finally. Although I'm not sure why I had to do this in the first place.
    You are overriding the default renderer for all the columns in your table, so you are no longer using the IconRenderer which means your custom renderer needs to do the special rendering.

    For requirements like this (to highlight the row background) I generally would use the approach suggested in Table Row Rendering « Java Tips Weblog so you don't have to create custom rendering for each class type.

Similar Threads

  1. trying to use AbstractTableModel
    By stefan_banu in forum Advanced Java
    Replies: 1
    Last Post: 05-21-2013, 02:35 PM
  2. Replies: 3
    Last Post: 03-21-2012, 06:57 PM
  3. creating a AbstractTableModel
    By darrenw in forum AWT / Swing
    Replies: 0
    Last Post: 10-31-2010, 01:04 PM
  4. The right way to inherit AbstractTableModel
    By skywriter in forum New To Java
    Replies: 3
    Last Post: 10-19-2010, 11:28 AM
  5. DefaultTableModel vs AbstractTableModel
    By chyrl in forum AWT / Swing
    Replies: 4
    Last Post: 05-02-2010, 12:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts