0

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?

  • It's not clear exactly what you are trying to achieve, nor what you've tried with JLayeredPane, which sounds like a viable piece of a solution to this – ControlAltDel Jan 03 '17 at 14:21
  • I updated my question again. My simple question is to bring my added component to the front of all the previously added component... – Sahar Jabeen Jan 03 '17 at 14:25
  • You have included this question: "So my Main question is how to bring this myJTable at the front of all previously added components on the incomingForm?" and this answer: "Its Bringing The Component To From But Spreading It All Over The Form From Top To Bottom And Left To Right Fully :( " So it's STILL not clear what you want, as opposed to this – ControlAltDel Jan 03 '17 at 15:04
  • @ControlAltDel This code `incomingForm.setComponentZOrder(myJTable,0);` is bringing my component on top of all other but its making my table size full all over the form means ignoring `myJTable.setSize(coloumWidth,rowHeight);` and `myJTable.setLocation(xpos, ypos);` code logic. So what to do in alternative way? – Sahar Jabeen Jan 03 '17 at 15:48
  • `So what to do in alternative way?` - you have been given two suggestions. – camickr Jan 03 '17 at 15:49
  • @camickr Thanks. I am looking into it... – Sahar Jabeen Jan 03 '17 at 15:50

1 Answers1

1

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.
camickr
  • 321,443
  • 19
  • 166
  • 288
  • You got my desire. I want to show a table of data on the top of all other components like a dialog box. I am adding the table for few sec then hiding is programmatically by using `myJTable.setVisible(false);` that's why I am overriding it on other component.... – Sahar Jabeen Jan 03 '17 at 15:45
  • On your `JDialog` hint, I found http://stackoverflow.com/a/8410677/3170029 and also Having errors as `adding a window to a container` so is this possible? – Sahar Jabeen Jan 03 '17 at 16:08
  • @SaharJabeen, Neither solution is about adding the table to the frame, you should NOT be doing this. Both solutions show how to display the table on top of the frame. A JDialog is exactly like a frame you create the dialog, add components to it and make it visible. The GlassPane is part of the frame. Read the tutorial for examples of how to use it. – camickr Jan 03 '17 at 16:59
  • Thanks a lot. Finally I am able to do it by using `JDialog` and some tweaks with it. If you want then I will share it in your answer too to help others... – Sahar Jabeen Jan 05 '17 at 14:44