I am under the impression that most of my code work, however, I think I am confident that I am only missing one line under the function updatedJList()
Please help, thank you.
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JList;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Ex1 extends JFrame {
private JTextField txtName;
private JList nameList;
private String[] nameArr;
private int arrCounter = 0;
private JLabel lblDisplayName;
public Ex1(){
this.setTitle("Exercise01");
this.setSize(300, 266);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
this.nameArr = new String[10];
JLabel lblName = new JLabel("Name:");
lblName.setBounds(10, 11, 264, 14);
getContentPane().add(lblName);
this.txtName = new JTextField();
this.txtName.setBounds(10, 25, 264, 20);
getContentPane().add(this.txtName);
this.txtName.setColumns(10);
JButton btnAddName = new JButton("Add Name to List");
btnAddName.setBounds(10, 49, 264, 23);
getContentPane().add(btnAddName);
btnAddName.addActionListener(new AddNameListener());
//Create ScrollPane
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 75, 264, 126);
getContentPane().add(scrollPane);
//Create and Add JList
this.nameList = new JList();
nameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
this.updateJList();
this.nameList.addListSelectionListener(new DisplayListener());
this.lblDisplayName = new JLabel("(Name will be shown here)");
this.lblDisplayName.setHorizontalAlignment(SwingConstants.CENTER);
this.lblDisplayName.setBounds(10, 203, 264, 14);
getContentPane().add(this.lblDisplayName);
this.setVisible(true);
}
private class AddNameListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
updateJList();
}
}
private void updateJList(){
//I think I am missing something on this line.. HELP
String name = this.txtName.getText();
this.txtName.setText(name);
this.arrCounter++;
this.txtName.setText("");
DefaultListModel model = new DefaultListModel();
for(int i =0;i<this.nameArr.length;i++)
{
model.addElement(name);
}
this.nameList.setModel(model);
}
private class DisplayListener implements ListSelectionListener {
@Override
public void valueChanged(ListSelectionEvent arg0) {
displayName();
}
}
private void displayName()
{
int index = this.nameList.getSelectedIndex();
String name = this.nameArr[index];
this.lblDisplayName.setText(name);
}
public static void main(String[] args) {
Ex1 gui = new Ex1();
}
}
I am very very very lost at this point, I cant seem to figure out what's wrong, I've been learning Java for about 8 weeks now, and I've also been working on this question for 4 hours. I really think I am missing a line, but if you think otherwise, please feel free to comment. Thank you, wonderful people!
Edit: After some discussion the issue that the OP is stating is the following:
Whenever I run the code, it will work, however, when I key in the name in the text box to store it in the array and display it, it will not show up in the text box, therefore I cannot display the name.
Compare the below code with that in your question. The changes are described after the code.
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JList;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Ex1 extends JFrame {
private JTextField txtName;
private JList<Object> nameList;
private String[] nameArr;
private int arrCounter = 0;
private JLabel lblDisplayName;
public Ex1() {
this.setTitle("Exercise01");
this.setSize(300, 266);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
this.nameArr = new String[10];
JLabel lblName = new JLabel("Name:");
lblName.setBounds(10, 11, 264, 14);
getContentPane().add(lblName);
this.txtName = new JTextField();
this.txtName.setBounds(10, 25, 264, 20);
getContentPane().add(this.txtName);
this.txtName.setColumns(10);
JButton btnAddName = new JButton("Add Name to List");
btnAddName.setBounds(10, 49, 264, 23);
getContentPane().add(btnAddName);
btnAddName.addActionListener(new AddNameListener());
// Create and Add JList
DefaultListModel<Object> model = new DefaultListModel<>();
this.nameList = new JList<>(model);
nameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// Create ScrollPane
JScrollPane scrollPane = new JScrollPane(nameList);
scrollPane.setBounds(10, 75, 264, 126);
getContentPane().add(scrollPane);
this.nameList.addListSelectionListener(new DisplayListener());
this.lblDisplayName = new JLabel("(Name will be shown here)");
this.lblDisplayName.setHorizontalAlignment(SwingConstants.CENTER);
this.lblDisplayName.setBounds(10, 203, 264, 14);
getContentPane().add(this.lblDisplayName);
this.setVisible(true);
}
private class AddNameListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
updateJList();
}
}
private void updateJList() {
// I think I am missing something on this line.. HELP
String name = this.txtName.getText();
this.txtName.setText(name);
this.arrCounter++;
this.txtName.setText("");
DefaultListModel<Object> model = (DefaultListModel<Object>) nameList.getModel();
model.addElement(name);
}
private class DisplayListener implements ListSelectionListener {
@Override
public void valueChanged(ListSelectionEvent arg0) {
displayName();
}
}
private void displayName() {
int index = this.nameList.getSelectedIndex();
if (index >= 0) {
ListModel<Object> model = nameList.getModel();
Object obj = model.getElementAt(index);
String name = obj == null ? "" : obj.toString();
this.lblDisplayName.setText(name);
}
}
public static void main(String[] args) {
Ex1 gui = new Ex1();
}
}
Create a model for the JList
first and pass it to the JList
constructor.
DefaultListModel<Object> model = new DefaultListModel<>();
this.nameList = new JList<>(model);
You are adding an empty JScrollPane
. You need to pass the JList
to the JScrollPane
constructor.
JScrollPane scrollPane = new JScrollPane(nameList);
In method updateJList()
, you don't need a new model, you just need to update the existing model by adding an element to it.
DefaultListModel<Object> model = (DefaultListModel<Object>) nameList.getModel();
model.addElement(name);
Excuse me, I didn't pay attention to that part of your code that displays the selected value from nameList
.
I changed method displayName()
in the above code to the following:
private void displayName() {
int index = this.nameList.getSelectedIndex();
if (index >= 0) {
ListModel<Object> model = nameList.getModel();
Object obj = model.getElementAt(index);
String name = obj == null ? "" : obj.toString();
this.lblDisplayName.setText(name);
}
}
You don't need nameArr
and you don't need arrCounter
.