I have been working on an app, where you set up a list of categories with items in it, which will then be made into a text file. But the GUI I’ve made for it doesn’t work entirely. I think the error is situated in the JMenuItem called Cat (or “add category”) or in the way I display the Categories (Update() method). It is supposed to ask for a name, make a Category named after that and display it in a JScrollPane, but nothing comes up. Here is the code:
public class GUIBuilder {
public JFrame frame;
public JPanel LeftPanel;
public JPanel RightPanel;
public JScrollPane scroll;
public JMenuBar bar;
public JMenu File;
public JMenu Add;
Inventory inv;
public void go() {
frame = new JFrame();
scroll = new JScrollPane();
bar = new JMenuBar();
File = new JMenu("File");
Add = new JMenu("Add...");
bar.add(File);
bar.add(Add);
JMenuItem Save = new JMenuItem(new AbstractAction("Save") {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent arg0) {
}
});
JMenuItem Load = new JMenuItem(new AbstractAction("Load") {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent arg0) {
}
});
JMenuItem Generate = new JMenuItem(new AbstractAction("Generate Text File") {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent arg0) {
}
});
File.add(Save);
File.add(Load);
File.add(Generate);
JMenuItem Cat = new JMenuItem(new AbstractAction("Add Category") {
private static final long serialVersionUID = 1L;
JFrame Cat;
public void actionPerformed(ActionEvent arg0) {
Cat = new JFrame("Add Category");
final JTextField name = new JTextField(15);
JButton Submit = new JButton(new AbstractAction("Submit") {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent arg0) {
String n = name .getText();
if (n != null) {
inv.addCategory(new Category(n));
pullThePlug();
GUIBuilder.this.Update();
}
}
});
Cat.setLayout(new BorderLayout());
Cat.add(name, BorderLayout.CENTER);
Cat.add(Submit, BorderLayout.SOUTH);
Cat.setSize(250, 150);
Cat.setVisible(true);
}
public void pullThePlug() {
WindowEvent wev = new WindowEvent(Cat, WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
}
});
JMenuItem item = new JMenuItem(new AbstractAction("Add Item") {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent arg0) {
}
});
Add.add(Cat);
Add.add(item);
frame.setJMenuBar(bar);
frame.setSize(500, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(scroll);
inv = new Inventory();
}
public void Update() {
for (int i = 0; i < inv.categories.size(); i++) {
Category cat = inv.categories.get(i);
JPanel p = new JPanel();
JTextPane name = new JTextPane();
name.setText(cat.getName());
scroll.add(p);
}
}
}
Thanks in advance 😉
You need to add your
JTextPane nameto yourJPanel p.(It also seems to me that you’re missing a
mainmethod that will construct aGUIBuilder– but maybe that’s in another file that you haven’t shown in your question?)Also, if you create an empty
JScrollPane, you need to add components viascrollpane.getViewport().add(yourcomponent);.You also need some layout management. The easiest way (and probably what you intended to do anyways) is: