views:

307

answers:

4

I have a subclass of JLabel that forms a component of my GUI. I have implemented the ability to drag and drop the component from one container to another, but without any visual effects. I want to have this JLabel follow the cursor during the drag of the item from one container to another. I figured that I could just create a glass pane and draw it on there. However, even after I add the component to the glass pane, set the component visible, and set the glass pane visible, and set the glass pane as opaque, I still so not see the component. I know the component works because I can add it to the content pane and have it show up.

How do I add a component to the glass pane?


Finally figured how to get the simple example working. Thanks, @akf. I was able to adapt this solution to my original problem, allowing me to remove ~60 lines of Java2D code that manually rendered a representation of the JLabel.

package test;

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

public class MainFrame extends JFrame {

    /**
     * @param args
     */
    public static void main(String[] args) {
        MainFrame mf = new MainFrame();
        mf.setSize(400, 400);
        mf.setLocationRelativeTo(null);
        mf.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        mf.setGlassPane(new JPanel());

        JLabel l = new JLabel();
        l.setText("Hello");
        l.setBorder(new LineBorder(Color.BLACK, 1));
        l.setBounds(10, 10, 50, 20);
        l.setBackground(Color.RED);
        l.setOpaque(true);
        l.setPreferredSize(l.getSize());

        //mf.add(l);
        ((JPanel)mf.getGlassPane()).add(l);
        mf.getGlassPane().setVisible(true);

        mf.setVisible(true);
    }
}
+2  A: 

This example 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("Hello");
l.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.

camickr
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
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
Start by creating a simple SSCCE (http://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
I have posted a minimal example that I cannot get to work, just like you asked.
Chris Lieb
+2  A: 

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.

http://jroller.com/gfx/entry/drag_and_drop_effects_the

I myself never did use a fizzy animation/effect on DnD, so can't comment any further :-|

ring bearer
Unfortunately, that uses images drawn directly onto the glass pane rather than components rendered by Swing.
Chris Lieb
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
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
+1  A: 

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);
}
trashgod
+1  A: 

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());
akf