Placing component on Glass Pane?

Besides the pointers to the LayerPane examples already provided, the issue with your original code centers around the setting of the preferred size of your label. You set it before the JLabel has been sized, so your.

Besides the pointers to the LayerPane examples already provided, the issue with your original code centers around the setting of the preferred size of your label. You set it before the JLabel has been sized, so your: l. SetPreferredSize(l.getSize()); is ineffectual.

If, on the other hand, you make that call after you make your call to setBounds, you will see your desired results. With that in mind, reorder this: l. SetPreferredSize(l.getSize()); l.

SetBounds(10, 10, 50, 20); to look like this: l. SetBounds(10, 10, 50, 20); l. SetPreferredSize(l.getSize()).

Since I had been following Romain Guy's blogs on Swing for a long time. I have a link that you might be interested in. He released the source - which used a GlassPane for DnD effects.

jroller.com/gfx/entry/drag_and_drop_effe... I myself never did use a fizzy animation/effect on DnD, so can't comment any further.

Unfortunately, that uses images drawn directly onto the glass pane rather than components rendered by Swing. – Chris Lieb Apr 1 '10 at 19:02 It shouldn't matter that the example draws images. To use components, you just add the component to the glass pane and make sure you have set the bounds properly.

– camickr Apr 1 '10 at 19:46 The problem was that I was adding the component to the glass pane and setting its bounds, but it still wasn't displaying. The only thing I can figure is that I have no idea how to set the bounds correctly on a component. – Chris Lieb Apr 2 '10 at 4:17.

The example code below shows how to drag a chess piece around a chess board. It uses JLayeredPane instead of a glass pane, but I'm sure the concepts would be the same. That is: a) add the glass pane to the root pane b) make the glass pane visible c) add the component to the glass pane making sure the bounds are valid d) use setLocation() to animate the dragging of the component Edit: added code to fix SSCCE JLabel l = new JLabel(); l.

SetText("setBorder(new LineBorder(Color. BLACK, 1)); // l. SetPreferredSize(l.getSize()); // l.

SetBounds(10, 10, 50, 20); ((JPanel)mf.getGlassPane()). Add(l); mf. SetVisible(true); mf.getGlassPane().

SetVisible(true); When using layout managers you never use the setSize() or setBounds() methods. In your case you just set the preferred size to (0, 0) since this is the default size of all components. It works when you add the label to the frame because the default layout manger for the content pane of the frame is a border layout, therefore the preferred size of the label is ignored and the label is made the size of the frame.

However, by default a JPanel uses a FlowLayout which does respect the preferred size of the component. Since the preferred size is 0, there is nothing to paint. Also, the glass pane needs to made visible in order for it to be painted.

I suggest you read the Swing tutorial. There are section on how layout managers work and on how glass panes work and each section has working examples. Edit: Example code added below: import java.awt.

*; import java.awt.event. *; import java.util. *; import javax.swing.

*; public class ChessBoard extends JFrame implements MouseListener, MouseMotionListener { JLayeredPane layeredPane; JPanel chessBoard; JLabel chessPiece; int xAdjustment; int yAdjustment; public ChessBoard() { Dimension boardSize = new Dimension(600, 600); // Use a Layered Pane for this this application layeredPane = new JLayeredPane(); layeredPane. SetPreferredSize( boardSize ); layeredPane. AddMouseListener( this ); layeredPane.

AddMouseMotionListener( this ); getContentPane(). Add(layeredPane); // Add a chess board to the Layered Pane chessBoard = new JPanel(); chessBoard. SetLayout( new GridLayout(8, 8) ); chessBoard.

SetPreferredSize( boardSize ); chessBoard. SetBounds(0, 0, boardSize. Width, boardSize.

Height); layeredPane. Add(chessBoard, JLayeredPane. DEFAULT_LAYER); // Build the Chess Board squares for (int I = 0; I SetBackground( (i + j) % 2 == 0?

Color. Red : Color. White ); chessBoard.

Add( square ); } } // Add a few pieces to the board ImageIcon duke = new ImageIcon("dukewavered. Gif"); // add an image here JLabel piece = new JLabel( duke ); JPanel panel = (JPanel)chessBoard. GetComponent( 0 ); panel.

Add( piece ); piece = new JLabel( duke ); panel = (JPanel)chessBoard. GetComponent( 15 ); panel. Add( piece ); } /* ** Add the selected chess piece to the dragging layer so it can be moved */ public void mousePressed(MouseEvent e) { chessPiece = null; Component c = chessBoard.

FindComponentAt(e.getX(), e.getY()); if (c instanceof JPanel) return; Point parentLocation = c.getParent().getLocation(); xAdjustment = parentLocation. X - e.getX(); yAdjustment = parentLocation. Y - e.getY(); chessPiece = (JLabel)c; chessPiece.

SetLocation(e.getX() + xAdjustment, e.getY() + yAdjustment); layeredPane. Add(chessPiece, JLayeredPane. DRAG_LAYER); layeredPane.

SetCursor(Cursor. GetPredefinedCursor(Cursor. MOVE_CURSOR)); } /* ** Move the chess piece around */ public void mouseDragged(MouseEvent me) { if (chessPiece == null) return; // The drag location should be within the bounds of the chess board int x = me.getX() + xAdjustment; int xMax = layeredPane.getWidth() - chessPiece.getWidth(); x = Math.

Min(x, xMax); x = Math. Max(x, 0); int y = me.getY() + yAdjustment; int yMax = layeredPane.getHeight() - chessPiece.getHeight(); y = Math. Min(y, yMax); y = Math.

Max(y, 0); chessPiece. SetLocation(x, y); } /* ** Drop the chess piece back onto the chess board */ public void mouseReleased(MouseEvent e) { layeredPane. SetCursor(null); if (chessPiece == null) return; // Make sure the chess piece is no longer painted on the layered pane chessPiece.

SetVisible(false); layeredPane. Remove(chessPiece); chessPiece. SetVisible(true); // The drop location should be within the bounds of the chess board int xMax = layeredPane.getWidth() - chessPiece.getWidth(); int x = Math.

Min(e.getX(), xMax); x = Math. Max(x, 0); int yMax = layeredPane.getHeight() - chessPiece.getHeight(); int y = Math. Min(e.getY(), yMax); y = Math.

Max(y, 0); Component c = chessBoard. FindComponentAt(x, y); if (c instanceof JLabel) { Container parent = c.getParent(); parent. Remove(0); parent.

Add( chessPiece ); parent.validate(); } else { Container parent = (Container)c; parent. Add( chessPiece ); parent.validate(); } } public void mouseClicked(MouseEvent e) {} public void mouseMoved(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public static void main(String args) { JFrame frame = new ChessBoard(); frame. SetDefaultCloseOperation( DISPOSE_ON_CLOSE ); frame.

SetResizable( false ); frame.pack(); frame. SetLocationRelativeTo( null ); frame. SetVisible(true); } }.

I'm having a little trouble switching the JLayeredPane since I use GridBagLayout for my main form's layout. This messes with the DRAG_LAYER since it is required to have the same layout manager as the rest of the content pane, but I don't want a layout manager since this is only going to be used for dragging. – Chris Lieb Apr 1 '10 at 18:59 My example uses a GridLayout and the DRAG_LAYER does not need to use the same layout manager.In fact it uses not layout managers since you are manually positioning the components.

I'm not suggesting you need to swith and use a layered pane only that the concepts should be the same. That is since a glass pane also does not use a layout manager you are responsible for setting the bounds of the component properly. Anyway I can't help futher.

I gave you working code, I don't know how your code is different. – camickr Apr 1 '10 at 19:44 1 Start by creating a simple SSCCE (sscce.Org) that displays a glass pane with a JLabel at a specific position. Once you master that, then you move on to animating the location of the label as you drag it with the mouse.

If you can't get the SSCCE working then you have simple code to post and we can provide more concrete suggestions. – camickr Apr 1 '10 at 19:48 I have posted a minimal example that I cannot get to work, just like you asked. – Chris Lieb Apr 1 '10 at 2:19 @camickr: I like this example, but I had trouble finding it in the new Oracle forums; is this the right one?

– trashgod Apr 1 '10 at 11:50.

Although tangential to the question, the JLayeredPane example cited by @camickr admits the following adaptation, which highlights the effect of mouseReleased() over an existing component. Public ChessBoard() { ... // Add a few pieces to the board addPiece(3, 0, "♛"); addPiece(4, 0, "♚"); addPiece(3, 7, "♕"); addPiece(4, 7, "♔"); } static Font font = new Font("Sans", Font. PLAIN, 72); private void addPiece(int col, int row, String glyph) { JLabel piece = new JLabel(glyph, JLabel.

CENTER); piece. SetFont(font); JPanel panel = (JPanel) chessBoard. GetComponent(col + row * 8); panel.

Add(piece); }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions