# \[JAVA\] Is there no 'scroll function' library?

**URL:** https://discourse.processing.org/t/java-is-there-no-scroll-function-library/39296
**Category:** Libraries
**Created:** [October 16, 2022, 12:59pm UTC](https://discourse.processing.org/t/java-is-there-no-scroll-function-library/39296 "2022-10-16T12:59:11Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)
#### Post date: [October 16, 2022, 12:59pm UTC](https://discourse.processing.org/t/java-is-there-no-scroll-function-library/39296/1 "2022-10-16T12:59:11Z")

</div>

[JAVA] Is there no ‘scroll function’ library?

hello. Thanks for clicking on my article.

 ![t1](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/e/4/e4fff6cd813078cad3a19cab94ac18a7441bb9aa.jpeg)

As an example, I upload a picture (program picture).  
As shown in the figure, you can view or input multiple data through ‘scroll’.

1. ‘controlP5’ library

- Example: ControlP5 MenuList

1. The problem with the above library is a burden on the computer ‘CPU’.

1. Examples provided by ‘[processing.org](http://processing.org)’  
[Scrollbar / Examples / Processing.org](https://processing.org/examples/scrollbar.html)

1. The current library is horizontal.
2. It is not easy to apply.

Is there an easy way or ‘library’ to solve it?  
Anyone who knows, please help.

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [October 20, 2022, 4:13pm UTC](https://discourse.processing.org/t/java-is-there-no-scroll-function-library/39296/2 "2022-10-20T16:13:56Z")

</div>

Would a JScrollPane work? This demo scrolls an embedded JTable.

```auto
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

javax.swing.JFrame frame;
java.awt.Canvas canvas;

final int _wndW = 400;
final int _wndH = 250;

void buildWnd(){
  // ===== Label ===== //
  JLabel label = new JLabel();
  label.setHorizontalAlignment(JLabel.CENTER);
  label.setBounds(30, 10, 200, 30);
  label.setText("Label for output display.");
  label.setToolTipText("label");
  frame.add(label);
  label.repaint();
    
  // ===== Table with scroll ===== //
   String data[][] = {
    {"Boat", "Yellow", "Yes"},
    {"Car", "Magenta", "Yes"},
    {"House", "White", "No"},
    {"Canoe", "Brown", "Yes"},
    {"Trailer", "Gray", "Yes"},
    {"Tractor", "Green", "No"},
    {"Lawn Mower", "Red", "No"},
    {"Fish Tank", "Glass", "Yes"},
    {"Horse", "Sorrel", "No"},
  };
  String header[] = {"ITEM", "COLOR", "FOR SALE"};
  JTable table = new JTable(data, header);
  table.setSelectionForeground(Color.WHITE);
  table.setToolTipText("table");
  JScrollPane scrlPane = new JScrollPane(table);
  scrlPane.setBounds(30, 50, 290, 80);
  frame.add(scrlPane);
  // ****Listener**** //
  ListSelectionModel cellSelectionModel = table.getSelectionModel();
  cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  cellSelectionModel.addListSelectionListener(new ListSelectionListener() {
    void valueChanged(ListSelectionEvent evnt) {
      String selectedData = null;
      int[] selectedRow = table.getSelectedRows();
      int[] selectedColumns = table.getSelectedColumns();
      for (int i = 0; i < selectedRow.length; i++) {
        for (int j = 0; j < selectedColumns.length; j++) {
          selectedData = (String) table.getValueAt(selectedRow[i], selectedColumns[j]);
          label.setText("Selected: " + selectedData);
        }
      }
    }
  }
  );
}

void setup() {
  frame = (javax.swing.JFrame) ((processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative()).getFrame();
  canvas = (processing.awt.PSurfaceAWT.SmoothCanvas) ((processing.awt.PSurfaceAWT)surface).getNative();  
  frame.setBounds(500, 300, _wndW, _wndH);
  frame.remove(canvas);
  surface.setResizable(true);
  surface.setTitle("Scrolling Table Demo");
  
  javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
      buildWnd(); // Builds components on EventDispatchThread
    }
  }
  );
}

```

 ![scrollPane_table](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/c/d/cd1215c95fb7c8b7ffb09245d7985d66d004c9f6.png)

---

<div class="post-metadata">

### Author: ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)
#### Post date: [October 20, 2022, 9:12pm UTC](https://discourse.processing.org/t/java-is-there-no-scroll-function-library/39296/3 "2022-10-20T21:12:08Z")

</div>

@svan

Thanks for answering my question.  
I am studying your source code.  
However, there is one error.

```auto
  cellSelectionModel.addListSelectionListener(new ListSelectionListener() {

    void valueChanged(ListSelectionEvent evnt) {
      String selectedData = null;
      int[] selectedRow = table.getSelectedRows();
      int[] selectedColumns = table.getSelectedColumns();
      for (int i = 0; i < selectedRow.length; i++) {
        for (int j = 0; j < selectedColumns.length; j++) {
          selectedData = (String) table.getValueAt(selectedRow[i], selectedColumns[j]);
          label.setText("Selected: " + selectedData);
        }
      }
    }
    
  }
  );

```

> Cannot reduce the visibility of the inherited method from ListSelectionListener

By any chance, do you know the reason?

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [October 20, 2022, 9:34pm UTC](https://discourse.processing.org/t/java-is-there-no-scroll-function-library/39296/4 "2022-10-20T21:34:18Z")

</div>

Runs without error on my system(Mac). Did you alter the source code?

JavaDocs are here: [https://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html](https://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html)

Try switching it over to checking just for single row and column and see if that makes a difference;

```auto
  cellSelectionModel.addListSelectionListener(new ListSelectionListener() {
    void valueChanged(ListSelectionEvent evnt) {
      String selectedData = null;
      int selectedRow = table.getSelectedRow();
      int selectedColumn = table.getSelectedColumn();
      selectedData = (String) table.getValueAt(selectedRow, selectedColumn);
      label.setText("Selected: " + selectedData);
    }
  }
  );

```

---

<div class="post-metadata">

### Author: ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)
#### Post date: [October 20, 2022, 10:59pm UTC](https://discourse.processing.org/t/java-is-there-no-scroll-function-library/39296/5 "2022-10-20T22:59:39Z")

</div>

@svan

```auto
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

javax.swing.JFrame frame;
java.awt.Canvas canvas;

final int _wndW = 400;
final int _wndH = 250;

JTable table;
JLabel label;
   String data[][] = {
    {"Boat", "Yellow", "Yes"},
    {"Car", "Magenta", "Yes"},
    {"House", "White", "No"},
    {"Canoe", "Brown", "Yes"},
    {"Trailer", "Gray", "Yes"},
    {"Tractor", "Green", "No"},
    {"Lawn Mower", "Red", "No"},
    {"Fish Tank", "Glass", "Yes"},
    {"Horse", "Sorrel", "No"},
  };
String header[] = {"ITEM", "COLOR", "FOR SALE"};
  
  
void buildWnd(){
  // ===== Label ===== //
  // JLabel label = new JLabel();  
  label = new JLabel();
  label.setHorizontalAlignment(JLabel.CENTER);
  label.setBounds(30, 10, 200, 30);
  label.setText("Label for output display.");
  label.setToolTipText("label");
  frame.add(label);
  label.repaint();
    
  // ===== Table with scroll ===== //

  // JTable table = new JTable(data, header);
  
  table = new JTable(data, header);
  table.setSelectionForeground(Color.WHITE);
  table.setToolTipText("table");
  JScrollPane scrlPane = new JScrollPane(table);
  scrlPane.setBounds(30, 50, 290, 80);
  frame.add(scrlPane);
  
  
  // ****Listener**** //
  ListSelectionModel cellSelectionModel = table.getSelectionModel();
  cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
  cellSelectionModel.addListSelectionListener(new ListSelectionListener() {    
    public void valueChanged(ListSelectionEvent e) {
      String selectedData = null;
      int selectedRow = table.getSelectedRow();
      int selectedColumn = table.getSelectedColumn();
      selectedData = (String) table.getValueAt(selectedRow, selectedColumn);
      label.setText("Selected: " + selectedData);
      println("Selected: " + selectedData.toString());
      println("data: " +data[selectedRow][selectedColumn]);
   }        
  });

}

void setup() {
  size(800, 800);
  
  frame = (javax.swing.JFrame) ((processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative()).getFrame();
  canvas = (processing.awt.PSurfaceAWT.SmoothCanvas) ((processing.awt.PSurfaceAWT)surface).getNative();  
  frame.setBounds(500, 300, _wndW, _wndH);
  frame.remove(canvas);
  surface.setResizable(true);
  surface.setTitle("Scrolling Table Demo");
  
  javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
      buildWnd(); // Builds components on EventDispatchThread
    }
  }
  );
  
}

```

 ![fig1](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/e/1/e109e3df5a5d92b764cc67fdca8fa2bcacac74d5.jpeg)

It was solved as above.

However, I am curious about two things.

1. I want to put a ‘Jframe’ canvas inside the processing canvas.

2. When you change a value in the table, you want to get feedback on it.

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [October 20, 2022, 11:42pm UTC](https://discourse.processing.org/t/java-is-there-no-scroll-function-library/39296/6 "2022-10-20T23:42:36Z")

</div>

A frame is a window. A JFrame is a Swing window. The canvas was removed from the Processing frame in the fifth line of setup() so that you won’t be bothered by the default postage stamp sized canvas. If you want it back for some reason then don’t remove it. If you change an item in the table and want to keep the change then you’ll probably need to update the data array. I don’t have code for this at the present time, but likely can be achieved by looking at the java docs.
