![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
This TableView connects to live data via a JDBCConnection and enables the user to interact with the data.
Here are the steps for creating a simple TableView:
The resulting code would look something like this:
TableViewtableView1 = new TableView();
JDBCToTableModelAdapter1 = new JDBCToTableModelAdapter();
...
Initialize your adapter here
...
tableView1.setModel( JDBCToTableModelAdapter1 );
JScrollPane1 = new JScrollPane( tableView1 );
contentPane.add(JScrollPane1);
|
The code shown on the Full Code tab will create a JDBCToTableModelAdapter and display a default TableView with that model.
This sample shows the following customizations:
Set up mouse handlers and execute the following code when the left mouse button is clicked:
if (javax.swing.SwingUtilities.isLeftMouseButton(event) && pressOk)
{
int col = tableView1.convertColumnIndexToModel(
tableView1.getTableHeader().columnAtPoint(event.getPoint()));
String columnName = com.sas.swing.visuals.tableview.TableViewUtil.getColumnName(
JDBCToTableModelAdapter1, col);
if (JDBCToTableModelAdapter1.isSortAllowed()
&& JDBCToTableModelAdapter1.isColumnSortable(columnName))
{
String[] columns = JDBCToTableModelAdapter1.getSortedColumns();
int[] directions = JDBCToTableModelAdapter1.getSortedDirections();
int direction = JDBCToTableModelAdapter1.ASCENDING;
if (columnName != null && columns != null && directions != null
&& columns.length == directions.length)
{
for (int i = 0; i < columns.length; i++)
{
if (columnName.equals(columns[i]))
{
if (directions[i] == JDBCToTableModelAdapter1.ASCENDING)
direction = JDBCToTableModelAdapter1.DESCENDING;
break;
}
}
}
java.util.List newColumns = new java.util.ArrayList();
java.util.List newDirections = new java.util.ArrayList();
newColumns.add(columnName);
newDirections.add(new Integer(direction));
for (int i = 0; i < columns.length; i++)
{
if (columns[i] != null && !columns[i].equals(columnName))
{
newColumns.add(columns[i]);
newDirections.add(new Integer(directions[i]));
}
}
int size = newDirections.size();
int[] ndirections = new int[size];
for (int i = 0; i < size; i++)
ndirections[i] = ((Integer)newDirections.get(i)).intValue();
try
{
if (tableView1.isEditing())
tableView1.getCellEditor().cancelCellEditing();
JDBCToTableModelAdapter1.sort((String[])newColumns.toArray(new String[0]),
ndirections);
}
catch (com.sas.table.SortException ex)
{
ex.printStackTrace();
}
}
}
|
tableView1.getRowModel().setRowMargin(2);
tableView1.getColumnModel().setColumnMargin(0);
tableView1.setShowVerticalLines(false);
tableView1.setGridWidth(2);
tableView1.setGridColor(java.awt.Color.darkGray);
|
tableView1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter()
{
public void mouseMoved(java.awt.event.MouseEvent event)
{
java.awt.Point p = event.getPoint();
// Locate the cell under the event location
int hitColumnIndex = tableView1.columnAtPoint(p);
int hitRowIndex = tableView1.rowAtPoint(p);
if (hitColumnIndex != -1 && hitRowIndex != -1)
{
Object obj = JDBCToTableModelAdapter1.getUnformattedValueAt(
tableView1.convertRowIndexToModel(hitRowIndex),
tableView1.convertColumnIndexToModel(hitColumnIndex));
String tip = null;
if (obj != null)
tip = obj.toString();
tableView1.setToolTipText(tip);
}
}
});
|
com.sas.swing.visuals.tableview.DefaultTableCellEditor textEditor =
new com.sas.swing.visuals.tableview.DefaultTableCellEditor()
{
public java.awt.Component getTableCellEditorComponent(javax.swing.JTable table, Object value,
boolean isSelected, int row, int column)
{
String format = JDBCToTableModelAdapter1.getFormat(tableView1.convertColumnIndexToModel(column));
if (format != null && format.trim().length() != 0)
setTransform(new com.sas.util.transforms.FormatTransform(
com.sas.text.SASFormat.getInstance(format)));
else
setTransform(null);
return super.getTableCellEditorComponent(table, value, isSelected, row, column);
}
};
javax.swing.JComboBox comboBox = new javax.swing.JComboBox();
comboBox.setEditable(true);
com.sas.swing.visuals.tableview.DefaultTableCellEditor comboEditor =
new com.sas.swing.visuals.tableview.DefaultTableCellEditor(comboBox)
{
public java.awt.Component getTableCellEditorComponent(javax.swing.JTable table, Object value,
boolean isSelected, int row, int column)
{
javax.swing.JComboBox combo = (javax.swing.JComboBox)super.getTableCellEditorComponent(table,
value, isSelected, row, column);
String columnName = com.sas.swing.visuals.tableview.TableViewUtil.getColumnName(
JDBCToTableModelAdapter1, tableView1.convertColumnIndexToModel(column));
com.sas.storage.jdbc.JDBCToListModelAdapter adapter =
new com.sas.storage.jdbc.JDBCToListModelAdapter(JDBCConnection1,
"select distinct " + columnName + " from sashelp.houses");
adapter.setTrimUsed(true);
Object[] objArray = new Object[adapter.getSize()];
for (int i = 0; i < objArray.length; i++)
objArray[i] = adapter.getElementAt(i);
javax.swing.DefaultComboBoxModel comboModel = new javax.swing.DefaultComboBoxModel(objArray);
combo.setModel(comboModel);
combo.setSelectedItem(value);
return combo;
}
};
for (int i = 0, colCount = tableView1.getColumnCount(); i < colCount; i++)
{
if (tableView1.getUnformattedColumnClass(i) == String.class)
{
tableView1.getColumnModel().getColumn(i).setCellEditor(comboEditor);
}
else
{
tableView1.getColumnModel().getColumn(i).setCellEditor(textEditor);
}
}
|
tableView1.populateToolBar(JToolBar1);
JToolBar1.revalidate();
JToolBar1.repaint();
|
java.util.Map rendererMap = new java.util.HashMap();
for (int i = 0, colCount = tableView1.getColumnCount(); i < colCount; i++)
{
Class cls = tableView1.getUnformattedColumnClass(i);
int col = tableView1.convertColumnIndexToModel(i) + 1;
javax.swing.table.TableCellRenderer renderer = tableView1.getDefaultRenderer(cls);
if (!rendererMap.containsKey(cls))
{
try
{
renderer = (javax.swing.table.TableCellRenderer)renderer.getClass().newInstance();
if (renderer instanceof javax.swing.JComponent)
((javax.swing.JComponent)renderer).setBackground(java.awt.SystemColor.controlHighlight);
rendererMap.put(cls, renderer);
}
catch (InstantiationException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
}
else
{
renderer = (javax.swing.table.TableCellRenderer)rendererMap.get(cls);
} //applies the renderer to the given column for every other row
tableView1.setCellRenderer(1, -1, 2, col, col, 1, renderer);
}
|
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
Tip: For help with building a SAS Java Project, see SAS Note 32218.
data sashelp.houses(label = 'Residential housing for sale' );
input style $ 1-8 sqfeet 10-13 bedrooms 15
baths 17-19 street $ 21-36 price 40-45;
label style = "Style of homes"
sqfeet = "Square footage"
bedrooms = "Number of bedrooms"
baths = "Number of bathrooms"
street = "Street address"
price = "Asking price";
format price dollar12.;
informat price comma12.;
cards;
RANCH 1250 2 1 Sheppard Avenue 64000
SPLIT 1190 1 1 Rand Street 65850
CONDO 1400 2 1.5 Market Street 80050
TWOSTORY 1810 4 3 Garris Street 107250
RANCH 1500 3 3 Kemble Avenue 86650
SPLIT 1615 4 3 West Drive 94450
SPLIT 1305 3 1.5 Graham Avenue 73650
CONDO 1390 3 2.5 Hampshire Avenue 79350
TWOSTORY 1040 2 1 Sanders Road 55850
CONDO 2105 4 2.5 Jeans Avenue 127150
RANCH 1535 3 3 State Highway 89100
TWOSTORY 1240 2 1 Fairbanks Circle 69250
RANCH 720 1 1 Nicholson Drive 34550
TWOSTORY 1745 4 2.5 Highland Road 102950
CONDO 1860 2 2 Arcata Avenue 110700
run;
|
private static final String JDBC_DRIVER_STRING = "jdbc:sasiom://machine:8591";
private static final String JDBC_USERNAME = "username";
private static final String JDBC_PASSWORD = "password";
package samples;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.event.MouseInputAdapter;
import javax.swing.table.TableCellRenderer;
import com.sas.storage.jdbc.JDBCConnection;
import com.sas.storage.jdbc.JDBCToListModelAdapter;
import com.sas.storage.jdbc.JDBCToTableModelAdapter;
import com.sas.swing.visuals.MenuBar;
import com.sas.swing.visuals.tableview.DefaultTableCellEditor;
import com.sas.swing.visuals.tableview.TableView;
import com.sas.swing.visuals.tableview.TableViewUtil;
import com.sas.text.SASFormat;
import com.sas.util.transforms.FormatTransform;
public class JDBCTableViewApp {
private static final String JDBC_DRIVER_STRING = "jdbc:sasiom://machine:8591";
private static final String JDBC_USERNAME = "username";
private static final String JDBC_PASSWORD = "password";
protected MenuBar menuBar1;
protected TableView tableView1;
protected JScrollPane JScrollPane1;
protected JDBCConnection JDBCConnection1;
protected JDBCToTableModelAdapter JDBCToTableModelAdapter1;
protected JToolBar JToolBar1;
protected TableView table;
public static void main(String[] args) {
// set swing look and feel with system look and feel
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
JDBCTableViewApp simpleApp = new JDBCTableViewApp();
simpleApp.createFrame(args).setVisible(true);
}
// create a frame and add the table view to it
public JFrame createFrame(String[] args) {
JFrame frame = new JFrame(this.getClass().getName());
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
// create a new empty TableView
tableView1 = new com.sas.swing.visuals.tableview.TableView();
// create a new adapter so we can use JDBC data in the TableView
JDBCToTableModelAdapter1 = new JDBCToTableModelAdapter();
// create the connection to connect to JDBC data
JDBCConnection1 = new JDBCConnection();
JDBCConnection1.setConnectionInfo(new java.util.Properties());
JDBCConnection1.setDatabaseURL(JDBC_DRIVER_STRING);
try {
JDBCConnection1.setDriverName("com.sas.rio.MVADriver");
} catch (Exception e) {
System.out.println(e.getMessage());
}
JDBCConnection1.setPassword(JDBC_PASSWORD);
JDBCConnection1.setUsername(JDBC_USERNAME);
// set up the adapter
JDBCToTableModelAdapter1.setModel(JDBCConnection1);
JDBCToTableModelAdapter1.setReadOnly(false);
JDBCToTableModelAdapter1.setTrimUsed(true);
JDBCToTableModelAdapter1.setFormattedDataUsed(true);
// set the query statement for the adapter - you can change this to a
// query of your choice
JDBCToTableModelAdapter1
.setQueryStatement("select * from sashelp.company");
// set the adapter just created as the model for the TableView
tableView1.setModel(JDBCToTableModelAdapter1);
// add the TableView to the scroll pane
JScrollPane1 = new JScrollPane(tableView1);
// add the scroll pane to the content pane
contentPane.add(JScrollPane1, BorderLayout.CENTER);
// add a toolbar above the TableView
// we will add a sort action to this toolbar below
JToolBar1 = new JToolBar();
JToolBar1.setRollover(true);
contentPane.add(JToolBar1, BorderLayout.NORTH);
contentPane.setSize(500, 250);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
// add customizations to the TableView
// this call can be removed if no customizations are desired
applyCustomizations();
return frame;
}
// applies the various customizations listed on the Details tab of the
// sample
// this method can be removed (as well as the call to it) if no
// customizations are desired
public void applyCustomizations() {
// Sort a column by clicking on a column header
MouseInputAdapter mouseAdapter = new MouseInputAdapter() {
boolean pressOk = false;
public void mouseDragged(java.awt.event.MouseEvent event) {
pressOk = false;
}
public void mousePressed(java.awt.event.MouseEvent event) {
if (tableView1.getColumnHeader().getCursor() == java.awt.Cursor
.getDefaultCursor())
pressOk = true;
else
pressOk = false;
}
public void mouseReleased(java.awt.event.MouseEvent event) {
if (javax.swing.SwingUtilities.isLeftMouseButton(event)
&& pressOk) {
int col = tableView1.convertColumnIndexToModel(tableView1
.getTableHeader().columnAtPoint(event.getPoint()));
String columnName = TableViewUtil.getColumnName(
JDBCToTableModelAdapter1, col);
if (JDBCToTableModelAdapter1.isSortAllowed()
&& JDBCToTableModelAdapter1
.isColumnSortable(columnName)) {
String[] columns = JDBCToTableModelAdapter1
.getSortedColumns();
int[] directions = JDBCToTableModelAdapter1
.getSortedDirections();
int direction = JDBCToTableModelAdapter1.ASCENDING;
if (columnName != null && columns != null
&& directions != null
&& columns.length == directions.length) {
for (int i = 0; i < columns.length; i++) {
if (columnName.equals(columns[i])) {
if (directions[i] == JDBCToTableModelAdapter1.ASCENDING)
direction = JDBCToTableModelAdapter1.DESCENDING;
break;
}
}
}
java.util.List newColumns = new java.util.ArrayList();
java.util.List newDirections = new java.util.ArrayList();
newColumns.add(columnName);
newDirections.add(new Integer(direction));
for (int i = 0; i < columns.length; i++) {
if (columns[i] != null
&& !columns[i].equals(columnName)) {
newColumns.add(columns[i]);
newDirections.add(new Integer(directions[i]));
}
}
int size = newDirections.size();
int[] ndirections = new int[size];
for (int i = 0; i < size; i++)
ndirections[i] = ((Integer) newDirections.get(i))
.intValue();
try {
if (tableView1.isEditing())
tableView1.getCellEditor().cancelCellEditing();
JDBCToTableModelAdapter1.sort((String[]) newColumns
.toArray(new String[0]), ndirections);
} catch (com.sas.table.SortException ex) {
ex.printStackTrace();
}
}
}
}
};
tableView1.getColumnHeader().addMouseMotionListener(mouseAdapter);
tableView1.getColumnHeader().addMouseListener(mouseAdapter);
// Modifying the grid lines
tableView1.getRowModel().setRowMargin(2);
tableView1.getColumnModel().setColumnMargin(0);
tableView1.setShowVerticalLines(false);
tableView1.setGridWidth(2);
tableView1.setGridColor(java.awt.Color.darkGray);
// Adding tooltips for each cell
tableView1.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent event) {
java.awt.Point p = event.getPoint();
// Locate the cell under the event location
int hitColumnIndex = tableView1.columnAtPoint(p);
int hitRowIndex = tableView1.rowAtPoint(p);
if (hitColumnIndex != -1 && hitRowIndex != -1) {
Object obj = JDBCToTableModelAdapter1
.getUnformattedValueAt(
tableView1
.convertRowIndexToModel(hitRowIndex),
tableView1
.convertColumnIndexToModel(hitColumnIndex));
String tip = null;
if (obj != null)
tip = obj.toString();
tableView1.setToolTipText(tip);
}
}
});
// Setting custom editors per column
DefaultTableCellEditor textEditor = new DefaultTableCellEditor() {
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column) {
String format = JDBCToTableModelAdapter1.getFormat(tableView1
.convertColumnIndexToModel(column));
if (format != null && format.trim().length() != 0)
setTransform(new FormatTransform(SASFormat
.getInstance(format)));
else
setTransform(null);
return super.getTableCellEditorComponent(table, value,
isSelected, row, column);
}
};
javax.swing.JComboBox comboBox = new javax.swing.JComboBox();
comboBox.setEditable(true);
// create a new combo editor
DefaultTableCellEditor comboEditor = new DefaultTableCellEditor(
comboBox) {
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column) {
JComboBox combo = (JComboBox) super
.getTableCellEditorComponent(table, value, isSelected,
row, column);
String columnName = TableViewUtil.getColumnName(
JDBCToTableModelAdapter1, tableView1
.convertColumnIndexToModel(column));
JDBCToListModelAdapter adapter = new JDBCToListModelAdapter(
JDBCConnection1, "select distinct " + columnName
+ " from sashelp.houses");
adapter.setTrimUsed(true);
Object[] objArray = new Object[adapter.getSize()];
for (int i = 0; i < objArray.length; i++)
objArray[i] = adapter.getElementAt(i);
DefaultComboBoxModel comboModel = new DefaultComboBoxModel(
objArray);
combo.setModel(comboModel);
combo.setSelectedItem(value);
return combo;
}
};
for (int i = 0, colCount = tableView1.getColumnCount(); i < colCount; i++) {
if (tableView1.getUnformattedColumnClass(i) == String.class) {
tableView1.getColumnModel().getColumn(i).setCellEditor(
comboEditor);
} else {
tableView1.getColumnModel().getColumn(i).setCellEditor(
textEditor);
}
}
// Connecting JToolBar to the table
tableView1.populateToolBar(JToolBar1);
JToolBar1.revalidate();
JToolBar1.repaint();
// Changing the cell background color for every other row
java.util.Map rendererMap = new java.util.HashMap();
for (int i = 0, colCount = tableView1.getColumnCount(); i < colCount; i++) {
Class cls = tableView1.getUnformattedColumnClass(i);
int col = tableView1.convertColumnIndexToModel(i) + 1;
TableCellRenderer renderer = tableView1.getDefaultRenderer(cls);
if (!rendererMap.containsKey(cls)) {
try {
renderer = (TableCellRenderer) renderer.getClass()
.newInstance();
if (renderer instanceof JComponent)
((JComponent) renderer)
.setBackground(java.awt.SystemColor.controlHighlight);
rendererMap.put(cls, renderer);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} else {
renderer = (TableCellRenderer) rendererMap.get(cls);
} // applies the renderer to the given column for every other row
tableView1.setCellRenderer(1, -1, 2, col, col, 1, renderer);
}
}
}
|
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
Type: | Sample |
Date Modified: | 2008-07-09 15:52:35 |
Date Created: | 2005-12-19 14:31:00 |
Product Family | Product | Host | Product Release | SAS Release | ||
Starting | Ending | Starting | Ending | |||
SAS System | SAS AppDev Studio | Windows Vista | 3.2 | 9.1 TS1M3 SP4 | ||
Microsoft Windows XP Professional | 3.2 | 9.1 TS1M3 SP4 | ||||
Microsoft Windows Server 2003 Standard Edition | 3.2 | 9.1 TS1M3 SP4 | ||||
Microsoft Windows Server 2003 Enterprise Edition | 3.2 | 9.1 TS1M3 SP4 | ||||
Microsoft Windows Server 2003 Datacenter Edition | 3.2 | 9.1 TS1M3 SP4 | ||||
Microsoft Windows NT Workstation | 3.2 | 9.1 TS1M3 SP4 | ||||
Microsoft Windows 2000 Professional | 3.2 | 9.1 TS1M3 SP4 | ||||
Microsoft Windows 2000 Server | 3.2 | 9.1 TS1M3 SP4 | ||||
Microsoft Windows 2000 Datacenter Server | 3.2 | 9.1 TS1M3 SP4 | ||||
Microsoft Windows 2000 Advanced Server | 3.2 | 9.1 TS1M3 SP4 | ||||
Microsoft® Windows® for x64 | 3.2 | 9.1 TS1M3 SP4 |