Java by API/javax.swing.event/TableModelEvent — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 14:33, 31 мая 2010
Содержание
TableModelEvent.ALL_COLUMNS
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
public class Main {
public static void main(String[] argv) throws Exception {
JTable table = new JTable();
table.getModel().addTableModelListener(new MyTableModelListener(table));
}
}
class MyTableModelListener implements TableModelListener {
JTable table;
MyTableModelListener(JTable table) {
this.table = table;
}
public void tableChanged(TableModelEvent e) {
int firstRow = e.getFirstRow();
int lastRow = e.getLastRow();
int index = e.getColumn();
switch (e.getType()) {
case TableModelEvent.INSERT:
for (int i = firstRow; i <= lastRow; i++) {
System.out.println(i);
}
break;
case TableModelEvent.UPDATE:
if (firstRow == TableModelEvent.HEADER_ROW) {
if (index == TableModelEvent.ALL_COLUMNS) {
System.out.println("A column was added");
} else {
System.out.println(index + "in header changed");
}
} else {
for (int i = firstRow; i <= lastRow; i++) {
if (index == TableModelEvent.ALL_COLUMNS) {
System.out.println("All columns have changed");
} else {
System.out.println(index);
}
}
}
break;
case TableModelEvent.DELETE:
for (int i = firstRow; i <= lastRow; i++) {
System.out.println(i);
}
break;
}
}
}
TableModelEvent.DELETE
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
public class Main {
public static void main(String[] argv) throws Exception {
JTable table = new JTable();
table.getModel().addTableModelListener(new MyTableModelListener(table));
}
}
class MyTableModelListener implements TableModelListener {
JTable table;
MyTableModelListener(JTable table) {
this.table = table;
}
public void tableChanged(TableModelEvent e) {
int firstRow = e.getFirstRow();
int lastRow = e.getLastRow();
int index = e.getColumn();
switch (e.getType()) {
case TableModelEvent.INSERT:
for (int i = firstRow; i <= lastRow; i++) {
System.out.println(i);
}
break;
case TableModelEvent.UPDATE:
if (firstRow == TableModelEvent.HEADER_ROW) {
if (index == TableModelEvent.ALL_COLUMNS) {
System.out.println("A column was added");
} else {
System.out.println(index + "in header changed");
}
} else {
for (int i = firstRow; i <= lastRow; i++) {
if (index == TableModelEvent.ALL_COLUMNS) {
System.out.println("All columns have changed");
} else {
System.out.println(index);
}
}
}
break;
case TableModelEvent.DELETE:
for (int i = firstRow; i <= lastRow; i++) {
System.out.println(i);
}
break;
}
}
}
TableModelEvent.INSERT
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
public class Main {
public static void main(String[] argv) throws Exception {
JTable table = new JTable();
table.getModel().addTableModelListener(new MyTableModelListener(table));
}
}
class MyTableModelListener implements TableModelListener {
JTable table;
MyTableModelListener(JTable table) {
this.table = table;
}
public void tableChanged(TableModelEvent e) {
int firstRow = e.getFirstRow();
int lastRow = e.getLastRow();
int index = e.getColumn();
switch (e.getType()) {
case TableModelEvent.INSERT:
for (int i = firstRow; i <= lastRow; i++) {
System.out.println(i);
}
break;
case TableModelEvent.UPDATE:
if (firstRow == TableModelEvent.HEADER_ROW) {
if (index == TableModelEvent.ALL_COLUMNS) {
System.out.println("A column was added");
} else {
System.out.println(index + "in header changed");
}
} else {
for (int i = firstRow; i <= lastRow; i++) {
if (index == TableModelEvent.ALL_COLUMNS) {
System.out.println("All columns have changed");
} else {
System.out.println(index);
}
}
}
break;
case TableModelEvent.DELETE:
for (int i = firstRow; i <= lastRow; i++) {
System.out.println(i);
}
break;
}
}
}
TableModelEvent.UPDATE
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
public class Main {
public static void main(String[] argv) throws Exception {
JTable table = new JTable();
table.getModel().addTableModelListener(new MyTableModelListener(table));
}
}
class MyTableModelListener implements TableModelListener {
JTable table;
MyTableModelListener(JTable table) {
this.table = table;
}
public void tableChanged(TableModelEvent e) {
int firstRow = e.getFirstRow();
int lastRow = e.getLastRow();
int index = e.getColumn();
switch (e.getType()) {
case TableModelEvent.INSERT:
for (int i = firstRow; i <= lastRow; i++) {
System.out.println(i);
}
break;
case TableModelEvent.UPDATE:
if (firstRow == TableModelEvent.HEADER_ROW) {
if (index == TableModelEvent.ALL_COLUMNS) {
System.out.println("A column was added");
} else {
System.out.println(index + "in header changed");
}
} else {
for (int i = firstRow; i <= lastRow; i++) {
if (index == TableModelEvent.ALL_COLUMNS) {
System.out.println("All columns have changed");
} else {
System.out.println(index);
}
}
}
break;
case TableModelEvent.DELETE:
for (int i = firstRow; i <= lastRow; i++) {
System.out.println(i);
}
break;
}
}
}