Reputation: 3
I need help on my program. I need to have a JScrollPane on a JList without putting JList on a JPanel.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class refurbished extends JFrame implements ActionListener {
ArrayList<String> names;
JButton add;
JTextField inputName;
JScrollPane scrollName;
JList nameList;
public refurbished() {
setSize(700,500);
setLayout(null);
names = new ArrayList<String>();
add = new JButton("Add");
add.setBounds(25,200,90,30);
add.setBackground(Color.WHITE);
add.addActionListener(this);
inputName = new JTextField();
inputName.setBounds(150,350,150,30);
nameList = new JList(names.toArray());
scrollName = new JScrollPane(nameList);
scrollName.setBounds(150,75,150,200);
getContentPane().add(add);
getContentPane().add(inputName);
getContentPane().add(scrollName);
setVisible(true);
}
public void actionPerformed (ActionEvent buttonclick) {
if (buttonclick.getSource() == add) {
names.add(inputName.getText().toLowerCase());
nameList = new JList(names.toArray());
scrollName = new JScrollPane(nameList);
scrollName.setBounds(150,75,150,200);
}
}
public static void main (String[] args) {
refurbished r = new refurbished();
}
}
Can you help me? I really need your help because this is the only missing feature in my code.
Your help is really appreciated.
Upvotes: 0
Views: 139
Reputation: 208944
Yout haven't added the lists to the scroll panes. You've only added the lists to the contentpane.
scrollName = new JScrollPane();
scrollNumber = new JScrollPane();
getContentPane().add(nameList); <-- Get rid of this
getContentPane().add(numberList); <-- Get rid of this
You need this instead
scrollName = new JScrollPane(nameList);
scrollNumber = new JScrollPane(numberList);
getContentPane().add(scrollName);
getContentPane().add(scrollNumber);
Also as @Alex2410 noted in his comment below, "Also you need to use LayoutManager, or set Bounds to JScrollPane rather then to JList"
Update: to original poster update
You need to both revalidate()
and repaint()
after adding or removing components. Add revalidate()
before where you have repaint()
. You only need to revalidate()
repaint()
once in the method
Edit: If you want to update the list, use a ListModel. You don't need to replace the entire list with a new list
See this code. What I've done is used a DefaultListModel
and set that model to the Jlist
. Then you can add element to the list dynamically. I fixed your code and it works. I commented on the adds and what I removed
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class refurbished extends JFrame implements ActionListener {
ArrayList<String> names;
JButton add;
JTextField inputName;
JScrollPane scrollName;
JList nameList;
DefaultListModel model; <-- declare DefaultListModel
public refurbished() {
setSize(700, 500);
setLayout(null);
names = new ArrayList<String>();
add = new JButton("Add");
add.setBounds(25, 200, 90, 30);
add.setBackground(Color.WHITE);
add.addActionListener(this);
inputName = new JTextField();
inputName.setBounds(150, 350, 150, 30);
model = new DefaultListModel(); <-- Initialize model
nameList = new JList(model); <-- set model to list
scrollName = new JScrollPane(nameList);
scrollName.setBounds(150, 75, 150, 200);
getContentPane().add(add);
getContentPane().add(inputName);
getContentPane().add(scrollName);
setVisible(true);
}
public void actionPerformed(ActionEvent buttonclick) {
if (buttonclick.getSource() == add) {
//names.add(inputName.getText().toLowerCase());
//nameList = new JList(names.toArray()); <-- don't need all this
//scrollName = new JScrollPane(nameList);
//scrollName.setBounds(150, 75, 150, 200);
String name = inputName.getText(); <-- get input
names.add(name);
model.addElement(name); <-- add name to model
}
}
public static void main(String[] args) {
refurbished r = new refurbished();
}
}
Take a look at Using Models
. You should take time to learn the MVC (Model, View, Controller) paradigm.
Upvotes: 3