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: AbstractTableModel Rendering Issue

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

    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. Here's a screenshot...

    Search1.jpg

    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. Here's a screenshot...

    Search2.jpg

    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.

            public void searchSpiritDatabase(String search) {
                    try {
                            tableSize = 0;
                            sqlSearchResultsTable.clear();
     
                            Class.forName("com.mysql.jdbc.Driver");
                            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + dbName, dbUsername, dbPassword);
                            statement = connection.createStatement();
                            resultset = statement.executeQuery(search);                        
     
                            while(resultset.next())
                                {
                                    tableSize++;
     
                                    ..........
     
                                    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);
                            table.setModel(model);
                            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) {
                         e.printStackTrace();
                    }
            }

    Here's the cell renderer...

            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));
                        }
                        if(s.equalsIgnoreCase("Drylands")) 
                        {
                            comp.setBackground(new Color(255,255,0));
                            comp.setForeground(new Color(0,0,0));
     
                        }
                        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 {
                            setHorizontalAlignment(JLabel.CENTER);
                        }
                        if(col == 17) {
                            setFont(new java.awt.Font("Cooper", 1, 10));
                        }
     
                        System.out.println("OBJECTVALUE: "+value+"        COLUMN CLASS: "+model.getColumnClass(col)+"        ROW: "+row+"        COLUMN"+col );
     
                        return(comp);
                    }
            }

    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?


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

    Default Re: AbstractTableModel Rendering Issue

    I finally figured it out after 3 days of frustration, although I'm not sure why I had to do this since the cell/column class was showing as ImageIcon before and after the rendering.

    if(col == 16 || col == 20) {
        ((JLabel)comp).setIcon((Icon)value);
        ((JLabel)comp).setText("");
        ((JLabel)comp).setHorizontalAlignment(JLabel.RIGHT);
    }

    Search3.jpg

Similar Threads

  1. Problems updating an AbstractTableModel
    By Psyclone625 in forum AWT / Java Swing
    Replies: 8
    Last Post: December 11th, 2013, 07:26 AM
  2. XML and AbstractTableModel
    By Sofaskurk in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 2nd, 2013, 10:57 AM
  3. AbstractTableModel - Adding rows
    By Drakey in forum AWT / Java Swing
    Replies: 1
    Last Post: April 15th, 2013, 08:33 AM
  4. AbstractTableModel
    By beruska in forum AWT / Java Swing
    Replies: 1
    Last Post: October 25th, 2011, 12:33 PM
  5. Custom Table using AbstractTableModel
    By dumb_terminal in forum AWT / Java Swing
    Replies: 11
    Last Post: November 16th, 2010, 10:02 AM