SwingX 的 JXCollapsiblePane,无法正常工作

发布于 2024-12-20 08:59:42 字数 6555 浏览 0 评论 0原文

我最近发现了 swingx,并想尝试一下 JXCollapsiblePane。但我无法让它工作。我找到了这段代码:

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

但是单击按钮没有任何反应。如果我最初将窗格设置为折叠,则当单击按钮时,窗格会关闭而没有动画,并且后续单击不会执行任何操作。我不擅长编程,请给一些建议。

编辑 我使用 IDE Netbeans 将组件拖放到框架中

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

EDIT2 好吧,问题是它被设置为向上切换,但没有地方可以折叠,我将其更改为向右并且有点工作,但仍然没有动画。我尝试手动创建框架和窗格,效果很好。我想问题出在 gui 插件将 collapsiblepane 添加到框架的方式上。

也许有使用 JXCollapsiblePane 和 Netbeans 经验的人可以帮助

EDIT3 在此处输入图像描述

在此处输入图像描述

当 collapsiblePane 打开时,框架上的其他内容不会移动。我从 gui 构建器创建窗格,在其中放置 JPanel,在面板中放置一些按钮和文本字段;代码中的标签和按钮:

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() 是 IDE 创建的定义布局的方法,我无法编辑它,我认为这个方法是问题

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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

指尖上的星空 2024-12-27 08:59:42

这是一个可能模拟问题的代码片段(无论出于何种原因,都不会接触 GroupLayout):可折叠元素被放置在带有 BoxLayout 的容器内(它在组件之间分配多余的高度,直到达到最大值)。因此,它将零偏好大小的组件的大小设置为折叠的可折叠组件,但仍然是某种东西......这会导致难以预测的行为。取消注释覆盖使其表现良好。组里可能也有类似的情况。

或者,将可折叠元素放入始终按首选大小调整的布局中 - 这可能是最安全的选择,因为动画或多或少依赖于始终按首选大小调整。

    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);

编辑

我们有一个SwingX 中的未解决问题,可能会显示相关“问题”。不过,现在想想,我想说这并不是一个真正的错误:可折叠的大小始终(展开/折叠或在动画期间)提供了一个尊重 prefSize 的 LayoutManager,至少不会增长超过(没有/没有最大大小)绑定,并不那么重要 - 他们可以自由地调整任何他们想要的大小)。否则无法想出一种既具有动画又具有零折叠尺寸的可靠方法。欢迎提出想法:-)

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 :-)

各自安好 2024-12-27 08:59:42

由于您发布的代码只是 JXCollapsiblePane 的 javadoc 的副本,因此看起来不错,并且问题很可能位于其他地方。

对于动画,您是否检查过可折叠窗格的动画实际上已激活(请参阅isAnimated)。您可以通过调用将其设置为动画,

collapsiblePane.setAnimated( true );

因为按钮可以折叠窗格,并且负责折叠/展开的操作有一个相当简单的实现(请参阅源代码)我认为问题定位其他地方。您可以在将面板添加到用户界面的位置上发布一些代码吗?也许父容器不允许组件在折叠后展开。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文