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?