tags:

views:

32

answers:

1

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

It sounds like you might want to look at the JTabbedPane. This will allow you to have multiple tabs with different content in each tab. When a user selects a tab, only the content on that tab is shown.

Links:

To dynamically add a tab through a button, you could use code similar to the following:

JButton newTabButton = new JButton("Add Tab");
newTabButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent arg0) {
        JPanel newTab = new JPanel();
        newTab.setLayout(null);
        // Dynamic panel is a JTabbedPane
        dynamicPanel.addTab(JOptionPane.showInputDialog("Name of tab"), newTab);
    }
});
jjnguy
yeh that is a good idea.. but can we add dynamic tabbedpanes like how we done for jbuttons ?
Deepak
You can use the `addTab()` method of `JTabbedPane`
jjnguy
okay i ll try that!!
Deepak
Got that working!! thanks again :)
Deepak
@Nelson: All is good so far!! Now how can i remove the tab from tabbedpane on button click ? Actually i made it like when i create a tab it automatically creates a button to manage it. So now i need to remove the tab and the buton when the same button is clicked. I tried to match it with name but the dynamically created button is returning null when i use getName().
Deepak
@Deepak, if you want to match it with a String, use `getText()`. That will get you the text that is displayed on the button. Or, you can just remove a tab based on which tab is showing.
jjnguy
`getSelectedIndex` will return the selected tab. and then `removeTab(int)` will remove the tab at the currently selected index.
jjnguy
no i wanted to remove the tab when the button is clicked no matter which tab is selected currently
Deepak
remember how we added buttons dynamically ?. same way when we add a button the tabs are also will be added... so now i want to remove the tab when the button is removed..
Deepak
@Deepak, ok. Well, you dynamically create the button, you need to modify the actionlistener you give it. The action listener will need to know which tab the button is associated with.
jjnguy
sexy mate!!!!!!!!!!! i got that working :)
Deepak