4

I've recently found swingx and wanted to experiment with JXCollapsiblePane. But i cant get it to work. I've found this code:

   Action toggleAction = collapsiblePane.getActionMap().
   get(JXCollapsiblePane.TOGGLE_ACTION);
   toggleButton.setAction(toggleAction);

But clicking button does nothing. If i set pane collapsed initially, when clicking button, pane closes WITHOUT animation, and subsequent clicks do nothing. Im horrible at programming please give some advice.

EDIT I use IDE Netbeans to drag'n'drop component into the frame

collapsiblePane = new org.jdesktop.swingx.JXCollapsiblePane();
collapsiblePane.setCollapsed(true);

EDIT2 Ok the problem was it was set to toggle UP, but there was no place to collapse, i changed it to RIGHT and kinda works but still no animation. I tried to just create frame and pane manually and it works just fine. I guess the problem is in the way gui plugin adds collapsiblepane to frame.

Maybe someone with expirience in working with JXCollapsiblePane and Netbeans could help

EDIT3 enter image description here

enter image description here

When the collapsiblePane opens other stuff on the frame is not moving. I create pane from gui builder, place JPanel in it, some buttons and text fields in panel; and label and button from code:

initComponents();
        setLayout(new BorderLayout());            
         add(new JLabel("JLabelFromCode", JLabel.CENTER)); 
         JPanel buttonPanel = new JPanel(); 
         add(buttonPanel, BorderLayout.SOUTH); 
         collapsingButtonFromCode = new JButton(); 
         collapsingButtonFromCode.setText("toggleButtonFromCode"); 
         buttonPanel.add(collapsingButtonFromCode);                     
         buttonFromCode.addActionListener(collapsiblePane.getActionMap().get( 
                JXCollapsiblePane.TOGGLE_ACTION));
         collapsingButtonFromCode.addActionListener(collapsiblePane.getActionMap().get( 
                JXCollapsiblePane.TOGGLE_ACTION));
         setVisible(true);

initComponents() is the method created by IDE which defines layout, i cant edit it, and i think this method is the problem:

java.awt.GridBagConstraints gridBagConstraints;

        buttonFromCode = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        collapsiblePane = new org.jdesktop.swingx.JXCollapsiblePane();
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        buttonFromCode.setText("CollapseButtonFromGuiBuilder");

        jLabel1.setText("jLabel1");

        jPanel1.setBackground(new java.awt.Color(102, 102, 102));
        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        jButton1.setText("jButton1");

        jLabel2.setText("jLabel2");

        jLabel3.setText("jLabel3");

        jTextField1.setText("jTextField1");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(jButton1)
                .addGap(76, 76, 76)
                .addComponent(jLabel2)
                .addGap(186, 186, 186)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(193, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(337, Short.MAX_VALUE)
                .addComponent(jLabel3)
                .addGap(296, 296, 296))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jLabel2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addContainerGap(22, Short.MAX_VALUE))
        );

        collapsiblePane.getContentPane().add(jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(buttonFromCode, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(44, 44, 44))
                    .addComponent(collapsiblePane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 661, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(collapsiblePane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonFromCode)
                    .addComponent(jLabel1))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>
bunnyjesse112
  • 747
  • 6
  • 27
  • 44
  • 2
    (not quite sure if you already solved it?, anyway:) should work - as @Robin already stated - time for an SSCCE to see what's going on – kleopatra Dec 09 '11 at 12:17
  • please, look at the last edit – bunnyjesse112 Dec 12 '11 at 10:36
  • that's not an sscce :-) Plus, if your tool stands in the way, don't use it ... – kleopatra Dec 12 '11 at 10:47
  • 2
    common ... google is at your fingertips ... – kleopatra Dec 12 '11 at 11:01
  • btw, that (mainly netbeans produced) mess looks like you'r mixing builder's layout (done in initComponents) with some hand-coded (the stuff after calling initComponents) - that's a _really_ bad idea: either let the builder do all or hand-code all (which I would recommend, as it's hard to understand what the builder is doing, that code isn't meant for human reading ;-) – kleopatra Dec 12 '11 at 11:05
  • But thats the problem. With builder CollapsiblePane dont work as supposed. – bunnyjesse112 Dec 12 '11 at 11:19

2 Answers2

4

here's a code snippet which might simulate the problem (wont touch GroupLayout for whatever reason ): the collapsible is placed inside a container with BoxLayout (which distributes excess height between components until their max is reached). As a consequence, it sizes a zero-pref-size component - as a collapsed collapsible - nevertheless to something ... which leads to hard-to-predict behaviour. Uncommenting the override makes it well-behaved. Might be similar in group.

Alternatively, place the collapsible into a layout which always sizes to pref - that's probably the safest option, as the animation more or less relies on being sized to pref at all times.

    JXCollapsiblePane top = new JXCollapsiblePane() {

//            @Override
//            public Dimension getMaximumSize() {
//                return getPreferredSize();
//            }

    };
    top.add(new JLabel("some random label"));
    top.add(new JLabel("and another"));
    top.add(new JButton(top.getActionMap().get(JXCollapsiblePane.TOGGLE_ACTION)));
    top.setCollapsed(true);
    JXFrame frame = new JXFrame("collapsible in top", true);
    frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.PAGE_AXIS));
    frame.add(top, BorderLayout.NORTH);
    frame.add(new JScrollPane(new JXTable(50, 4)));
    frame.add(new JButton(top.getActionMap().get(JXCollapsiblePane.TOGGLE_ACTION)), BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true);

Edit

we have an open issue in SwingX which might show a related "problem". Though, thinking about it now, I would say that's not really a bug: The sizing of the collapsible always (expanded/collapsed or during animation) affords a LayoutManager which respects prefSize, at least doesn't grow beyond (with/out a max bound, doesn't matter so much - they are free to size whatever they want anyway). Can't think of a reliable way to have both animation and zero collapsed size, otherwise. Ideas welcome :-)

kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • 1
    no, just an example (btw: what happened when you removed it? Or in other words: simply try it - replace with whatever container you want to - that's what example snippets are meant for :-) – kleopatra Dec 12 '11 at 13:55
  • If i set layout for collapsible pane it starts working correctly. Thanks for helping me once more, kleopatra! you're awesome! – bunnyjesse112 Dec 13 '11 at 09:26
3

Since the code you posted is just a copy from the javadoc of JXCollapsiblePane it looks OK, and the problem is most likely located somewhere else.

For the animation, have you checked that animation of your collapsible pane is actually activated (see isAnimated). You can set it to animated by calling

collapsiblePane.setAnimated( true );

Since the button works to collapse the pane, and the action responsible for collapsing/expanding has a rather simple implementation (see source code) I think the problem is located elsewhere. Can you post some code on where you add the panel to your UI. Perhaps the parent container does not allow the component to expand once it is collapsed.

Robin
  • 36,233
  • 5
  • 47
  • 99