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

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

hello. Thanks for clicking on my article.

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
    Scrollbar / Examples / Processing.org
  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.

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

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
    }
  }
  );
}

2 Likes

@svan

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

  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?

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

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

  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);
    }
  }
  );
1 Like

@svan

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
    }
  }
  );
  
}

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.

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.

1 Like