1

I want to show a glasspane over my JFrame. I could do that and now my problem is, the glasspane is visible on the top left corner. I want to know how to set glass pane's position as I want(A given location in a JFrame or a JPanel on a JFrame). I tried to set location using setPostion(x,y); and setBounds(x,y,w,h); But it was not successful. I use Netbeans.Here is my code how to show a glasspane.

public void showPanelMy(){

    javax.swing.JList jList1;
    javax.swing.JPanel jPanel1;
    javax.swing.JScrollPane jScrollPane1;

    jPanel1 = new javax.swing.JPanel();
    jScrollPane1 = new javax.swing.JScrollPane();
    jList1 = new javax.swing.JList();
    JPanel g=(JPanel)myJFrame.getGlassPane();
    g.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
    setMaximumSize(new java.awt.Dimension(100, 100));
    g.setBackground(new java.awt.Color(204, 92, 92));

    g.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

    jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
    jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

    jList1.setModel(new javax.swing.AbstractListModel() {....}
    });
    jScrollPane1.setViewportView(jList1);
    jPanel1.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 120, 120));

    g.add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 140, 140));}

This is a my testing code. Here I put a JList and some panels on the glasspane. Please any one tell my how to set position to glass pane. Thank you

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Débora
  • 5,816
  • 28
  • 99
  • 171

1 Answers1

3

Ok. I'll try to give an answer even if you didn't post the whole relevant code.

I suppose "g" is the component you want to use like a GlassPane. You don't have to add other Components to the glass pane itself, but instead construct your component hierarchy like you usually do without glasspane. Then add the glass pane to the frame with setGlassPane() method of JFrame.

Here's a nice tutorial.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Heisenbug
  • 38,762
  • 28
  • 132
  • 190
  • It is best to link to the latest version of the JavaDocs. I have edited your answer to point to J2SE 7. For tips on getting a link to the latest docs, see [point 2 of advantages](http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7090875). – Andrew Thompson Sep 18 '11 at 08:44
  • Thank you Heisenbug very much. I tried what you mentioned. I set properties of component as where they should be.Then every thing is ok :) – Débora Sep 18 '11 at 08:48
  • 1
    and excelent workaround by @camickr http://tips4java.wordpress.com/?s=glass+pane +1 – mKorbel Sep 18 '11 at 09:05