JXCollapsiblePane from SwingX, not working correctly

I recently found swingx and wanted to experiment with JXCollapsiblePane. But I can’t make it work. I found this code:

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

But pressing a button does nothing. If I first installed the panel, when I click the button, the panel closes WITHOUT animation, and subsequent clicks do nothing. Im terrible at programming, please give some advice.

EDIT I use the NetBeans IDE to drag a component into a frame

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

EDIT2 Okay, the problem is that it was configured to enable UP, but there was no room for a breakup, I changed it to CORRECT and it seems to work, but still animation. I tried just creating the frame and panel manually, and everything works fine. I think the problem is how the gui plugin adds collapsiblepane to the frame.

Maybe someone with experience with JXCollapsiblePane and Netbeans can help

EDIT3 enter image description here

enter image description here

When the resettable panel opens, other things in the frame do not move. I create a panel from gui builder, put the JPanel in it, some buttons and text fields in the panel; and the label and button from the 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 a method created by the IDE that defines the layout, I cannot edit it, and I think this method is a 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> 
+4
source share
2 answers

here is a piece of code that can simulate a problem (do not touch GroupLayout for any reason): the foldable one is inside the container with BoxLayout (which distributes the excess height between the components until the maximum value is reached). As a result, it constitutes a component with a zero prefix - like a convoluted folded one - however, to something ... which leads to difficult to predict behavior. Exposing overrides makes it good. May be similar in group.

Alternatively, put a drop-down on a layout that always has a size up to pref - this is probably the safest option since the animation more or less relies on being set to pref anytime.

  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 problem in SwingX that may show a related β€œproblem”. Although, thinking about it now, I would say that it’s actually not a mistake: the size of the disassembly always (expanded / compensated or during animation) provides a LayoutManager that respects prefSize, at least it does not grow (with / out max does not have values ​​- they are free in any size, no matter what they want). It is impossible to think of a reliable way to have both animation and zero collapsed size, otherwise. Ideas are welcome :-)

+4
source

Since the code you submitted is just a copy from the javadoc JXCollapsiblePane , it looks fine and the problem is most likely somewhere else.

For the animation, you checked that the animation of your reset panel is actually activated (see isAnimated ). You can configure it to animate by calling

 collapsiblePane.setAnimated( true ); 

Since the button works to minimize the panel, and the action responsible for minimizing / expanding has a fairly simple implementation (see the source code ), I think that the problem is in a different place. Can you post some code where you add a panel to your interface. Perhaps the parent container does not allow the component to expand after it collapses.

+3
source

Source: https://habr.com/ru/post/1385372/


All Articles