SWT TableColumn does not work? - java

Can someone explain what is wrong with my code ?
I want to create a simple swt table.
Eclipse notice that TableColumn is undefined:
TableColumn column = new TableColumn(table, SWT.NONE);
Here is the complete Code:
Shell shell = new Shell();
shell.setSize(280, 300);
shell.setText("Testtabelle");
Table table = new Table(shell, SWT.MULTI | SWT.BORDER
| SWT.FULL_SELECTION);
table.setLinesVisible(true);
table.setHeaderVisible(true);
String[] titles = { " ", "C", "!", "Description", "Resource", "In Folder", "Location" };
for (int i = 0; i < titles.length; i++) {
TableColumn column = new TableColumn(table, SWT.NONE);
column.setText(titles[i]);
}
int count = 128;
for (int i = 0; i < count; i++) {
TableItem item = new TableItem(table, SWT.NONE);
item.setText(0, "x");
item.setText(1, "y");
item.setText(2, "!");
item.setText(3, "this stuff behaves the way I expect");
item.setText(4, "almost everywhere");
item.setText(5, "some.folder");
item.setText(6, "line " + i + " in nowhere");
}
for (int i = 0; i < titles.length; i++) {
table.getColumn(i).pack();
}
table.setSize(table.computeSize(SWT.DEFAULT, 200));
shell.pack();
shell.open();

Make sure that you import the correct TableColumn. In your case, this will be:
org.eclipse.swt.widgets.TableColumn
Also make sure that you don't import any other TableColumn if you don't need it. A popular example would be:
javax.swing.table.TableColumn

Related

JavaSwing add multiple JTable to JFrame

I am working on java swing application.I have to browse multiple CSV data files and display them into JFrame using JTable.I am trying to add multiple(non static) JTable into JFrame.All (N) JTable i want to include inside a JSrollPane.At this moment i can display only 1 table to JScrollPane.
Thanks in advance!
if (e.getSource() == jbtreadbrowsefile) {
try {
for(int k=0;k<file_locations.length;k++) {
String currentLocation=file_locations[k];
inputList = new ArrayList();
File inputF = new File(currentLocation);
InputStream inputFS = new FileInputStream(inputF);
BufferedReader br = new BufferedReader(new InputStreamReader(inputFS));
inputList = br.lines().collect(Collectors.toList());
br.close();
int count = inputList.size();
if(count>0)
{
data = new String[count - 1][8];
for (int i = 0; i < count - 1; i++) {
String[] arrOfStr = inputList.get(i + 1).toString().split(",");
String test1= arrOfStr[0];
String test2= arrOfStr[1];
String test3= arrOfStr[2];
String test4 = arrOfStr[3];
String test5= arrOfStr[4];
String test6= arrOfStr[5];
String test7= arrOfStr[6];
data[i][0] = "" + (i + 1);
data[i][1] = test1;
data[i][2] = test2;
data[i][3] = test3;
data[i][4] = test4;
data[i][5] = test5;
data[i][6] = test6;
data[i][7] = test7;
}
j = new JTable(data, columnNames);
TableColumnModel tcm = j.getColumnModel();
tcm.getColumn(0).setPreferredWidth(40);
tcm.getColumn(1).setPreferredWidth(220);
tcm.getColumn(2).setPreferredWidth(120);
tcm.getColumn(3).setPreferredWidth(80);
tcm.getColumn(4).setPreferredWidth(80);
tcm.getColumn(5).setPreferredWidth(80);
tcm.getColumn(6).setPreferredWidth(80);
tcm.getColumn(7).setPreferredWidth(80);
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment(JLabel.CENTER);
j.getColumnModel().getColumn(0).setCellRenderer(centerRenderer);
for (int i = 0; i < j.getRowCount(); i++) {
j.setRowHeight(i, 20);
}
j.getTableHeader().setFont(new Font("SansSerif", Font.BOLD, 15));
j.getTableHeader().setReorderingAllowed(false);
j.repaint();
JScrollPane sp = new JScrollPane(j);
sp.setBounds(10, 200, 780, 440);
add(sp);
}
else
{
JOptionPane.showMessageDialog(this,"No Data Found in the File");
}
}
JScrollPane sp = new JScrollPane(j);
sp.setBounds(10, 200, 780, 440);
add(sp);
Don't use setBounds(). It is the job of the layout manager to determine the size/location of a component.
At this moment i can display only 1 table to JScrollPane.
Correct. A JScrollPane is designed to display a single JTable. The header of the JTable will be displayed at the header view of the scroll pane and the table will be displayed in the center of the scroll pane. You may then also see scrollbars in the scroll pane depending on the data in the table.
If you want to "merge" data from multiple files into the same table, then you need to update the TableModel with the data from each file instead of creating a new table each time.
You can dynamically add data to the model by using the addRow(…) method of the DefaultTableModel.
For displaying each JTable into JFrame,i added all JTable into a JPanel and added this JPanel into JScrollPane.After displaying each JTable the problem was that we can not display each JTable header.I added to another JPanel each JTable header and it's shows up.
if (e.getSource() == jbtreadbrowsefile) {
GridLayout grid = new GridLayout(0, 1, 30, 20);
jpaneltable=new JPanel(grid);
try {
for(int k=0;k<file_locations.length;k++) {
String currentLocation=file_locations[k];
inputList = new ArrayList();
File inputF = new File(currentLocation);
InputStream inputFS = new FileInputStream(inputF);
BufferedReader br = new BufferedReader(new InputStreamReader(inputFS));
inputList = br.lines().collect(Collectors.toList());
br.close();
int count = inputList.size();
if(count>0)
{
data = new String[count - 1][8];
for (int i = 0; i < count - 1; i++) {
String[] arrOfStr = inputList.get(i + 1).toString().split(",");
String test1= arrOfStr[0];
String test2= arrOfStr[1];
String test3= arrOfStr[2];
String test4= arrOfStr[3];
String test5= arrOfStr[4];
String test6= arrOfStr[5];
String test7= arrOfStr[6];
data[i][0] = "" + (i + 1);
data[i][1] = test1;
data[i][2] = test2;
data[i][3] = test3;
data[i][4] = test4;
data[i][5] = test5;
data[i][6] = test6;
data[i][7] = test7;
}
j = new JTable(data, columnNames);
TableColumnModel tcm = j.getColumnModel();
tcm.getColumn(0).setPreferredWidth(40);
tcm.getColumn(1).setPreferredWidth(220);
tcm.getColumn(2).setPreferredWidth(120);
tcm.getColumn(3).setPreferredWidth(80);
tcm.getColumn(4).setPreferredWidth(80);
tcm.getColumn(5).setPreferredWidth(80);
tcm.getColumn(6).setPreferredWidth(80);
tcm.getColumn(7).setPreferredWidth(80);
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment(JLabel.CENTER);
j.getColumnModel().getColumn(0).setCellRenderer(centerRenderer);
for (int i = 0; i < j.getRowCount(); i++) {
j.setRowHeight(i, 20);
}
j.getTableHeader().setFont(new Font("SansSerif", Font.BOLD, 15));
j.getTableHeader().setReorderingAllowed(false);
j.getTableHeader().setPreferredSize(new Dimension(0,HEADER_HEIGHT));
j.repaint();
jpaneltable.add(j.getTableHeader(),BorderLayout.NORTH);
jpaneltable.add(j, BorderLayout.CENTER);
}
else
{
JOptionPane.showMessageDialog(this,"No Data Found in the File");
}
}
JScrollPane sp = new JScrollPane(jpaneltable,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
sp.setBounds(10, 200, 780, 440);
add(sp);
}

How to change content of a cell in a Table when clicking on that cell

I have a Table on Java SWT. I have a "payed" column and all the items have "non payed" written and a icon of non payed. It's the fourth column of the table.
I have populated the table with dummy content, Now, I want to add a listener on the cells of the fourth column "payed" to change the content of the clicked cell. If the cell has non payed text and icon, must change to payed text and icon, and the opposite behaviour.
I can't find the way to achieve that, because I can't add a selection listener to a cell of the table, or I don't know how to do it.
This is my source code:
Table membersTable = new Table(clubComposite, SWT.BORDER | SWT.CHECK | SWT.FULL_SELECTION);
membersTable.setLinesVisible(true);
membersTable.setHeaderVisible(true);
membersTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
TableColumn tblclmnName = new TableColumn(membersTable, SWT.NONE);
tblclmnName.setWidth(150);
tblclmnName.setText("Nombre");
TableColumn tblclmnCommonPhoneNumber = new TableColumn(membersTable, SWT.NONE);
tblclmnCommonPhoneNumber.setWidth(120);
tblclmnCommonPhoneNumber.setText("Teléfono");
TableColumn tblclmnCommonMoney = new TableColumn(membersTable, SWT.NONE);
tblclmnCommonMoney.setWidth(150);
tblclmnCommonMoney.setText("Participación Habitual");
TableColumn tblclmnPayed = new TableColumn(membersTable, SWT.CENTER);
tblclmnPayed.setWidth(50);
tblclmnPayed.setText("Payed");
// populate Table
for (int i=0; i<50; i++) {
TableItem tableItem = new TableItem(membersTable, SWT.CENTER);
tableItem.setText(new String[] {"person "+i, "610610620", "100", "non payed"});
tableItem.setImage(3, uncheckedImage);
}
EDIT:
OK i managed to do it with a checkbox, but now how can i know in which row is clicked the checkbox? I need it to get the value of the third column
// populate Table
for (int i=0; i<50; i++) {
TableItem tableItem = new TableItem(membersTable, SWT.NONE);
tableItem.setText(new String[] {"person "+i, "610610620", "100"});
Button button = new Button(membersTable, SWT.CHECK);
button.pack();
TableEditor editor = new TableEditor(membersTable);
editor.minimumWidth = button.getSize().x;
editor.horizontalAlignment = SWT.CENTER;
editor.setEditor(button, tableItem, 3);
button.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
//how to know in which row is clicked the checkbox?
}
});
}
Since you're inside the for-loop creating the TableItem, you already have the index of the row. Similarly, if you need the actual TableItem, you already have access to that as well.
for (int i=0; i<50; i++) {
TableItem tableItem = new TableItem(membersTable, SWT.NONE);
tableItem.setText(new String[] {"person "+i, "610610620", "100"});
Button button = new Button(membersTable, SWT.CHECK);
button.pack();
TableEditor editor = new TableEditor(membersTable);
editor.minimumWidth = button.getSize().x;
editor.horizontalAlignment = SWT.CENTER;
editor.setEditor(button, tableItem, 3);
final int index = i; // Hold the row index here
button.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
//how to know in which row is clicked the checkbox?
System.out.println("Row selected: " + index);
System.out.println("Participación Habitual: " + tableItem.getText(2));
}
});
}
Edit:
As mentioned in the comments, using index will be unreliable if a row is deleted. Instead you can use Table.indexOf(TableItem) if you need the numerical index.
#Override
public void widgetSelected(SelectionEvent e) {
//how to know in which row is clicked the checkbox?
System.out.println("Row selected: " + membersTable.indexOf(tableItem));
System.out.println("Participación Habitual: " + tableItem.getText(2));
}

How do you add buttons to a table in SWT?

To clarify, I'm just using SWT - not JFace (trying to learn one GUI library at a time, though perhaps this is not wise). Adding buttons to a table (java swt) gives an example for a TreeEditor, which I'm not using currently.
The problem I'm having is that I can't seem to get a button to show up in row 0, column 0, for the life of me:
One thing I noticed during my debugging is that the check-buttons are shifted down by one; the first visible button in row 1 actually corresponds to item 0.
The code below is in Scala, though it is sufficiently basic that it shouldn't prove difficult to understand for anyone familiar with Java and SWT; I'd happily accept an answer in Java.
package com.bar.baz
import org.eclipse.swt.widgets._
import org.eclipse.swt.custom.TableEditor
import org.eclipse.swt.layout.FillLayout
import org.eclipse.swt.SWT
object TableTest {
/**
* Launch the application.
*
* #param args
*/
def main(args: Array[String]) = {
try {
val nrows = 5
val ncols = 3
val display = new Display ()
val shell = new Shell (display)
shell.setLayout(new FillLayout())
val table: Table = new Table(
shell, SWT.BORDER /*| SWT.CHECK */| SWT.FULL_SELECTION | SWT.SINGLE
)
val editor: TableEditor = new TableEditor(table)
editor.horizontalAlignment = SWT.LEFT
editor.grabHorizontal = true
table.setLinesVisible(true)
table.setHeaderVisible(true)
for (ii <- 0 until ncols) {
val column: TableColumn = new TableColumn(table, SWT.NONE)
column.setText("Col " + ii.toString)
column.setWidth(40)
}
for (row <- 0 until nrows; col <- 0 until ncols) {
if (col == 0) {
new TableItem(table, SWT.NONE)
createDeleteRowButton(row, col)
}
else {
val item = table.getItem(row)
item.setText(col, s"$row, $col")
}
}
for (col <- 0 until ncols) {
table.getColumn(col).pack()
}
shell.pack ()
shell.open ()
while (!shell.isDisposed) {
if (!display.readAndDispatch ()) display.sleep ()
}
display.dispose ()
def createDeleteRowButton(row: Int, col: Int): Button = {
val delButton = new Button(table, SWT.CHECK)
val item = table.getItem(row)
editor.setEditor(delButton, item, col)
delButton
}
}
catch {
case e: Exception =>
e.printStackTrace()
}
}
}
Edit: Some additional info that I should have included. In my actual application, I found that the items wrap around in a very strange way:
I had to disable the period column so that I could see this.
Edit 2: Added Java code
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.SWT;
public class TableTestJava {
public static void main (String[] args) {
Integer nrows = 5;
Integer ncols = 3;
Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout(new FillLayout());
Table table = new Table(
shell, SWT.BORDER /*| SWT.CHECK */| SWT.FULL_SELECTION | SWT.SINGLE
);
TableEditor editor = new TableEditor(table);
editor.horizontalAlignment = SWT.LEFT;
editor.grabHorizontal = true;
table.setLinesVisible(true);
table.setHeaderVisible(true);
for (int ii = 0; ii < ncols; ii++) {
TableColumn column = new TableColumn(table, SWT.NONE);
column.setText("Col " + Integer.toString(ii));
column.setWidth(40);
}
//for (row <- 0 until nrows) {;}
for (int row = 0; row < nrows; row ++) {
for (int col = 0; col < ncols; col ++) {
if (col == 0) {
new TableItem(table, SWT.NONE);
//Inlined function: createDeleteRowButton
Button delButton = new Button(table, SWT.CHECK);
TableItem item = table.getItem(row);
editor.setEditor(delButton, item, col);
//End of inlined function
} else {
TableItem item = table.getItem(row);
item.setText(col, Integer.toString(row) + ", " + Integer.toString(col));
}
}
}
shell.pack ();
shell.open ();
while (!shell.isDisposed()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
I'm posting this as an answer, though it isn't ideal, as it adds an empty row and requires a lot of finagling in the real application with indices ... but it works, except the buttons do not move when the rest of the table scrolls vertically. This is the "shift everything down by one" solution.
Note that it requires creating table items ahead of time in a separate loop.
import org.eclipse.swt.widgets._
import org.eclipse.swt.custom.TableEditor
import org.eclipse.swt.layout.FillLayout
import org.eclipse.swt.SWT
object TableTest {
def main(args: Array[String]) = {
try {
val nrows = 5
val ncols = 3
val display = new Display ()
val shell = new Shell (display)
shell.setLayout(new FillLayout())
val table: Table = new Table(
shell, SWT.BORDER /*| SWT.CHECK */| SWT.FULL_SELECTION | SWT.SINGLE
)
val editor: TableEditor = new TableEditor(table)
editor.horizontalAlignment = SWT.LEFT
editor.grabHorizontal = true
table.setLinesVisible(true)
table.setHeaderVisible(true)
for (ii <- 0 until ncols) {
val column: TableColumn = new TableColumn(table, SWT.NONE)
column.setText("Col " + ii.toString)
column.setWidth(40)
}
for (row <- 0 until nrows) {new TableItem(table, SWT.NONE)}
for (row <- 0 until nrows; col <- 0 until ncols) {
if (col == 0) {
createDeleteRowButton(row, col)
}
else if (row < nrows - 1) {
val item = table.getItem(row+1)
item.setText(col, s"$row, $col")
}
}
shell.pack ()
shell.open ()
while (!shell.isDisposed) {
if (!display.readAndDispatch ()) display.sleep ()
}
display.dispose ()
def createDeleteRowButton(row: Int, col: Int): Button = {
val delButton = new Button(table, SWT.CHECK)
val item = table.getItem(row)
editor.setEditor(delButton, item, col)
delButton
}
}
catch {
case e: Exception =>
e.printStackTrace()
}
}
}
It looks like this:

SWT Table don't see items from other columns

I added columns on code:
String[] titles = {"Nazwa", "Uzytkownik", "Haslo"};
for(int i=0; i<titles.length; i++){
TableColumn column = new TableColumn(table, SWT.NULL);
column.setText(titles[i]);
}
Next, I added items to said columns:
String[][] a = new String[passwords.length][3];
for(int i=0; i<passwords.length; i++){
a[i] = passwords[i].split(":");
}
for(int i=0; i<passwords.length; i++){
TableItem item = new TableItem(table, SWT.NULL);
//item.setText(Integer.toString(i));
for(int j=0; j<a[i].length; j++){
item.setText(j, a[i][j]);
}
}
Now, I want get selected items from the second column.
I added TableCursor and KeyListener:
tableCursor.addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent e) {
if(e.character == 'c') {
Point p = tableCursor.getLocation();
//for(int i=0; i<selection.length; i++){
TableItem item = table.getItem(p);
if(item != null){
MessageBox message = new MessageBox(shell, SWT.ICON_ERROR);
message.setMessage(item.toString());
message.open();
}
}
}
});
And now, for example, when I select item from the second column, it doesn't see anything from the second column, only item from the first column. What's the problem?
The problem is the way you try to find the selected item/column in the table.
Here is an example that does exactly what you want:
public static void main(String[] args)
{
Display display = Display.getDefault();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Table table = new Table(shell, SWT.NONE);
table.setHeaderVisible(true);
/* Create columns */
for (int i = 0; i < 3; i++)
{
TableColumn col = new TableColumn(table, SWT.NONE);
col.setText("Col " + i);
}
/* Create items */
for (int i = 0; i < 10; i++)
{
TableItem item = new TableItem(table, SWT.NONE);
item.setText(new String[] { "Row" + i + "Col1", "Row" + i + "Col2", "Row" + i + "Col3" });
}
/* Pack columns */
for (int i = 0; i < 3; i++)
{
table.getColumn(i).pack();
}
table.addListener(SWT.MouseUp, new Listener()
{
#Override
public void handleEvent(Event event)
{
Point pt = new Point(event.x, event.y);
TableItem item = table.getItem(pt);
if (item != null)
{
/* Iterate over all columns and check if event is contained */
for (int col = 0; col < table.getColumnCount(); col++)
{
Rectangle rect = item.getBounds(col);
if (rect.contains(pt))
{
System.out.println(item.getText(col));
}
}
}
}
});
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}

Scroll a JScrollPane to a specific row on a JTable [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
JTable Scrolling to a specified row index
I have a JTable and I programmatically need to select a row by using this code:
myTable.setRowSelectionInterval(i, j);
(where i and j are valid row and column numbers respectively).
The problem is, when you jump to a row, the JScrollPane does not move.
In this case, the table is quite long, and often the "selected row" is not visible on the screen, so the user has to stay scrolling up/down manually to find it. I would like to know how I can make the JScrollPane automatically jump to the specific location of the row.
Edit: Found this one liner which can do it:
table.scrollRectToVisible(table.getCellRect(row,0, true));
just extends post by #Eng.Fouad +1, no works as I exactly expected (with kind help by StanislavL from another Java Swing forum)
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.DefaultTableModel;
public class TableSelectionGood implements ListSelectionListener {
private JTable[] tables;
private boolean ignore = false;
public TableSelectionGood() {
Object[][] data1 = new Object[100][5];
Object[][] data2 = new Object[50][5];
Object[][] data3 = new Object[50][5];
for (int i = 0; i < data1.length; i++) {
data1[i][0] = "Company # " + (i + 1);
for (int j = 1; j < data1[i].length; j++) {
data1[i][j] = "" + (i + 1) + ", " + j;
}
}
for (int i = 0; i < data2.length; i++) {
data2[i][0] = "Company # " + ((i * 2) + 1);
for (int j = 1; j < data2[i].length; j++) {
data2[i][j] = "" + ((i * 2) + 1) + ", " + j;
}
}
for (int i = 0; i < data3.length; i++) {
data3[i][0] = "Company # " + (i * 2);
for (int j = 1; j < data3[i].length; j++) {
data3[i][j] = "" + (i * 2) + ", " + j;
}
}
String[] headers = {"Col 1", "Col 2", "Col 3", "Col 4", "Col 5"};
DefaultTableModel model1 = new DefaultTableModel(data1, headers);
DefaultTableModel model2 = new DefaultTableModel(data2, headers);
DefaultTableModel model3 = new DefaultTableModel(data3, headers);
final JTable jTable1 = new JTable(model1);
jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
final JScrollPane sp1 = new JScrollPane();
sp1.setPreferredSize(new Dimension(600, 200));
sp1.setViewportView(jTable1);
final JTable jTable2 = new JTable(model2);
jTable2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
final JScrollPane sp2 = new JScrollPane();
sp2.setPreferredSize(new Dimension(600, 200));
sp2.setViewportView(jTable2);
final JTable jTable3 = new JTable(model3);
jTable3.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
final JScrollPane sp3 = new JScrollPane();
sp3.setPreferredSize(new Dimension(600, 200));
sp3.setViewportView(jTable3);
TableSelectionGood tableSelection = new TableSelectionGood(jTable1, jTable2, jTable3);
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(3, 0, 10, 10));
panel1.add(sp1);
panel1.add(sp2);
panel1.add(sp3);
JFrame frame = new JFrame("tableSelection");
frame.add(panel1);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public TableSelectionGood(JTable... tables) {
for (JTable table : tables) {
table.getSelectionModel().addListSelectionListener(this);
}
this.tables = tables;
}
private JTable getTable(Object model) {
for (JTable table : tables) {
if (table.getSelectionModel() == model) {
return table;
}
}
return null;
}
private void changeSelection(JTable table, String rowKey) {
int col = table.convertColumnIndexToView(0);
for (int row = table.getRowCount(); --row >= 0;) {
if (rowKey.equals(table.getValueAt(row, col))) {
table.changeSelection(row, col, false, false);
return;
}
}
table.clearSelection();
}
#Override
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() || ignore) {
return;
}
ignore = true;
try {
JTable table = getTable(e.getSource());
int row = table.getSelectedRow();
String rowKey = table.getValueAt(row, table.convertColumnIndexToView(0)).toString();
for (JTable t : tables) {
if (t == table) {
continue;
}
changeSelection(t, rowKey);
JViewport viewport = (JViewport) t.getParent();
Rectangle rect = t.getCellRect(t.getSelectedRow(), 0, true);
Rectangle r2 = viewport.getVisibleRect();
t.scrollRectToVisible(new Rectangle(rect.x, rect.y, (int) r2.getWidth(), (int) r2.getHeight()));
System.out.println(new Rectangle(viewport.getExtentSize()).contains(rect));
}
} finally {
ignore = false;
}
}
public static void main(String[] args) {
TableSelectionGood tableSelection = new TableSelectionGood();
}
}
I used this in my old project:
public static void scrollToVisible(JTable table, int rowIndex, int vColIndex)
{
if (!(table.getParent() instanceof JViewport)) return;
JViewport viewport = (JViewport)table.getParent();
Rectangle rect = table.getCellRect(rowIndex, vColIndex, true);
Point pt = viewport.getViewPosition();
rect.setLocation(rect.x-pt.x, rect.y-pt.y);
viewport.scrollRectToVisible(rect);
}
Reference: http://www.exampledepot.com/egs/javax.swing.table/Vis.html

Categories

Resources