Search code examples
javaswingjlayeredpane

Typecast String to JLayeredPane


Is there a way to typecast String type to JLayeredPane? Following is the code I am using:

private static void build_tables() {  
    String sql="SELECT * FROM pos_tables WHERE pos_company_id='"
        +global_variables.company_id
        +"' AND shop_type='"
        +global_variables.shop_type
        +"'";  
    if(mysql_query.count_mysqls(variables.con.conn, sql)>0){  
        try {  
            ResultSet rs = mysql_query.execute_mysql(variables.con.conn, sql);  
            while (rs.next()) {  
                JLayeredPane JL = (JLayeredPane)rs.getObject("parent_floor");  
                tablesetup.addButton(rs.getString("table_name"),
                    Integer.parseInt(rs.getString("x")),
                    Integer.parseInt(rs.getString("y")), JL);    
            }  
        } catch (SQLException ex) {   
            Logger.getLogger(builder.class.getName()).log(Level.SEVERE, null, ex);  
        }  
    }  
}  

I am getting the following error:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
java.lang.String cannot be cast to javax.swing.JLayeredPane

Solution

  • JLayeredPane JL = (JLayeredPane)rs.getObject("parent_floor");
    

    A database doesn't contain JLayeredPane objects. It contains a string.

    What are you trying to do? Why are you using a JLayeredPane?

    Just use a JPanel. Then you can create a JLabel using the string returned from the database query and then you add the label to the panel.

    Something like:

    String text = rs.getObject("parent_floor").toString();
    JLabel label = new JLabel( text );
    panel.add(label);