Reputation: 5931
I searched StackOverflow and found this question but I couldn't get it.
How do I invoke a Java method when given the method name as a string?
I have some JLabels and JPanels and each Label is working as a custom graphical button to change background color of a particular JPanel and as there are alot of these labels so I have created a custom MouseListener which is to change the background color of particular JPanel with the name each JLabel has.
Now as these JLabels are giving name of calling JPanels in String value, I want something like this
@Override
public void mouseClicked(MouseEvent e)
{
e.getComponent().getName().setBackground(new COLOR.RED);
}
But I cannot do so.
I just want to convert my string into JPanel's name.
Upvotes: 0
Views: 6567
Reputation: 47608
I would say that the simplest solution to associate 2 elements of the UI is to combine them into a class. Then referencing the corresponding element from the other becomes obvious.
Something like:
class LabelPanel {
JLabel label;
JPanel pane;
...
}
Basic working example:
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestLabelPanelComposition {
public static class LabelPanel {
private final JLabel label;
private final JPanel panel;
private Color colorToSet;
public LabelPanel(String labelText, final Color colorToSet) {
super();
this.colorToSet = colorToSet;
this.label = new JLabel(labelText);
this.panel = new JPanel();
label.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Color old= panel.getBackground();
panel.setBackground(LabelPanel.this.colorToSet);
LabelPanel.this.colorToSet = old;
}
});
}
public JLabel getLabel() {
return label;
}
public JPanel getPanel() {
return panel;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TestLabelPanelComposition().initUI();
}
});
}
protected void initUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Random r= new Random();
List<LabelPanel> labelPanels =new ArrayList<TestLabelPanelComposition.LabelPanel>();
for(int i=0;i<10;i++) {
LabelPanel labelPanel = new LabelPanel("My Label to click "+(i+1), new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256)));
labelPanel.getPanel().add(new JLabel("Some dummy text inside panel "+(i+1)));
labelPanels.add(labelPanel);
}
frame.setLayout(new GridLayout(0, 5));
for (LabelPanel labelPanel : labelPanels) {
frame.add(labelPanel.getLabel());
}
for (LabelPanel labelPanel : labelPanels) {
frame.add(labelPanel.getPanel());
}
frame.pack();
frame.setVisible(true);
}
}
This should not be too hard to adapt to your actual situation .
Upvotes: 0
Reputation: 3592
You can use "client properties" from JComponent. Each Jcomponent contains a Map to put properties inside. You can use a constant String like "associatedPanel" for the key, and the JPanel for the value. The code could be something like this:
JPanel panel1 = new JPanel();
JLabel label1 = new JLabel();
label1.putClientProperty("associatedPanel", panel1);
Now in the mouse listener use getClientProperty("associatedPanel") to obtain the associated panel to set the background.
Upvotes: 2
Reputation:
You can do it like this:
create a map:
Map<String, JPanel> map = new HashMap<String, JPanel>();
then put all your jPanels in it using their names as keys:
map.put("jPanel1", jPanel1);
Then in the event listener:
JPanel jPanel1 = map.get(e.getComponent().getName());
Upvotes: 0