This discussion is archived
0 Replies Latest reply: Jan 26, 2010 8:17 AM by 807559 RSS

Popup or JDialog for complex objects

807559 Newbie
Currently Being Moderated
Hello all, I'm developing this small JAgenda based on JCalendar by Kai Tödter (

This is my idea for showing a modal popup window to magnify the single AgendaDayObject (in place of the simple jbutton in JCalendar) in the center of the screen:
         public class AgendaDayObject extends JPanel {

          private int day;
          private JLabel label;
          private JList list;
          private JButton magnifier;
          private int magnifierSize = 16;
          public AgendaDayObject() {
          public AgendaDayObject(int day) {

      = day;
               this.setLayout(new GridLayout(1,1));
               //LABEL PANEL
               label = new JLabel();
               if (day != 0) label.setText(String.valueOf(day));
               list = new JList();
               list.setModel(new DefaultListModel());
               //JScrollPane scroll = new JScrollPane(list);
               magnifier = new JButton();
               magnifier.setIcon(new ImageIcon("rsc/icons/zoom_r_button.png"));
               magnifier.addActionListener(new MagnifierListener());
               //LAYER FOR OVERLAPPING
               FilledLayeredPane layers = new FilledLayeredPane();
               layers.add(label, JLayeredPane.PALETTE_LAYER);
               layers.add(list, JLayeredPane.DEFAULT_LAYER);
               layers.add(magnifier, JLayeredPane.POPUP_LAYER);

          public int getDay() {
               return day;

          public void setDay(int day) {
      = day;

          public JList getList() {
               return list;

          public void setList(JList list) {
               this.list = list;
          public JLabel getLabel() {
               return label;

          private class FilledLayeredPane extends JLayeredPane {
               * Layout each of the components in this JLayeredPane so that they all fill
               * the entire extents of the layered pane -- from (0,0) to (getWidth(), getHeight())
              public void doLayout() {
                  // Synchronizing on getTreeLock, because I see other layouts doing that.
                  // see BorderLayout::layoutContainer(Container)
                  synchronized(getTreeLock()) {
                      int w = getWidth();
                      int h = getHeight();
                      for(Component c : getComponents()) {
                           c.setBounds(0, 0, w, h);
                           if (c instanceof JLabel) {
                               c.setFont(new Font("Serif", Font.PLAIN, (int) (h*0.8)));
                           if (c instanceof JButton) {
                                c.setBounds(w-magnifierSize-5, h-magnifierSize-5, magnifierSize, magnifierSize);
          public void setVisible(boolean flag) {
          public class MagnifierListener implements ActionListener {

               public void actionPerformed(ActionEvent e) {
                    int day =;
                    JPanel thisDay = null;
                    thisDay = AgendaDayObject.this.popUp();
                    final JDialog dialog = new JDialog();
                    dialog.setSize(new Dimension(600,400));
                    dialog.addFocusListener(new FocusListener() {

                         public void focusGained(FocusEvent e) {
                              // TODO Auto-generated method stub

                         public void focusLost(FocusEvent e) {
          private JPanel popUp() {
               //A NEW PANEL
               JPanel copyPanel = new JPanel();
               copyPanel.setLayout(new GridLayout(1,1));
               JLabel label = new JLabel(this.label.getText());
               //LIST WITH SCROLL
               JList list = new JList(this.list.getModel());
               JScrollPane scroll = new JScrollPane(list);
               FilledLayeredPane layers = new FilledLayeredPane();
               layers.add(label, JLayeredPane.PALETTE_LAYER);
               layers.add(scroll, JLayeredPane.DEFAULT_LAYER);
               return copyPanel;
I wrote a popUp() method that basically re-create the Panel with desired content; this one is shown in a JDialog that auto-close itself on focus lost event. JPopMenu was not working fine as well as clone() method (some errors about layout management) but I'm not a master about them so sure there is a more simple and linear way to do it!

Any suggestion and/or complaints... :p are both almost welcome!

Edited by: mwiti on Jan 26, 2010 8:16 AM