3

I have a JPanel with n number of JXTitledPanels. The user should be able to click the JXTitledPanel and hit a remove button to remove it.

My question is how do I know what JXTitlePanel the user has selected.

here is a screen from my program basically I want a user to click "Hospitals", click remove and the Hospitals Table will disappear.

screen shot

fuxia
  • 62,923
  • 6
  • 54
  • 62
Phox
  • 101
  • 1
  • 1
  • 10
  • for better help sooner post an [SSCCE](http://sscce.org/) – mKorbel Aug 23 '12 at 13:17
  • Since you can add those panels using the `JPanel#add` method, I would suggest to use the `JPanel#remove` method. And without that SSCCE, it is difficult to give more concrete advice – Robin Aug 23 '12 at 13:23
  • i plane to use the JPanel#remove. but i am not sure how to know to determine which titledPane to remove. – Phox Aug 23 '12 at 13:36
  • you might reconsider the user experience: s/he must click somewhere on the panel (its title area?) then click somewhere else (far away relative to the panel) to remove the last clicked (a destructive action) - without any visual clue as to what will be destructed. A titledPanel is _designed_ to support such actions _at one single location_ ... see the answer of @MadProgrammer. Even if you decide to not follow it, you must at least add some visual clue (f.i. like changing the background) as to which is the "selected" target panel – kleopatra Aug 24 '12 at 06:19
  • @kleopatra Yea this is something i was worried about too. I thought about changing the titlepainter to a red or orange to show the selected table. Also i plan to go back and disable buttons that you can't use yet. (if nothing is selected you, the remove button will be disabled) – Phox Aug 24 '12 at 12:40
  • why don't you add a remove button on each panel? Can be as unobstrusive as the typical delete-x, configure with whatever action type your company guidelines require. – kleopatra Aug 24 '12 at 13:24
  • @kleopatra That might not be a bad idea. gonna look into it now – Phox Aug 24 '12 at 13:58

2 Answers2

3

I'd probably add a "remove" control into the right decoration position. This way you could pass a reference to the control of the JXTiltedPane

titledPane.addRightDecoration(new MyRemoveControl(titkedPane));

Or such

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • yeah - except that you don't need a reference: walk up the hierarchy until you find a parent which is a JXTitledPane :-) – kleopatra Aug 23 '12 at 13:55
  • @kleopatra you could do hat, but I'm lazy ;) - would prevent possible memory link with strong reference – MadProgrammer Aug 23 '12 at 19:34
  • @MadProgrammer I am confused at what you mean. – Phox Aug 24 '12 at 12:26
  • @Phox if you pass a reference of the JXTitledPane to the control, you run the risk of creating a memory link because the contol is maintaining a strong reference to the JXTiledPane. On its own, it may not be an issue, but it's just another potential risk tat might prevent the component from being garage collected – MadProgrammer Aug 24 '12 at 12:34
  • @MadProgrammer at first i wasn't really sure what you meant (I have no experience with JXTitledPanels). after adding the remove to the control i also added the edit and move up and down buttons as well. Works great! – Phox Aug 27 '12 at 19:57
0

@madprogrammer probably has the simplest answer, but if you don't want to change the look of the application, you could combine your button's actionListener with a mouseListener for the panels.

The mouseListener portion saves the last panel clicked, and the actionListener just removes the panel that was registered by the mouseListener.

Here's a quick sample I cooked up - it doesn't use JXTitledPane but that shouldn't matter because they're all in the same hierarchy.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TempProject extends JFrame{

    public TempProject(){
        Box mainContent = Box.createVerticalBox();

        //Create Button
        JButton removePanel = new JButton("RemovePanel");
        RemoveListener listener = new RemoveListener(mainContent);
        removePanel.addActionListener(listener);
        mainContent.add(removePanel);

        //Create Panels
        mainContent.add(getPanel(Color.red, listener));
        mainContent.add(getPanel(Color.orange, listener));
        mainContent.add(getPanel(Color.pink, listener));
        mainContent.add(getPanel(Color.magenta, listener));

        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        setContentPane(mainContent);    
        pack();
        setVisible(true);
    }

    public JPanel getPanel(Color color, RemoveListener l){
        JPanel result = new JPanel();
        result.setBackground(color);
        result.add(new JLabel(color.toString()));
        result.addMouseListener(l);
        return result;
    }

    public static void main(String args[])
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new TempProject();
            }
        });
    }

    public static class RemoveListener extends MouseAdapter implements ActionListener{

        Component lastSelectedComponent = null;
        Container master; //The panel containing the ones being listened to

        public RemoveListener(Container master){
            this.master = master;
        }

        @Override
        public void mouseClicked(MouseEvent arg0) {
            lastSelectedComponent = (Component)arg0.getSource();
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if(lastSelectedComponent != null){
                master.remove(lastSelectedComponent);
                master.repaint();
            }
        }

    }

}
Nick Rippe
  • 6,465
  • 14
  • 30
  • possible but too low-level (especially in the context of JXTitledPane) - and see my comment to the question as per usability :) – kleopatra Aug 24 '12 at 06:34
  • the only thing that worries me about this is how my companies framework. They use a modified MVC and all listeners have to be implemented as a separate class in a different package and extend their custom action. – Phox Aug 24 '12 at 12:29
  • @kleopatra I completely agree as far as usability (madprogrammer's answer is the way I would go). This alternative way was just addressing the exact question - in case there was some silly reason it couldn't be done that way. – Nick Rippe Aug 24 '12 at 15:33
  • @Phox That framework would complicate things a little bit. Essentially instead of combining them into one class, you'd have to use a top level class for storing the information and child classes for each type of listener (the child classes could extend your company's listeners). It would be easier if Java supported polymorphism... (topic for another day). – Nick Rippe Aug 24 '12 at 15:36