I have a program with a JScrollbar, a JButton and a JList which should run like this:
import java.awt.*;
import jawa.awt.event.ActionEvent;
import jawa.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.*;
@SuppressWarnings("serial")
public class ListClass extends JFrame implements ActionListener, ListSelectionListener {
static ListClass statList = new ListClass();
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(600, 800);
frame.setDefaultCloseOperation(3);
JPanel panel = new JPanel();
JTextArea newTask_field = new JTextArea();
newTask_field.setPreferredSize(new Dimension(400, 80));
newTask_field.setLineWrap(true);
newTask_field.setWrapStyleWord(true);
JScrollPane newTask = new JScrollPane(newTask_field);
newTask.setPreferredSize(new Dimension(400, 80));
JButton confirm = new JButton("Confirm");
confirm.setHorizontalAlignment(JButton.CENTER);
confirm.setEnabled(false);
DefaultListModel<String> listModel = new DefaultListModel<>();
JList<String> myList = new JList<String>(listModel);
myList.setPreferredSize(new Dimension(200, 400));
// Missing code //
panel.add(newTask);
panel.add(confirm);
panel.add(myList);
frame.add(panel);
frame.setVisible(true);
}
}
I want the JButton confirm to be enabled when the JScrollPane contains something and to be disabled again when the JScrollPane is empty. And when the JButton is clicked, the content of the JScrollPane becomes a new element in the JList, the JScrollPane is emptied and the JButton is disabled. But apparently a JScrollPane can't use the ActionListener. Also, there isn't any "if([Name of JScrollPane].isEmpty())" or "if([Name of JScrollPane].getContent=="")" or something like that.
How can I solve this problem?