1 31 32 package demo; 33 34 import javax.swing.*; 35 import javax.swing.table.*; 36 import javax.swing.tree.*; 37 import javax.swing.event.*; 38 import java.awt.*; 39 import java.awt.image.*; 40 import java.awt.event.*; 41 import java.net.*; 42 43 public class EverythingSwing extends JFrame { 44 45 public void test() { 46 47 setTitle("Test"); 48 setSize(1000, 340); 49 setLocation(200, 200); 50 51 try { 52 setIconImage(new ImageIcon(getClass().getResource("/demo/pic.gif")).getImage()); 53 } 54 catch (Exception e) {} 55 56 getContentPane().setLayout(new BorderLayout()); 57 58 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 60 addWindowListener(new WindowAdapter() { 61 public void windowClosing(WindowEvent e) { 62 checkClose(); 63 } 64 }); 65 66 JPanel comps = new JPanel(); 68 comps.setLayout(new FlowLayout()); 69 70 JButton btn = new JButton("Button"); 72 btn.addActionListener(new ActionListener() { 73 public void actionPerformed(ActionEvent e) { 74 showConfirm(); 75 } 76 }); 77 btn.setMnemonic('B'); 78 btn.setToolTipText("Test tooltip!"); 79 comps.add(btn); 80 81 comps.add(new JLabel("Quick test")); 82 JButton btn2 = new JButton("This works!"); 83 btn2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); 84 btn2.addActionListener(new ActionListener() { 85 public void actionPerformed(ActionEvent e) { 86 showMessage(); 87 } 88 }); 89 comps.add(btn2); 90 91 92 final JPopupMenu jpop = new JPopupMenu(); 94 JMenu mnuP = new JMenu("Popup!"); 95 jpop.add(mnuP); 96 JMenuItem mnuI = new JMenuItem("Test Item"); 97 mnuP.add(mnuI); 98 99 final JButton btn3 = new JButton("Image Button"); 101 try { 102 btn3.setIcon(new ImageIcon(getClass().getResource("/demo/Computer.png"))); 103 } 104 catch (Exception e) {} 105 btn3.addActionListener(new ActionListener() { 106 public void actionPerformed(ActionEvent e) { 107 jpop.show(btn3, 0, 0); 108 } 109 }); 110 comps.add(btn3); 111 112 JTextField tb = new JTextField("Text box"); 114 tb.addFocusListener(new FocusListener() { 115 public void focusGained(FocusEvent e) { 116 System.out.println("Text box got focus"); 117 } 118 public void focusLost(FocusEvent e) { 119 } 120 }); 121 comps.add(tb); 122 123 JScrollPane jp = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 125 JTextArea ta = new JTextArea("Text Area"); 126 ta.addKeyListener( new KeyListener() { 127 public void keyTyped(KeyEvent e) { 128 showMessage(); 129 } 130 public void keyPressed(KeyEvent e) { 131 showMessage(); 132 } 133 public void keyReleased(KeyEvent e) { 134 } 135 }); 136 jp.getViewport().add(ta); 137 comps.add(jp); 138 139 comps.add(new JCheckBox("Test")); 140 141 ButtonGroup bg = new ButtonGroup(); 142 JRadioButton rad1 = new JRadioButton("Radio1"); 143 JRadioButton rad2 = new JRadioButton("Radio2"); 144 bg.add(rad1); 145 bg.add(rad2); 146 bg.setSelected(rad1.getModel(), true); 147 comps.add(rad1); 148 comps.add(rad2); 149 150 JPasswordField pass = new JPasswordField(); 152 pass.setEchoChar('*'); 153 pass.setPreferredSize(new Dimension(100, 25)); 154 comps.add(pass); 155 156 JComboBox jc = new JComboBox(); 158 jc.setEditable(false); 159 jc.addItem("Test 1"); 160 jc.addItem("Test 2"); 161 jc.addItem("Test 3"); 162 comps.add(jc); 163 164 165 JList jl = new JList( new String [] { "Item 1", "Item 2", "Item 3" } ); 167 JScrollPane jlScr = new JScrollPane(jl); 168 jlScr.setPreferredSize(new Dimension(50, 100)); 169 comps.add(jlScr); 170 171 JSlider slid = new JSlider(JSlider.VERTICAL, 1, 100, 50); 173 slid.setPreferredSize(new Dimension(30, 150)); 174 comps.add(slid); 175 176 Object [] cols = { "Column 1", "Column 2" }; 178 Object [][] data = new Object [2][2]; 179 data[0][0] = "Col 1 data"; 180 data[0][1] = "Col 2 data"; 181 data[1][0] = "Col 1.1 data"; 182 data[1][1] = "Col 2.1 data"; 183 DefaultTableModel dm = new DefaultTableModel(data, cols); 184 JTable tab = new JTable(); 185 tab.setModel(dm); 186 tab.setCellSelectionEnabled(false); 187 tab.setRowSelectionAllowed(true); 188 tab.setPreferredSize(new Dimension(150, 100)); 189 190 tab.getColumnModel().getColumn(0).setCellRenderer( new DefaultTableCellRenderer() { 192 193 private Font renderFont = new Font("Dialog", 0, 12); 194 195 public Component getTableCellRendererComponent(JTable table, Object value, 196 boolean isSelected, boolean hasFocus, 197 int row, int column) { 198 199 setFont(renderFont); 200 if (isSelected) { 201 setBackground(table.getSelectionBackground()); 202 setForeground(table.getSelectionForeground()); 203 } 204 else 205 { 206 setBackground(table.getBackground()); 207 setForeground(table.getForeground()); 208 } 209 setIcon(new ImageIcon(getClass().getResource("/demo/pic.gif"))); 210 setValue(value); 211 return this; 212 } 213 214 }); 215 JScrollPane tabScr = new JScrollPane(tab); 216 tabScr.setPreferredSize(new Dimension(150, 100)); 217 comps.add(tabScr); 218 219 DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("RootNode"); 221 222 DefaultMutableTreeNode node11 = new DefaultMutableTreeNode("Node 11"); 223 DefaultMutableTreeNode node12 = new DefaultMutableTreeNode("Node 12"); 224 225 DefaultMutableTreeNode book111 = new DefaultMutableTreeNode("Node 111"); 226 DefaultMutableTreeNode book112 = new DefaultMutableTreeNode("Node 112"); 227 228 DefaultMutableTreeNode book121 = new DefaultMutableTreeNode("Node 121"); 229 DefaultMutableTreeNode book122 = new DefaultMutableTreeNode("Node 122"); 230 231 DefaultMutableTreeNode book131 = new DefaultMutableTreeNode("Node 131"); 232 DefaultMutableTreeNode book132 = new DefaultMutableTreeNode("Node 132"); 233 234 DefaultMutableTreeNode book141 = new DefaultMutableTreeNode("Node 141"); 235 DefaultMutableTreeNode book142 = new DefaultMutableTreeNode("Node 142"); 236 237 DefaultMutableTreeNode book151 = new DefaultMutableTreeNode("Node 151"); 238 DefaultMutableTreeNode book152 = new DefaultMutableTreeNode("Node 152"); 239 240 241 rootNode.add(node11); 242 rootNode.add(node12); 243 244 node12.add(book111); 245 node12.add(book112); 246 247 book112.add(book121); 248 book112.add(book122); 249 250 book122.add(book131); 251 book122.add(book132); 252 253 book132.add(book141); 254 book132.add(book142); 255 256 book142.add(book151); 257 book142.add(book152); 258 259 260 JTree tree = new JTree(rootNode); 261 tree.setPreferredSize(new Dimension(150, 100)); 262 tree.addTreeExpansionListener(new TreeExpansionListener() { 263 public void treeExpanded(TreeExpansionEvent event) { 264 System.out.println("treeExpanded :- "+ event.getPath()); 265 } 266 267 public void treeCollapsed(TreeExpansionEvent event) { 268 System.out.println("treeExpanded :- "+ event.getPath()); 269 } 270 }); 271 272 tree.setCellRenderer( new DefaultTreeCellRenderer() { 274 275 public Component getTreeCellRendererComponent(JTree tree, 276 Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { 277 278 super.getTreeCellRendererComponent( 279 tree, value, selected, 280 expanded, leaf, row, 281 hasFocus); 282 283 if (value.toString().indexOf("Root") != -1) 284 setIcon(new ImageIcon(getClass().getResource("/demo/pic.gif"))); 285 else 286 setIcon(null); 287 setText(value.toString()); 288 return this; 289 } 290 291 }); 292 JScrollPane treeScr = new JScrollPane(tree); 293 treeScr.setPreferredSize(new Dimension(150, 150)); 294 comps.add(treeScr); 295 296 JComponent c = new JComponent() { 298 public void paint(Graphics g) { 299 g.drawString("Direct Graphics Rendering", 20, 20); 300 g.drawOval(40, 40, 50, 50); 301 } 302 }; 303 c.setPreferredSize(new Dimension(200, 100)); 304 comps.add(c); 305 306 307 JMenuBar mb = new JMenuBar(); 309 310 JMenu mnuFile = new JMenu("File"); 311 mnuFile.setMnemonic('F'); 312 313 JCheckBoxMenuItem mnuFileToggle = new JCheckBoxMenuItem("Toggle", true); 314 JRadioButtonMenuItem mnuFileRadio = new JRadioButtonMenuItem("Radio", true); 315 JRadioButtonMenuItem mnuFileRadio2 = new JRadioButtonMenuItem("Radio2", false); 316 317 JMenu mnuFileOpen = new JMenu("Open"); 318 mnuFileOpen.setMnemonic('O'); 319 JMenuItem mnuFileOpenDoc = new JMenuItem("Document"); 320 mnuFileOpenDoc.setMnemonic('D'); 321 mnuFileOpenDoc.addActionListener(new ActionListener() { 322 public void actionPerformed(ActionEvent e) { 323 showMessage(); 324 } 325 }); 326 327 try { 328 mnuFileOpenDoc.setIcon(new ImageIcon(getClass().getResource("/demo/pic.gif"))); 329 } 330 catch (Exception e) {} 331 332 JMenuItem mnuFileOpenFileDialog = new JMenuItem("File Dialog"); 333 mnuFileOpenFileDialog.setMnemonic('i'); 334 mnuFileOpenFileDialog.addActionListener(new ActionListener() { 335 public void actionPerformed(ActionEvent e) { 336 showFileDialog(); 337 } 338 }); 339 340 JMenuItem mnuFileOpenColorDialog = new JMenuItem("Color Dialog"); 341 mnuFileOpenColorDialog.setMnemonic('C'); 342 mnuFileOpenColorDialog.addActionListener(new ActionListener() { 343 public void actionPerformed(ActionEvent e) { 344 showColorDialog(); 345 } 346 }); 347 348 JMenuItem mnuFileExit = new JMenuItem("Exit"); 349 mnuFileExit.setMnemonic('x'); 350 mnuFileExit.setAccelerator(KeyStroke.getKeyStroke( 351 KeyEvent.VK_X, InputEvent.CTRL_MASK)); 352 mnuFileExit.addActionListener(new ActionListener() { 353 public void actionPerformed(ActionEvent e) { 354 dispose(); 355 System.exit(0); 356 } 357 }); 358 359 360 mb.add(mnuFile); 361 mnuFile.add(mnuFileToggle); 362 mnuFile.addSeparator(); 363 mnuFile.add(mnuFileRadio); 364 mnuFile.add(mnuFileRadio2); 365 mnuFile.addSeparator(); 366 mnuFile.add(mnuFileOpen); 367 mnuFileOpen.add(mnuFileOpenDoc); 368 mnuFileOpen.add(mnuFileOpenFileDialog); 369 mnuFileOpen.add(mnuFileOpenColorDialog); 370 mnuFile.add(mnuFileExit); 371 372 setJMenuBar(mb); 373 374 JToolBar bar = new JToolBar(); 376 getContentPane().add(bar, BorderLayout.NORTH); 377 JButton btnTool1 = new JButton("ToolButton 1"); 378 bar.add(btnTool1); 379 JButton btnTool2 = new JButton("ToolButton 2"); 380 bar.add(btnTool2); 381 382 JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JButton("SplitPane"), comps); 384 jsp.setResizeWeight(0.1); 385 getContentPane().add(jsp, BorderLayout.CENTER); 386 387 show(); 389 } 390 391 public void showMessage() { 392 JOptionPane.showMessageDialog(this, "You did something and I caught the event!"); 393 } 394 395 public void showConfirm() { 396 if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(this, "Are you sure you wanted to press that?")) 397 System.out.println("You picked yes"); 398 else 399 System.out.println("You picked no"); 400 } 401 402 public void checkClose() { 403 if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(this, "Sure you want to close?")) { 404 dispose(); 405 System.exit(0); 406 } 407 } 408 409 public void showFileDialog() { 410 try { 411 JFileChooser jf = new JFileChooser(); 412 jf.setSelectedFile(new java.io.File (System.getProperty("user.home"))); 413 int picked = jf.showOpenDialog(this); 414 if (picked == jf.CANCEL_OPTION) 415 System.out.println("You cancelled the dialog"); 416 else { 417 System.out.println("You chose: " + jf.getSelectedFile().getAbsolutePath()); 418 } 419 } 420 catch (Exception e) { 421 e.printStackTrace(); 422 } 423 } 424 425 public void showColorDialog() { 426 try { 427 JColorChooser jf = new JColorChooser(); 428 Color c = jf.showDialog(this, "Choose that colour!", null); 429 if (c == null) 430 System.out.println("You cancelled the dialog"); 431 else { 432 System.out.println("You chose: " + c.toString()); 433 } 434 } 435 catch (Exception e) { 436 e.printStackTrace(); 437 } 438 } 439 440 public static void main(String [] args) { 441 new EverythingSwing().test(); 442 } 444 445 } 446 | Popular Tags |