import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
/**
*
*/
public class SearchPanel extends JPanel implements ActionListener
{
private JButton searchButton = new JButton("Search");
private JTextField searchField = new JTextField(15);
private JLabel resultLabel = new JLabel();
public SearchPanel()
{
super(new BorderLayout(30,70));
// Setup the GUI Elements
// Main panel
JPanel mainPanel = new JPanel(new BorderLayout());
add(mainPanel, BorderLayout.CENTER);
add(new JLabel(), BorderLayout.WEST);
add(new JLabel(), BorderLayout.EAST);
add(new JLabel(), BorderLayout.NORTH);
add(new JLabel(), BorderLayout.SOUTH);
// Top panel base
JPanel basePanel = new JPanel(new BorderLayout(20,20));
JPanel basePanelGrid = new JPanel(new GridLayout(2,1,0,10));
basePanel.add(new JLabel(), BorderLayout.WEST);
basePanel.add(new JLabel(), BorderLayout.EAST);
basePanel.add(new JLabel(), BorderLayout.NORTH);
basePanel.add(new JLabel(), BorderLayout.SOUTH);
basePanel.add(basePanelGrid, BorderLayout.CENTER);
basePanel.setBorder(new EtchedBorder());
mainPanel.add(basePanel, BorderLayout.CENTER);
// Search field
searchField.addActionListener(this);
JPanel searchBorder = new JPanel();
searchBorder.setBorder(new TitledBorder("Query"));
searchBorder.add(searchField);
basePanelGrid.add(searchBorder);
// Result label
JPanel resultBorder = new JPanel();
resultBorder.setBorder(new TitledBorder("Result"));
resultBorder.add(resultLabel);
basePanelGrid.add(resultBorder);
// Button
JPanel buttonPanel = new JPanel();
searchButton.addActionListener(this);
buttonPanel.add(searchButton, JPanel.CENTER_ALIGNMENT);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == searchButton || e.getSource() == searchField)
{
try
{
String query = searchField.getText();
query = query.toUpperCase();
searchField.setText(query);
if (query.equals("")) throw new Exception("");
if (LetterCombinationGUI.combinationSet.contains(query))
{
resultLabel.setText("contains " + query);
}
else
{
resultLabel.setText("does not contain " + query);
}
}
catch (NullPointerException err)
{
resultLabel.setText("No combinations to search");
}
catch (Exception err2)
{
resultLabel.setText("The search field is empty");
}
}
}
} Powered by
GeSHi Syntax Highlighting software.
Author of all (other) material unless otherwise specified:
Loren Segal. Copyright 2005.