1 package net.walend.somnifugi.example; 2 3 import javax.swing.JFrame ; 4 import javax.swing.JTextField ; 5 import javax.swing.JTextArea ; 6 import javax.swing.JLabel ; 7 import javax.swing.JPanel ; 8 import javax.swing.JButton ; 9 import javax.swing.SwingUtilities ; 10 11 import java.awt.FlowLayout ; 12 import java.awt.BorderLayout ; 13 import java.awt.Dimension ; 14 15 import java.awt.event.ActionListener ; 16 import java.awt.event.ActionEvent ; 17 18 21 22 public abstract class SearchUI 23 implements LookupListener 24 { 25 protected JTextField searchTF; 26 private JTextArea outputTA; 27 private JFrame frame; 28 29 public SearchUI() 30 { 31 layoutComponents(); 32 } 33 34 private void layoutComponents() 35 { 36 frame = new JFrame ("CommandQueueExample"); 37 searchTF = new JTextField (); 38 outputTA = new JTextArea (); 39 outputTA.setEditable(false); 40 41 JLabel searchLabel = new JLabel ("Search"); 42 JPanel topPanel = new JPanel (new FlowLayout (FlowLayout.LEFT)); 43 JButton searchButton = new JButton ("Go"); 44 45 searchButton.addActionListener(new ActionListener () 46 { 47 public void actionPerformed(ActionEvent e) 48 { 49 searchButtonActionPerformed(); 50 } 51 }); 52 53 frame.getContentPane().setLayout(new BorderLayout ()); 54 55 searchLabel.setPreferredSize(new Dimension (45, 26)); 56 topPanel.add(searchLabel); 57 searchTF.setPreferredSize(new Dimension (100, 26)); 58 topPanel.add(searchTF); 59 topPanel.add(searchButton); 60 61 frame.getContentPane().add(topPanel, BorderLayout.NORTH); 62 frame.getContentPane().add(outputTA, BorderLayout.CENTER); 63 64 frame.setLocation(200,200); 65 frame.setSize(300,300); 66 67 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 68 } 69 70 void showFrame() 71 { 72 SwingUtilities.invokeLater(new Runnable () 73 { 74 public void run() 75 { 76 frame.setVisible(true); 77 } 78 }); 79 } 80 81 protected void handleThrowable(Throwable thrown) 82 { 83 thrown.printStackTrace(); 84 } 85 86 89 protected abstract void searchButtonActionPerformed() ; 90 91 public void lookupStarted(final LookupRequest request) 92 { 93 SwingUtilities.invokeLater(new Runnable () 94 { 95 public void run() 96 { 97 outputTA.setText("Searching for: " + request.getSearchText()); 98 } 99 }); 100 } 101 102 public void lookupCompleted(final LookupResults result) 103 { 104 SwingUtilities.invokeLater(new Runnable () 105 { 106 public void run() 107 { 108 outputTA.setText(""); 109 String [] results = result.getResults(); 110 for (int i = 0; i < results.length; i++) 111 { 112 String result = results[i]; 113 outputTA.setText(outputTA.getText() + "\n" + result); 114 } 115 } 116 }); 117 } 118 } 119 | Popular Tags |