package table;
import com.ultraswing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.File;
public class Table
{
private static CFrame _frame;
private static JComponent _centerComp;
private static CFrame createFrame()
{
CFrame frame = new CFrame();
createFrame(frame);
frame.setSize(800, 600);
frame.centerOnScreen();
frame.setVisible(true);
frame.setExitOnClose();
return frame;
}
private static void createFrame(CFrame frame)
{
frame.setTitle( "Table demo" );
_centerComp = createMainWindow();
frame.getContentPane().add( _centerComp, BorderLayout.CENTER );
frame.getContentPane().add( createToolBar(), BorderLayout.NORTH);
StatusBar status = new StatusBar( new JComponent[]
{StatusBar.INDICATOR_STATUS,
StatusBar.SEPARATOR,
StatusBar.INDICATOR_DATE,
StatusBar.SEPARATOR,
StatusBar.INDICATOR_TIME,
StatusBar.SEPARATOR,
StatusBar.INDICATOR_JVM
});
frame.addStatusBar(status);
}
private static JScrollPane createMainWindow()
{
Object[][] data = {
{"Mary", "Campione",
"Snowboarding", new Integer(5), new Boolean(false)},
{"Alison", "Huml",
"Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath",
"Chasing toddlers", new Integer(2), new Boolean(false)},
{"Mark", "Andrews", "Speed reading", new Integer(20), new Boolean(true)},
{"Angela", "Lih", "Teaching high school", new Integer(4), new Boolean(false)}
};
String[] columnNames = {"First\nName\n",
"<html>Last<br>Name",
"Sport",
"# of Years",
"Vegetarian"};
CTable table = new CTable(data, columnNames);
int rowCount = table.getRowCount();
ArrayList list = new ArrayList();
for (int i = 0; i < rowCount ; i++)
{
list.add(new JLabel("rh" + i)); }
table.setRowHeaderValues(list);
table.setAlternate(new Color(233, 233, 233), Color.white, true);
table.setRowSelectionAllowed(true);
table.setSelectionBackground(Color.blue);
table.setSelectionForeground(Color.white);
JScrollPane scrollPane = new JScrollPane(table);
table.setRowHeight(30);
table.setRowHeight(2, 50);
table.setRowHeight(0, 70);
table.setRowHeaderEnabled(true);
CTableRowHeader header = table.getCTableRowHeader();
if(header != null)
{
header.setWidth(100);
}
return scrollPane;
}
private static JScrollPane createMainWindow2(String driver, String url, String query)
{
try
{
ResultSetTableModel model = new ResultSetTableModel(driver, url, query);
CTable table = new CTable(model);
table.setRowSelectionAllowed(true);
table.setSelectionBackground(Color.blue);
table.setSelectionForeground(Color.white);
JScrollPane scrollPane = new JScrollPane(table);
return scrollPane;
}
catch(Exception e)
{
System.out.println( e);
return null;
}
}
private static JScrollPane createMainWindow3(String fileName)
{
try
{
CTable table = new CTable(fileName, true);
table.setRowSelectionAllowed(true);
table.setSelectionBackground(Color.blue);
table.setSelectionForeground(Color.white);
JScrollPane scrollPane = new JScrollPane(table);
return scrollPane;
}
catch(Exception e)
{
System.out.println( e);
return null;
}
}
private static CToolBar createToolBar()
{
CToolBar toolbar = new CToolBar();
ToolBarButton btn = new ToolBarButton("CSV table");
btn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
FileDialog fdlg = new FileDialog(_frame);
fdlg.show();
String file = fdlg.getFile();
String dir = fdlg.getDirectory();
if(file != null)
{
File f = new File(dir + File.separator + file);
System.out.println("file=" + f.getAbsolutePath());
JScrollPane pane = createMainWindow3(f.getAbsolutePath());
_frame.getContentPane().remove(_centerComp);
_frame.getContentPane().add(pane, BorderLayout.CENTER);
_centerComp = pane;
_frame.validate();
_frame.repaint();
}
}
});
toolbar.add(btn);
btn = new ToolBarButton("Database table");
btn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String driver = null;
String url = null;
String query = null;
if( (driver = JOptionPane.showInputDialog(null, "Enter JDBC driver string")) != null &&
(url = JOptionPane.showInputDialog(null, "Enter JDBC url string")) != null &&
(query = JOptionPane.showInputDialog(null, "Enter SQL query")) != null)
{
JScrollPane pane = createMainWindow2(driver, url, query);
_frame.getContentPane().remove(_centerComp);
_frame.getContentPane().add(pane, BorderLayout.CENTER);
_centerComp = pane;
_frame.validate();
_frame.repaint();
}
}
});
toolbar.add(btn);
return toolbar;
}
public static CFrame getFrame()
{
return _frame;
}
public static void setFrame(CFrame f)
{
_frame = f;
}
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e)
{
System.out.println( "setLookAndFeel failed ..........................." );
}
_frame = createFrame();
}
}