0

so I'm having a problem with my buttons in java. I created my buttons through extending a JButton so I can have all of my buttons looking the same. Now, I added an ActionListener to the constructor of that button, and the ActionListener decides what action to run based on the text of the button. (Using

String buttonString = e.getActionCommand(); 

and then uses a switch/case to check the text.)

At the moment, I have it where when I click a button, it adds a new JXCollapsiblePane, but it's added to the bottom of the panel. How do I get it to be added to underneath the buttons?

An SSCCE based on my code, which is roughly the same:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.jdesktop.swingx.JXCollapsiblePane;

public class ActualCode{
    public static JPanel newPanel;
    public static JFrame frame;

    public static void createGUI(){
        JButton newButton= new JButton("TextButton");
        newButton.addActionListener(new ButtonClickActionK());

        JPanel newPanel = new JPanel();
        newPanel.setVisible(true);
        newPanel.setOpaque(false);
        newPanel.setLayout(new BorderLayout());
        newPanel.add(newButton, BorderLayout.NORTH);

        JFrame frame = new JFrame("Command Line Test");
        frame.setLayout(new BorderLayout());
        frame.add(newPanel, BorderLayout.CENTER);

        frame.setSize(new Dimension(720, 480));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }



    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createGUI();

            } // public void run() Closing
        }); // SwingUtilities Closing
    }

}

 class ButtonClickActionK extends ActualCode implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        String str = e.getActionCommand();

        switch (str) {


        case "TextButton":
            //TODO Make a JXCollapsiblePane slide out
            System.out.println("Slide out panel not made yet!");
            JXCollapsiblePane testp = new JXCollapsiblePane();
            newPanel.add(testp);
            break;

        default:
            JOptionPane.showMessageDialog(frame, "Erm, this is embarrasing! Something went (horribly) wrong...");
        }
    }
}

Stack Trace Error:

Slide out panel not made yet!
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at com.warlords.warlordsorganizer.ButtonClickActionK.actionPerformed(ActualCode.java:64)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

The MAIN problem I'm having is just how to add the JXCollapsiblePane directly under the button, and how to set the toggle to said button. The rest I can probably figure out by myself.

Anything else I can provide to help?

Hathor
  • 187
  • 1
  • 2
  • 10
  • the NPE is due to the not initialized class field newPanel. That said: the setup is rather weird a) why do you subclass ActualCode? All you need is a Action/Listener b) subclassing JSomething usually is a bad idea, they are meant to be used as-are and configured as needed – kleopatra Jul 03 '13 at 14:25

1 Answers1

2

Here's a snippet which

  • is adding the collapsible without needing a reference to the panel: simply add to parent of the source of ActionEvent
  • not extending anything unrelated (to its ActionListener-ness)

Note that the parent container of a JXCollapsiblePane must use a LayoutManager which respects the collapsible's preferredSize always, otherwise you'll get unpredicatable effects on collapse/expand

class ButtonClickActionK implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        String str = e.getActionCommand();
        Component source = (Component) e.getSource();

        switch (str) {


        case "TextButton":
            Container parent = source.getParent();
            JXCollapsiblePane testp = new JXCollapsiblePane();
            testp.add(new JLabel("dummy, just to see we are open"));
            parent.add(testp);
            parent.revalidate();
            break;

        default:
            JOptionPane.showMessageDialog(source, 
               "Erm, this is embarrasing! Something went (horribly) wrong...");
        }
    }
}

// usage
JButton newButton= new JButton("TextButton");
newButton.addActionListener(new ButtonClickActionK());
// note: _not_ using a class field
JComponent newPanel = new JPanel(new VerticalLayout());
newPanel.add(newButton);
kleopatra
  • 51,061
  • 28
  • 99
  • 211