Reputation: 11
I am currently trying to develop a desktop volunteer application that allows a user to input a name via a textbox and then display this name on the same window in a JList
. However, currently my code is not displaying my panel/ list or the scrollbar that I added as well.
My code is below. Any help is greatly appreciated!
import javax.swing.*;
import java.awt.event.*;
public class StudentViewer implements ActionListener {
JFrame frame = new JFrame("View Students");
JPanel panel = new JPanel();
JButton button = new JButton();
JButton button2 = new JButton();
JTextField field = new JTextField("", 25);
DefaultListModel<String> model = new DefaultListModel<>();
JList<String> list = new JList<>(model);
public StudentViewer() {
button = new JButton("Click to add to the list");
button.setFocusPainted(false);
button.addActionListener(this);
button2 = new JButton("New Window");
button2.setFocusPainted(false);
button2.addActionListener(this);
panel.add(field);
panel.add(button);
panel.add(button2);
list = new JList(model);
list.setFixedCellWidth(300);
list.setFixedCellHeight(50);
list.setLayoutOrientation(JList.VERTICAL);
// scroll = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// panel.add(scroll);
frame.add(panel);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
String text = field.getText();
model.addElement(text);
JOptionPane.showMessageDialog(frame, "Added to the list", "Done",
JOptionPane.INFORMATION_MESSAGE);
frame.dispose();
} else if (e.getSource() == button2) {
System.exit(0);
}
}
public static void main(String[] args) {
Runnable r = () -> {
new StudentViewer();
};
SwingUtilities.invokeLater(r);
}
}
Upvotes: 1
Views: 139
Reputation: 168845
There is much about the code that either was actively part of the problem or just made no sense. Have a look over the changes made to produce this screenshot.
import javax.swing.*;
import java.awt.event.*;
public class StudentViewer implements ActionListener {
JFrame frame = new JFrame("View Students");
JPanel panel = new JPanel();
JButton button = new JButton();
JButton button2 = new JButton();
JTextField field = new JTextField("", 5);
DefaultListModel<String> model = new DefaultListModel<>();
JList<String> list = new JList<>(model);
public StudentViewer() {
button = new JButton("Click to add to the list");
button.setFocusPainted(false);
button.addActionListener(this);
button2 = new JButton("New Window");
button2.setFocusPainted(false);
button2.addActionListener(this);
panel.add(field);
panel.add(button);
panel.add(button2);
list = new JList(model);
//list.setFixedCellWidth(300);
//list.setFixedCellHeight(50);
//list.setLayoutOrientation(JList.VERTICAL);
JScrollPane scroll = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panel.add(scroll);
frame.add(panel);
// frame.setSize(400, 400); redundant given pack()
frame.pack();
frame.setVisible(true);
//frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
String text = field.getText();
model.addElement(text);
JOptionPane.showMessageDialog(frame, "Added to the list", "Done",
JOptionPane.INFORMATION_MESSAGE);
//frame.dispose();
} else if (e.getSource() == button2) {
System.exit(0);
}
}
public static void main(String[] args) {
Runnable r = () -> {
new StudentViewer();
};
SwingUtilities.invokeLater(r);
}
}
Upvotes: 2