comparison 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
comparison
equal deleted inserted replaced
6:670905eb688a 7:ea3ae6ce9fea
1 package connectionmanager; 1 package connectionmanager;
2 2
3 import java.awt.Component;
3 import java.awt.Point; 4 import java.awt.Point;
4 import java.awt.event.MouseAdapter; 5 import java.awt.event.MouseAdapter;
5 import java.awt.event.MouseEvent; 6 import java.awt.event.MouseEvent;
7 import javax.swing.JLabel;
8 import javax.swing.JTable;
6 import javax.swing.table.DefaultTableModel; 9 import javax.swing.table.DefaultTableModel;
10 import javax.swing.table.DefaultTableCellRenderer;
11
12
13 class CellRenderer extends DefaultTableCellRenderer {
14 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
15 return super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
16 }
17 }
18
7 19
8 public class DatabaseSelectorDialog extends javax.swing.JDialog { 20 public class DatabaseSelectorDialog extends javax.swing.JDialog {
9 21
10 public boolean cancelled = true; 22 public boolean cancelled = true;
11 public String hostname; 23 public String hostname;
13 public String username; 25 public String username;
14 26
15 public DatabaseSelectorDialog(java.awt.Frame parent) { 27 public DatabaseSelectorDialog(java.awt.Frame parent) {
16 super(parent, true); 28 super(parent, true);
17 initComponents(); 29 initComponents();
30 getRootPane().setDefaultButton(selectButton);
31 databasesTable.setDefaultRenderer(Object.class, new CellRenderer());
18 databasesTable.addMouseListener(new MouseListenerDoubleClick()); 32 databasesTable.addMouseListener(new MouseListenerDoubleClick());
19 } 33 }
20 34
21 public DatabaseSelectorDialog() { 35 public DatabaseSelectorDialog() {
22 super(new javax.swing.JFrame(), true); 36 super(new javax.swing.JFrame(), true);
23 initComponents(); 37 initComponents();
38 getRootPane().setDefaultButton(selectButton);
39 databasesTable.setDefaultRenderer(Object.class, new CellRenderer());
24 databasesTable.addMouseListener(new MouseListenerDoubleClick()); 40 databasesTable.addMouseListener(new MouseListenerDoubleClick());
25 } 41 }
26 42
27 private class MouseListenerDoubleClick extends MouseAdapter { 43 private class MouseListenerDoubleClick extends MouseAdapter {
28 44
46 private void initComponents() { 62 private void initComponents() {
47 63
48 cancelButton = new javax.swing.JButton(); 64 cancelButton = new javax.swing.JButton();
49 newButton = new javax.swing.JButton(); 65 newButton = new javax.swing.JButton();
50 selectButton = new javax.swing.JButton(); 66 selectButton = new javax.swing.JButton();
51 jScrollPane1 = new javax.swing.JScrollPane(); 67 scrollPane = new javax.swing.JScrollPane();
52 databasesTable = new javax.swing.JTable(); 68 databasesTable = new javax.swing.JTable();
53 69
54 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); 70 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
55 setTitle("Select a connection"); 71 setTitle("Select database");
72 setResizable(false);
73 addKeyListener(new java.awt.event.KeyAdapter() {
74 public void keyPressed(java.awt.event.KeyEvent evt) {
75 formKeyPressed(evt);
76 }
77 });
56 78
57 cancelButton.setText("Cancel"); 79 cancelButton.setText("Cancel");
58 cancelButton.addActionListener(new java.awt.event.ActionListener() { 80 cancelButton.addActionListener(new java.awt.event.ActionListener() {
59 public void actionPerformed(java.awt.event.ActionEvent evt) { 81 public void actionPerformed(java.awt.event.ActionEvent evt) {
60 cancelButtonActionPerformed(evt); 82 cancelButtonActionPerformed(evt);
75 } 97 }
76 }); 98 });
77 99
78 databasesTable.setModel(new javax.swing.table.DefaultTableModel( 100 databasesTable.setModel(new javax.swing.table.DefaultTableModel(
79 new Object [][] { 101 new Object [][] {
80 102 {"1", null, null},
103 {"2", null, null},
104 {"3", null, null},
105 {"4", null, null},
106 {"5", null, null},
107 {"6", null, null},
108 {"7", null, null},
109 {"8", null, null}
81 }, 110 },
82 new String [] { 111 new String [] {
83 "Hostname", "Database", "Username" 112 "Hostname", "Database", "Username"
84 } 113 }
85 ) { 114 ) {
96 125
97 public boolean isCellEditable(int rowIndex, int columnIndex) { 126 public boolean isCellEditable(int rowIndex, int columnIndex) {
98 return canEdit [columnIndex]; 127 return canEdit [columnIndex];
99 } 128 }
100 }); 129 });
101 jScrollPane1.setViewportView(databasesTable); 130 databasesTable.setFocusable(false);
131 databasesTable.setGridColor(new java.awt.Color(204, 204, 204));
132 databasesTable.setIntercellSpacing(new java.awt.Dimension(0, 0));
133 databasesTable.setRowHeight(24);
134 databasesTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
135 databasesTable.setShowVerticalLines(false);
136 scrollPane.setViewportView(databasesTable);
102 137
103 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); 138 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
104 getContentPane().setLayout(layout); 139 getContentPane().setLayout(layout);
105 layout.setHorizontalGroup( 140 layout.setHorizontalGroup(
106 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 141 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
107 .add(layout.createSequentialGroup() 142 .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
108 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 143 .addContainerGap()
109 .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() 144 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
110 .add(183, 183, 183) 145 .add(org.jdesktop.layout.GroupLayout.LEADING, scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 409, Short.MAX_VALUE)
146 .add(layout.createSequentialGroup()
147 .add(newButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
148 .add(184, 184, 184)
111 .add(cancelButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 149 .add(cancelButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
112 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 150 .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
113 .add(newButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 151 .add(selectButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
114 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
115 .add(selectButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
116 .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
117 .addContainerGap()
118 .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)))
119 .addContainerGap()) 152 .addContainerGap())
120 ); 153 );
121 layout.setVerticalGroup( 154 layout.setVerticalGroup(
122 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 155 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
123 .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() 156 .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
124 .addContainerGap(20, Short.MAX_VALUE) 157 .addContainerGap()
125 .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 225, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 158 .add(scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE)
126 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 159 .add(18, 18, 18)
127 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER) 160 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
161 .add(newButton)
128 .add(selectButton) 162 .add(selectButton)
129 .add(newButton)
130 .add(cancelButton)) 163 .add(cancelButton))
131 .add(207, 207, 207)) 164 .addContainerGap())
132 ); 165 );
133 166
134 pack(); 167 pack();
135 }// </editor-fold>//GEN-END:initComponents 168 }// </editor-fold>//GEN-END:initComponents
136 169
159 username = new String(""); 192 username = new String("");
160 doClose(); 193 doClose();
161 } 194 }
162 }//GEN-LAST:event_newButtonActionPerformed 195 }//GEN-LAST:event_newButtonActionPerformed
163 196
197 private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
198 System.out.printf("key %d is %d\n", evt.getKeyCode(), evt.VK_DOWN);
199 if (evt.getKeyCode() == evt.VK_DOWN) {
200 int row = databasesTable.getSelectedRow() + 1;
201 databasesTable.addRowSelectionInterval(row, row);
202 return;
203 }
204 if (evt.getKeyCode() == evt.VK_UP) {
205 int row = databasesTable.getSelectedRow() - 1;
206 databasesTable.addRowSelectionInterval(row, row);
207 return;
208 }
209 }//GEN-LAST:event_formKeyPressed
210
164 private void doClose() { 211 private void doClose() {
165 setVisible(false); 212 setVisible(false);
166 dispose(); 213 dispose();
167 } 214 }
168 215
169 public void add(String hostname, String database, String username) { 216 public void add(String hostname, String database, String username) {
170 DefaultTableModel model = (DefaultTableModel) databasesTable.getModel(); 217 DefaultTableModel model = (DefaultTableModel) databasesTable.getModel();
171 model.insertRow(model.getRowCount(), new Object[]{hostname, database, username}); 218 model.insertRow(model.getRowCount(), new Object[]{hostname, database, username});
219 databasesTable.addRowSelectionInterval(0, 0);
172 } 220 }
173 221
174 // Variables declaration - do not modify//GEN-BEGIN:variables 222 // Variables declaration - do not modify//GEN-BEGIN:variables
175 private javax.swing.JButton cancelButton; 223 private javax.swing.JButton cancelButton;
176 private javax.swing.JTable databasesTable; 224 private javax.swing.JTable databasesTable;
177 private javax.swing.JScrollPane jScrollPane1;
178 private javax.swing.JButton newButton; 225 private javax.swing.JButton newButton;
226 private javax.swing.JScrollPane scrollPane;
179 private javax.swing.JButton selectButton; 227 private javax.swing.JButton selectButton;
180 // End of variables declaration//GEN-END:variables 228 // End of variables declaration//GEN-END:variables
181 } 229 }