I want to add/hide/remove jlayeredpanes dynamically on runtime and also should hide the contents on each pane when another pane is selected. I have tried the following code and i am not sure how to do this. The following code hides the content of each pane when alternate pane is selected but it does not hide its content constantly. When we mousemove over the hidden content area they are made visible again. Plz help me out in this!!
public class floorsetup {
public static void createfloor(String name)
{
String name1=name+"_pane";
JButton b = new JButton(name);
final JLayeredPane jp = new JLayeredPane();
jp.setName(name1);
floor_plan.dynamicPane_floors.put(name1, jp);
//jp.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jp.setAutoscrolls(true);
jp.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
jp.setMinimumSize(new java.awt.Dimension(1000, 700));
jp.setOpaque(true);
jp.setBounds(floor_plan.ground.getBounds());
floor_plan.jLayeredPane2.add(jp);
jp.addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
//floor_plan.jLayeredPane2.setVisible(false);
int x = 0,y = 0;
//ComponentOrientation componentOrientation = jLayeredPane2.getComponentOrientation();
// Rectangle bounds = jLayeredPane2..getBounds();
// x=bounds.x;
//y=bounds.y;
//System.out.println(bounds);
x=evt.getX();
y=evt.getY();
System.out.println(x);
System.out.println(y);
// String name=floor_plan.table_name.getText();
String name="some name";
if(floor_plan.delete!=1)
tablesetup.addButton(name,x,y, (JLayeredPane) evt.getSource());
System.out.println((evt.getSource()));
}
});
b.setActionCommand(name);
b.setAlignmentX(Component.CENTER_ALIGNMENT);
b.setPreferredSize(new Dimension(125, 25));
b.setBackground(Color.green);
floor_plan.floors.add(b);
floor_plan.floors.add(Box.createRigidArea(new Dimension(10, 15)));
// b.setSize(125, 25);
floor_plan.dynamicButtons_floors.put(name, b);
MouseListenerClass M1 = new MouseListenerClass();
MouseClass M2 = new MouseClass();
b.addMouseMotionListener(M1);
b.addMouseListener(M2);
b.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
if(floor_plan.delete==1)
{
removeButton(evt.getActionCommand());
}
else if(floor_plan.edit==1)
{
String edit_name = JOptionPane.showInputDialog("Name of the button:");
JButton source = (JButton) evt.getSource();
source.setActionCommand(edit_name);
source.setText(edit_name);
floor_plan.dynamicButtons_floors.put(edit_name, source);
}
else
{
String switcher=evt.getActionCommand();
switcher+="_pane";
switch_pane(switcher,evt);
}
}
});
floor_plan.floors.validate();
floor_plan.floors.repaint();
}
public static void removeButton(String name) {
JButton b = floor_plan.dynamicButtons_floors.remove(name);
floor_plan.jLayeredPane2.remove(b);
floor_plan.jLayeredPane2.invalidate();
floor_plan.jLayeredPane2.repaint();
}
public static void switch_pane(String name,ActionEvent evt)
{
JLayeredPane jp = floor_plan.dynamicPane_floors.get(name);
System.out.println(floor_plan.jLayeredPane2);
System.out.println(jp);
floor_plan.ground.setVisible(false);
floor_plan.ground.setEnabled(false);
jp.setVisible(true);
jp.moveToFront(floor_plan.ground);
}
}
This is the bit of code using getText(). but i am getting error!!
if(floor_plan.delete==1)
{
JButton source = (JButton) evt.getSource();
int index=floor_plan.floors.getComponentCount();
int val=0;
Component[] components = floor_plan.floors.getComponents();
for(int i=0; i<index;i++)
{
System.out.println(source.getName());
if(components[i].getText().equals(source.getName()))
{
val=i;
}
}
removeButton(evt.getActionCommand(),val);
}