It drives me crazy. I try to change panels when you click on 3 different buttons, and this works, but for one panel only once.
If you click the AddPerson button - the Person panel, If you then click addCD - the CD panel will appear; (the same for the browse store) If you then click on addPerson, this will not work. It throws a nullpointer exception. Even if you click on addCD / viewstore and THEN add the Person that it shows, but it just wonβt appear a second time.
In the test file, I created a graphical interface with adding and removing: if I clicked on the add button, it would throw a null pointer exception, but if I just added it and compiled, everything was fine ...
public JPanel create_PersonPnl() { personPnl = new JPanel(new FlowLayout(FlowLayout.CENTER)); personPnl.setBackground(Color.WHITE); personPnl.setPreferredSize(minPnl); Box personBox = Box.createVerticalBox(); personBox.setBorder(new TitledBorder(new LineBorder(Color.DARK_GRAY), "Person")); Box nameBox = Box.createHorizontalBox(); nameBox.add(Box.createHorizontalStrut(10)); nameLbl = new JLabel("Name: "); nameBox.add(nameLbl); nameBox.add(Box.createHorizontalStrut(5)); nameTF = new JTextField(); nameBox.add(nameTF); nameBox.add(Box.createHorizontalStrut(10)); personBox.add(nameBox); personBox.add(Box.createVerticalStrut(10)); Box limitBox = Box.createHorizontalBox(); limitBox.add(Box.createHorizontalStrut(10)); limitLbl = new JLabel("CD Limit: "); limitBox.add(limitLbl); limitTF = new JFormattedTextField(); limitBox.add(limitTF); limitBox.add(Box.createHorizontalStrut(10)); personBox.add(limitBox); personBox.add(Box.createVerticalStrut(10)); personBox.add(addPersonBtn = new JButton("Add Person")); personBox.add(Box.createVerticalStrut(10)); personList = new JList(temp); personList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); scrollp = new JScrollPane(personList); scrollp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); scrollp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); scrollp.setSize(100, 45); personBox.add(scrollp); personBox.add(Box.createVerticalStrut(10)); Box optionsBox = Box.createHorizontalBox(); editPrsnBtn = new JButton("Edit"); editPrsnBtn.addActionListener(this); optionsBox.add(editPrsnBtn); removePrsnBtn = new JButton("Remove"); removePrsnBtn.addActionListener(this); optionsBox.add(removePrsnBtn); personBox.add(optionsBox); personPnl.add(personBox); return personPnl; }
This is what is in my ActionPerformed method.
if(e.getSource() == addPersonBtn) { changePnl.removeAll(); changePnl.add(create_PersonPnl()); changePnl.revalidate(); System.out.println("PersonPnl added"); } if(e.getSource() == addCDBtn) { changePnl.removeAll(); changePnl.add(create_CDPnl()); changePnl.revalidate(); } if(e.getSource() == viewStoreBtn) { changePnl.removeAll(); changePnl.add(create_StorePnl()); changePnl.revalidate(); }
Only code for ChangePnl.
changePnl = new JPanel(); changePnl.setBackground(Color.WHITE); defaultPnl = new JPanel(); defaultPnl.setBackground(Color.WHITE); defaultPnl.add(new JLabel("Welcome to the CD Store")); defaultPnl.add(new JLabel("Click an option from the left")); changePnl.add(defaultPnl);
SOP had to debug in order to see what was running .. and that only prints once and that it if I did not pull out .add (create_PersonPnl ()); so I narrowed it down, but I have no clue as it works the first time.
Thanks in advance!
Separate the test file to prove it create_PersonPnl ()
import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import javax.swing.border.LineBorder; import javax.swing.border.TitledBorder; public class Test implements ActionListener { JButton add, remove; JButton addPersonBtn, editPrsnBtn, removePrsnBtn; JFrame frame; JFormattedTextField limitTF; JLabel nameLbl, limitLbl; JList personList; JPanel TotalGUI, welcomePnl, mainPnl, imagePnl, changePnl, defaultPnl, cdPnl, storePnl; JPanel personPnl; JScrollPane scrollp; JTextField nameTF; Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); Dimension minPnl = new Dimension(300, 400); /* Test for JList */ String[] temp = {"1", "2", "3", "4", "1", "2", "3", "4","1", "2", "3", "4","1", "2", "3", "4" }; public Test() { frame = new JFrame("CD Store"); frame.setExtendedState(JFrame.NORMAL); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //frame.pack(); //sets size based on components size in TotalGUI //frame.setJMenuBar(create_MenuBar()); //frame.setMinimumSize(minDim); frame.setSize(725, 550); //set frame location (central to screen) int fw = frame.getSize().width; int fh = frame.getSize().height; int fx = (dim.width-fw)/2; int fy = (dim.height-fh)/2; frame.getContentPane().add(create_Content_Pane()); frame.setVisible(true); //moves the frame to the centre frame.setLocation(fx, fy); } public JPanel create_Content_Pane() { JPanel TotalGUI = new JPanel(); TotalGUI.add(remove = new JButton("Remove")); remove.addActionListener(this); TotalGUI.add(add = new JButton("Add")); <- this crashes when selected add.addActionListener(this); //TotalGUI.add(create_PersonPnl()); <- works just fine return TotalGUI; } public JPanel create_PersonPnl() { personPnl = new JPanel(new FlowLayout(FlowLayout.CENTER)); personPnl.setBackground(Color.WHITE); personPnl.setPreferredSize(minPnl); /* VERTICAL BOX for Person boxes */ Box personBox = Box.createVerticalBox(); personBox.setBorder(new TitledBorder(new LineBorder(Color.DARK_GRAY), "Person")); /* Horizontal Box for Name Lbl & TF */ Box nameBox = Box.createHorizontalBox(); nameBox.add(Box.createHorizontalStrut(10)); nameLbl = new JLabel("Name: "); nameBox.add(nameLbl); nameBox.add(Box.createHorizontalStrut(5)); nameTF = new JTextField(); nameBox.add(nameTF); nameBox.add(Box.createHorizontalStrut(10)); personBox.add(nameBox); personBox.add(Box.createVerticalStrut(10)); Box limitBox = Box.createHorizontalBox(); limitBox.add(Box.createHorizontalStrut(10)); limitLbl = new JLabel("CD Limit: "); limitBox.add(limitLbl); limitTF = new JFormattedTextField(); limitBox.add(limitTF); limitBox.add(Box.createHorizontalStrut(10)); personBox.add(limitBox); personBox.add(Box.createVerticalStrut(10)); personBox.add(addPersonBtn = new JButton("Add Person")); personBox.add(Box.createVerticalStrut(10)); personList = new JList(temp); personList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); scrollp = new JScrollPane(personList); scrollp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); scrollp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); scrollp.setSize(100, 45); personBox.add(scrollp); personBox.add(Box.createVerticalStrut(10)); Box optionsBox = Box.createHorizontalBox(); editPrsnBtn = new JButton("Edit"); editPrsnBtn.addActionListener(this); optionsBox.add(editPrsnBtn); removePrsnBtn = new JButton("Remove"); removePrsnBtn.addActionListener(this); optionsBox.add(removePrsnBtn); personBox.add(optionsBox); personPnl.add(personBox); return personPnl; } public static void main(String[] args) { new Test(); } public void actionPerformed(ActionEvent e) { if(e.getSource() == remove) { TotalGUI.removeAll(); TotalGUI.revalidate(); } if(e.getSource() == add) { TotalGUI.add(create_PersonPnl()); TotalGUI.revalidate(); } } }