javaswingjtablecomponentsjform

How To Add A Component And Bring It To Front On A JFrame In Java?


I am making a Java Class that's constructor will get a JForm from its parameter and then will add a JTable on that form programmatically as shown in below code...

public class DEMOCLASS
{
private JTable myJTable = new JTable();
public DEMOCLASS(JFrame incomingForm)
    {  
        incomingForm.add(myJTable);
        myJTable.setSize(coloumWidth,rowHeight);
        myJTable.setLocation(xpos, ypos);
        myJTable.setVisible(true);  
        //incomingForm.setComponentZOrder(myJTable,0);    // Its Bringing The Component To From But Spreading It All Over The Form From Top To Bottom And Left To Right Fully :(      
    }
}

Now the main problem is that where this class is called, there are many component already added by some other coding and I want to bring myJTable component on the front that is now hiding behind them. In short, I cant control others already added component. I also heard about MyGlassPane and JLayeredPane but cant make them work with my concept here.

So my Main question is how to bring this myJTable at the front of all previously added components on the incomingForm?


Solution

  • The default painting logic of Swing is that the last component added to a panel is painted first.

    You can change this by playing with the ZOrder as you attempted to do with your commented out code.

    However Swing is not really designed to have components painted on top of one another. Components are typically positioned in a 2D area, not 3D. Your reason for doing this is unclear.

    I would suggest you could:

    1. Use a child JDialog to display the table and its data (you can make the dialog undecorated to remove the Borders).
    2. Maybe use a GlassPane to display the table.