@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.
-
I want to put a ‘Jframe’ canvas inside the processing canvas.
-
When you change a value in the table, you want to get feedback on it.