1 2 import com4j.ComException; 3 import wsh.ClassFactory; 4 5 import javax.swing.*; 6 import javax.swing.border.EmptyBorder ; 7 import java.awt.*; 8 import java.awt.event.ActionEvent ; 9 import java.awt.event.ActionListener ; 10 11 16 public class SwingDemo extends JFrame { 17 private JTextField value; 18 private JButton submit; 19 private JTextField name; 20 21 public SwingDemo() throws HeadlessException { 22 super("com4j demo"); 23 24 Container panel = getContentPane(); 25 panel.setLayout(new GridBagLayout()); 26 GridBagConstraints c = new GridBagConstraints(); 27 28 panel.add( new JLabel("Env var name:"), c ); 29 30 c = new GridBagConstraints(); 31 c.gridx=1; 32 c.ipadx=200; 33 c.fill=GridBagConstraints.BOTH; 34 panel.add( name = new JTextField(), c ); 35 name.setEditable(true); 36 37 c = new GridBagConstraints(); 38 c.gridx=2; 39 panel.add( submit = new JButton("Enter"), c ); 40 41 c = new GridBagConstraints(); 42 c.gridy=1; 43 c.gridwidth=3; 44 c.fill=GridBagConstraints.BOTH; 45 panel.add( value = new JTextField("Type in an environment variable (like 'PATH') and see its value"), c ); 46 value.setEditable(false); 47 value.setBorder(new EmptyBorder (4,4,4,4)); 48 value.setBackground(getBackground()); 49 registerListeners(); 50 51 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 52 53 pack(); 54 setVisible(true); 55 setResizable(false); 56 } 57 58 private void registerListeners() { 59 ActionListener l = new ActionListener () { 60 public void actionPerformed(ActionEvent e) { 61 value.setText(expandEnvVar(name.getText())); 62 } 63 }; 64 submit.addActionListener(l); 65 name.addActionListener(l); 66 } 67 68 69 public String expandEnvVar(String name) { 70 try { 71 name = '%'+name+'%'; 72 String value = ClassFactory.createWshShell().expandEnvironmentStrings(name); 73 if(name.equals(value)) 74 return "Not a valid variable"; 75 else 76 return value; 77 } catch( ComException e ) { 78 return e.getMessage(); 79 } 80 } 81 82 public static void main(String [] args) throws Exception { 83 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 84 JFrame.setDefaultLookAndFeelDecorated(true); 85 new SwingDemo(); 86 } 87 } 88
| Popular Tags
|