diff ConnectionManager/src/connectionmanager/DatabaseSelectorDialog.java @ 7:ea3ae6ce9fea default tip

Improved Java interface look.
author Daniele Nicolodi <daniele@grinta.net>
date Wed, 23 Nov 2011 20:27:21 +0100
parents 670905eb688a
children
line wrap: on
line diff
--- a/ConnectionManager/src/connectionmanager/DatabaseSelectorDialog.java	Wed Jun 02 18:02:25 2010 +0200
+++ b/ConnectionManager/src/connectionmanager/DatabaseSelectorDialog.java	Wed Nov 23 20:27:21 2011 +0100
@@ -1,9 +1,21 @@
 package connectionmanager;
 
+import java.awt.Component;
 import java.awt.Point;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
+import javax.swing.JLabel;
+import javax.swing.JTable;
 import javax.swing.table.DefaultTableModel;
+import javax.swing.table.DefaultTableCellRenderer;
+
+
+class CellRenderer extends DefaultTableCellRenderer {
+    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
+        return super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
+    }
+}
+
 
 public class DatabaseSelectorDialog extends javax.swing.JDialog {
 
@@ -15,12 +27,16 @@
     public DatabaseSelectorDialog(java.awt.Frame parent) {
         super(parent, true);
         initComponents();
+        getRootPane().setDefaultButton(selectButton);
+        databasesTable.setDefaultRenderer(Object.class, new CellRenderer());
         databasesTable.addMouseListener(new MouseListenerDoubleClick());
     }
 
     public DatabaseSelectorDialog() {
         super(new javax.swing.JFrame(), true);
         initComponents();
+        getRootPane().setDefaultButton(selectButton);
+        databasesTable.setDefaultRenderer(Object.class, new CellRenderer());
         databasesTable.addMouseListener(new MouseListenerDoubleClick());
     }
 
@@ -48,11 +64,17 @@
         cancelButton = new javax.swing.JButton();
         newButton = new javax.swing.JButton();
         selectButton = new javax.swing.JButton();
-        jScrollPane1 = new javax.swing.JScrollPane();
+        scrollPane = new javax.swing.JScrollPane();
         databasesTable = new javax.swing.JTable();
 
         setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
-        setTitle("Select a connection");
+        setTitle("Select database");
+        setResizable(false);
+        addKeyListener(new java.awt.event.KeyAdapter() {
+            public void keyPressed(java.awt.event.KeyEvent evt) {
+                formKeyPressed(evt);
+            }
+        });
 
         cancelButton.setText("Cancel");
         cancelButton.addActionListener(new java.awt.event.ActionListener() {
@@ -77,7 +99,14 @@
 
         databasesTable.setModel(new javax.swing.table.DefaultTableModel(
             new Object [][] {
-
+                {"1", null, null},
+                {"2", null, null},
+                {"3", null, null},
+                {"4", null, null},
+                {"5", null, null},
+                {"6", null, null},
+                {"7", null, null},
+                {"8", null, null}
             },
             new String [] {
                 "Hostname", "Database", "Username"
@@ -98,37 +127,41 @@
                 return canEdit [columnIndex];
             }
         });
-        jScrollPane1.setViewportView(databasesTable);
+        databasesTable.setFocusable(false);
+        databasesTable.setGridColor(new java.awt.Color(204, 204, 204));
+        databasesTable.setIntercellSpacing(new java.awt.Dimension(0, 0));
+        databasesTable.setRowHeight(24);
+        databasesTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
+        databasesTable.setShowVerticalLines(false);
+        scrollPane.setViewportView(databasesTable);
 
         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
         getContentPane().setLayout(layout);
         layout.setHorizontalGroup(
             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
-            .add(layout.createSequentialGroup()
-                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
-                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
-                        .add(183, 183, 183)
+            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
+                .addContainerGap()
+                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
+                    .add(org.jdesktop.layout.GroupLayout.LEADING, scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 409, Short.MAX_VALUE)
+                    .add(layout.createSequentialGroup()
+                        .add(newButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
+                        .add(184, 184, 184)
                         .add(cancelButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
-                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
-                        .add(newButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
-                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
-                        .add(selectButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
-                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
-                        .addContainerGap()
-                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)))
+                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
+                        .add(selectButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                 .addContainerGap())
         );
         layout.setVerticalGroup(
             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
             .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
-                .addContainerGap(20, Short.MAX_VALUE)
-                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 225, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
-                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
+                .addContainerGap()
+                .add(scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE)
+                .add(18, 18, 18)
                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
+                    .add(newButton)
                     .add(selectButton)
-                    .add(newButton)
                     .add(cancelButton))
-                .add(207, 207, 207))
+                .addContainerGap())
         );
 
         pack();
@@ -161,6 +194,20 @@
         }
     }//GEN-LAST:event_newButtonActionPerformed
 
+    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
+        System.out.printf("key %d is %d\n", evt.getKeyCode(), evt.VK_DOWN);
+        if (evt.getKeyCode() == evt.VK_DOWN) {
+            int row = databasesTable.getSelectedRow() + 1;
+            databasesTable.addRowSelectionInterval(row, row);
+            return;
+        }
+        if (evt.getKeyCode() == evt.VK_UP) {
+            int row = databasesTable.getSelectedRow() - 1;
+            databasesTable.addRowSelectionInterval(row, row);
+            return;
+        }
+    }//GEN-LAST:event_formKeyPressed
+
     private void doClose() {
         setVisible(false);
         dispose();
@@ -169,13 +216,14 @@
     public void add(String hostname, String database, String username) {
         DefaultTableModel model = (DefaultTableModel) databasesTable.getModel();
         model.insertRow(model.getRowCount(), new Object[]{hostname, database, username});
+        databasesTable.addRowSelectionInterval(0, 0);
     }
     
     // Variables declaration - do not modify//GEN-BEGIN:variables
     private javax.swing.JButton cancelButton;
     private javax.swing.JTable databasesTable;
-    private javax.swing.JScrollPane jScrollPane1;
     private javax.swing.JButton newButton;
+    private javax.swing.JScrollPane scrollPane;
     private javax.swing.JButton selectButton;
     // End of variables declaration//GEN-END:variables
 }