0

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zero
  • 84
  • 8
  • You don't use a JScrollBar. You use a JScrollPane as a container for the Swing component you wish to scroll. Oracle has a helpful tutorial, [Creating a GUI With Swing](https://docs.oracle.com/javase/tutorial/uiswing/index.html). Skip the Learning Swing with the NetBeans IDE section. Pay particular attention to the [How to Use Scroll Panes](https://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html) section. – Gilbert Le Blanc Sep 20 '22 at 14:27

1 Answers1

0

You may be able to just check if newTask_Field is empty or not since it is the content you have put inside JScrollPane.

if(newTask_Field.getText().eqauls("")){}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Snowald
  • 16
  • 1
  • I could do this, but I still need the program to somehow notice it whenever I want it to notice. So I probably need a listener. To enable the JButton when the JTextArea (or the JScrollPane) is not empty and to disable it, it has to be checked everytime I change it. If I do the if-statement somewhere in the main-method (or in another method and then call it in main), it will just be called when it's reached and I don't see a way to call the statement exactly when I want it to be checked - other than a listener which I don't know how to use on this element. Do you have an idea how this could go? – Zero Sep 21 '22 at 05:24
  • I think this is what you want https://stackoverflow.com/questions/7740465/text-changed-event-in-jtextarea-how-to – Snowald Sep 22 '22 at 10:04