1 36 37 40 41 51 52 import java.applet.Applet ; 53 import java.awt.*; 54 import java.awt.event.*; 55 import javax.swing.*; 56 import javax.swing.table.*; 57 import javax.swing.event.*; 58 import javax.swing.border.*; 59 60 public class TableExample implements LayoutManager { 61 static String [] ConnectOptionNames = { "Connect" }; 62 static String ConnectTitle = "Connection Information"; 63 64 Dimension origin = new Dimension(0, 0); 65 66 JButton fetchButton; 67 JButton showConnectionInfoButton; 68 69 JPanel connectionPanel; 70 JFrame frame; 72 JLabel userNameLabel; 73 JTextField userNameField; 74 JLabel passwordLabel; 75 JTextField passwordField; 76 JTextArea queryTextArea; 78 JComponent queryAggregate; 79 JLabel serverLabel; 80 JTextField serverField; 81 JLabel driverLabel; 82 JTextField driverField; 83 84 JPanel mainPanel; 85 86 TableSorter sorter; 87 JDBCAdapter dataBase; 88 JScrollPane tableAggregate; 89 90 94 void activateConnectionDialog() { 95 if(JOptionPane.showOptionDialog(tableAggregate, connectionPanel, ConnectTitle, 96 JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, 97 null, ConnectOptionNames, ConnectOptionNames[0]) == 0) { 98 connect(); 99 frame.setVisible(true); 100 } 101 else if(!frame.isVisible()) 102 System.exit(0); 103 } 104 105 109 public void createConnectionDialog() { 110 userNameLabel = new JLabel("User name: ", JLabel.RIGHT); 112 userNameField = new JTextField("guest"); 113 114 passwordLabel = new JLabel("Password: ", JLabel.RIGHT); 115 passwordField = new JTextField("trustworthy"); 116 117 serverLabel = new JLabel("Database URL: ", JLabel.RIGHT); 118 serverField = new JTextField("jdbc:sybase://dbtest:1455/pubs2"); 119 120 driverLabel = new JLabel("Driver: ", JLabel.RIGHT); 121 driverField = new JTextField("connect.sybase.SybaseDriver"); 122 123 124 connectionPanel = new JPanel(false); 125 connectionPanel.setLayout(new BoxLayout(connectionPanel, 126 BoxLayout.X_AXIS)); 127 128 JPanel namePanel = new JPanel(false); 129 namePanel.setLayout(new GridLayout(0, 1)); 130 namePanel.add(userNameLabel); 131 namePanel.add(passwordLabel); 132 namePanel.add(serverLabel); 133 namePanel.add(driverLabel); 134 135 JPanel fieldPanel = new JPanel(false); 136 fieldPanel.setLayout(new GridLayout(0, 1)); 137 fieldPanel.add(userNameField); 138 fieldPanel.add(passwordField); 139 fieldPanel.add(serverField); 140 fieldPanel.add(driverField); 141 142 connectionPanel.add(namePanel); 143 connectionPanel.add(fieldPanel); 144 } 145 146 public TableExample() { 147 mainPanel = new JPanel(); 148 149 createConnectionDialog(); 151 152 showConnectionInfoButton = new JButton("Configuration"); 154 showConnectionInfoButton.addActionListener(new ActionListener() { 155 public void actionPerformed(ActionEvent e) { 156 activateConnectionDialog(); 157 } 158 } 159 ); 160 161 fetchButton = new JButton("Fetch"); 162 fetchButton.addActionListener(new ActionListener() { 163 public void actionPerformed(ActionEvent e) { 164 fetch(); 165 } 166 } 167 ); 168 169 queryTextArea = new JTextArea("SELECT * FROM titles", 25, 25); 171 queryAggregate = new JScrollPane(queryTextArea); 172 queryAggregate.setBorder(new BevelBorder(BevelBorder.LOWERED)); 173 174 tableAggregate = createTable(); 176 tableAggregate.setBorder(new BevelBorder(BevelBorder.LOWERED)); 177 178 mainPanel.add(fetchButton); 180 mainPanel.add(showConnectionInfoButton); 181 mainPanel.add(queryAggregate); 182 mainPanel.add(tableAggregate); 183 mainPanel.setLayout(this); 184 185 frame = new JFrame("TableExample"); 187 frame.addWindowListener(new WindowAdapter() { 188 public void windowClosing(WindowEvent e) {System.exit(0);}}); 189 frame.setBackground(Color.lightGray); 190 frame.getContentPane().add(mainPanel); 191 frame.pack(); 192 frame.setVisible(false); 193 frame.setBounds(200, 200, 640, 480); 194 195 activateConnectionDialog(); 196 } 197 198 public void connect() { 199 dataBase = new JDBCAdapter( 200 serverField.getText(), 201 driverField.getText(), 202 userNameField.getText(), 203 passwordField.getText()); 204 sorter.setModel(dataBase); 205 } 206 207 public void fetch() { 208 dataBase.executeQuery(queryTextArea.getText()); 209 } 210 211 public JScrollPane createTable() { 212 sorter = new TableSorter(); 213 214 217 JTable table = new JTable(sorter); 219 table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 221 222 sorter.addMouseListenerToHeaderInTable(table); 224 225 JScrollPane scrollpane = new JScrollPane(table); 226 227 return scrollpane; 228 } 229 230 public static void main(String s[]) { 231 new TableExample(); 232 } 233 234 public Dimension preferredLayoutSize(Container c){return origin;} 235 public Dimension minimumLayoutSize(Container c){return origin;} 236 public void addLayoutComponent(String s, Component c) {} 237 public void removeLayoutComponent(Component c) {} 238 public void layoutContainer(Container c) { 239 Rectangle b = c.getBounds(); 240 int topHeight = 90; 241 int inset = 4; 242 showConnectionInfoButton.setBounds(b.width-2*inset-120, inset, 120, 25); 243 fetchButton.setBounds(b.width-2*inset-120, 60, 120, 25); 244 queryAggregate.setBounds(inset, inset, b.width-2*inset - 150, 80); 246 tableAggregate.setBounds(new Rectangle(inset, 247 inset + topHeight, 248 b.width-2*inset, 249 b.height-2*inset - topHeight)); 250 } 251 252 } 253 | Popular Tags |