How to make JLable in JPanel selectable and deletable?

Dear Friends:

I have a JPanel and it has several JLabels (such as JLabel_1, JLabel_2, JLabel_3 etc) on it,

I can move any JLabels within it, but I need following:

1. when I click on JLabel_1, then, JLabel_1 was selected (e.g, its border is changed to some red line or dot line or broken line) and its border color was changed,

2. then if I want to delete JLabel_1, I can press delete key to remove it

How to make JLable in JPanel selectable and deletable?

any good code example like this welcome.

Thanks

[553 byte] By [sunnymanmana] at [2007-11-27 5:14:25]
# 1
MouseListener on the label.KeyListener on the label.the label probably needs to be made focusable to receive keyboard events.
bsampieria at 2007-7-12 10:36:22 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks a lot,

I have following code, it is runnable,

When I click the JLabel, I hope its border becomes red, whenI did not click it, no red border at all, can you help advice how to do it, I tried but failed.

import java.awt.Color;

import java.awt.GridLayout;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import javax.swing.BorderFactory;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.SwingConstants;

import javax.swing.TransferHandler;

import javax.swing.border.BevelBorder;

import javax.swing.border.Border;

import javax.swing.border.EmptyBorder;

import javax.swing.border.LineBorder;

import javax.swing.border.TitledBorder;

import com.test.BevelExample.RolloverListener;

public class LabelDnD extends JPanel {

JTextField textField;

BevelBorder bevel;

JLabel label;

EmptyBorder empty;

public LabelDnD() {

super(new GridLayout(2, 1));

textField = new JTextField(40);

textField.setDragEnabled(true);

JPanel tfpanel = new JPanel(new GridLayout(1, 1));

TitledBorder t1 = BorderFactory.createTitledBorder("JTextField: drag and drop is enabled");

tfpanel.add(textField);

tfpanel.setBorder(t1);

bevel = new BevelBorder(BevelBorder.RAISED);

Border thickBorder = new LineBorder(Color.red, 10);

label = new JLabel("I'm a Label!", SwingConstants.LEADING);

label.setTransferHandler(new TransferHandler("text"));

MouseListener listener = new DragMouseAdapter();

label.addMouseListener(listener);

label.addMouseListener(new RolloverListener());

JPanel lpanel = new JPanel(new GridLayout(1, 1));

TitledBorder t2 = BorderFactory.createTitledBorder("JLabel: drag from or drop to this label");

lpanel.add(label);

lpanel.setBorder(t2);

add(tfpanel);

add(lpanel);

setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

}

private class DragMouseAdapter extends MouseAdapter {

public void mousePressed(MouseEvent e) {

JComponent c = (JComponent) e.getSource();

TransferHandler handler = c.getTransferHandler();

handler.exportAsDrag(c, e, TransferHandler.COPY);

}

}

/**

* Create the GUI and show it. For thread safety, this method should be

* invoked from the event-dispatching thread.

*/

private static void createAndShowGUI() {

//Make sure we have nice window decorations.

JFrame.setDefaultLookAndFeelDecorated(true);

//Create and set up the window.

JFrame frame = new JFrame("LabelDnD");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.

JComponent newContentPane = new LabelDnD();

newContentPane.setOpaque(true); //content panes must be opaque

frame.setContentPane(newContentPane);

//Display the window.

frame.pack();

frame.setVisible(true);

}

public static void main(String[] args) {

//Schedule a job for the event-dispatching thread:

//creating and showing this application's GUI.

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGUI();

}

});

}

// Inner class to respond to mouse events for the "rollover" effect

class RolloverListener extends MouseAdapter {

public void mouseEntered(MouseEvent e) {

((JLabel)e.getComponent()).setBorder(bevel);

repaint();

}

public void mouseExited(MouseEvent e) {

((JLabel)e.getComponent()).setBorder(empty);

repaint();

}

public void mouseClicked(MouseEvent e) {

String text = ((JLabel)e.getComponent()).getText();

System.out.println("You clicked " + text + "!");

}

}

}

Thanks a lot

sunnymanmana at 2007-7-12 10:36:22 > top of Java-index,Desktop,Core GUI APIs...
# 3

How many times have you been asked to post a SSCCE with your original question?

Your problem is because you are also doing DnD which was not mentioned in your original posting. So everybody who took the time to read the question ended up wasting their time because they did not have all the information required to solve the problem.

I'm not going to waste time answering your questions until you learn how to post a reasonable question.

camickra at 2007-7-12 10:36:22 > top of Java-index,Desktop,Core GUI APIs...
# 4
Sorry about that, this probelm is I met after I read his advice and i tested some code then post a new question, in this case, do i need to post a new one?Thanks
sunnymanmana at 2007-7-12 10:36:22 > top of Java-index,Desktop,Core GUI APIs...
# 5

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class DeletingLabels extends MouseAdapter {

JLabel selectedLabel = null;

public void mousePressed(MouseEvent e) {

Point p = e.getPoint();

Component[] c = ((JPanel)e.getSource()).getComponents();

for(int j = 0; j < c.length; j++) {

if(c[j].getBounds().contains(p)) {

if(selectedLabel != null && selectedLabel != (JLabel)c[j])

selectedLabel.setBorder(BorderFactory.createEtchedBorder());

selectedLabel = (JLabel)c[j];

selectedLabel.setBorder(BorderFactory.createLineBorder(Color.red));

break;

}

}

}

private JPanel getContent() {

JPanel panel = new JPanel(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

gbc.weightx = 1.0;

gbc.weighty = 1.0;

Dimension d = new Dimension(100,25);

for(int j = 0; j < 4; j++) {

JLabel label = new JLabel("Label " + (j+1), JLabel.CENTER);

label.setPreferredSize(d);

label.setBorder(BorderFactory.createEtchedBorder());

gbc.gridwidth = ((j+1) % 2 == 0) ? GridBagConstraints.REMAINDER

: GridBagConstraints.RELATIVE;

panel.add(label, gbc);

}

panel.addMouseListener(this);

register(panel);

return panel;

}

private void register(JComponent jc) {

int c = JComponent.WHEN_IN_FOCUSED_WINDOW;

jc.getInputMap(c).put(KeyStroke.getKeyStroke("DELETE"), "DELETE");

jc.getActionMap().put("DELETE", deleteAction);

}

Action deleteAction = new AbstractAction() {

public void actionPerformed(ActionEvent e) {

JPanel parent = (JPanel)e.getSource();

if(selectedLabel != null) {

parent.remove(selectedLabel);

selectedLabel = null;

resetLayout(parent);

}

}

};

private void resetLayout(Container parent) {

GridBagLayout gridbag = (GridBagLayout)parent.getLayout();

Component[] c = parent.getComponents();

for(int j = 0; j < c.length; j++) {

GridBagConstraints gbc = gridbag.getConstraints(c[j]);

gbc.gridwidth = ((j+1) % 2 == 0) ? GridBagConstraints.REMAINDER

: GridBagConstraints.RELATIVE;

gridbag.setConstraints(c[j], gbc);

}

parent.validate();

parent.repaint();

}

public static void main(String[] args) {

DeletingLabels test = new DeletingLabels();

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(test.getContent());

f.setSize(400,400);

f.setLocation(200,200);

f.setVisible(true);

}

}

crwooda at 2007-7-12 10:36:22 > top of Java-index,Desktop,Core GUI APIs...
# 6
Really good, thanks so much, that is what I need!!
sunnymanmana at 2007-7-12 10:36:22 > top of Java-index,Desktop,Core GUI APIs...