Sample Code

Post comment (Allready 0)


This is a tool (a java library) to provide a way to display and edit conditional filters in java swing.

It is losely based on NSRuleEditor from Apple's Cocoa framework used for example in iTune's smart playlist. Losely based as in "just looked at a screenshot" as I neither use Mac nor have iTunes.

Example used in GUI

As an additional Bonus, it provides funtions to gen an SQL where-string (or prepared statements) from the filters, making filtering from a DB ideally as easy as

String sql = "SELECT * FROM TABLE WHERE " + filter.getSqlWhere();
Back to contents

Sample Code

This will build an example frame with an empty/new conditionalFilter. Note that the MasterFilter is your end filter that you will need, as received by ConditionalFilter.getMasterFilter()

final JFrame frame = new JFrame ("Filter Test");
frame.setSize(800, 600);

// lets make some possible filters that the user can use
FlatFilter firstNameFilter = new StringFilter("First name", "firstname");
FlatFilter lastNameFilter = new StringFilter("Last name", "lastname");
NumberFilter favNumberFilter = new NumberFilter("Favorite number", "favno");
StringFilter favColorFilter = new FixedValuesStringFilter("Favorite color", "favco",
		"red", "blue", "green", "black", "white", "purple");
DateFilter birthdayFilter = new DateFilter("Birthday"); // example that a raw name isn't needed if it's he same or you don't plan to use sql anyway

ConditionalFilter filterPanel = new ConditionalFilter(false, firstNameFilter, lastNameFilter,
		favNumberFilter, favColorFilter, birthdayFilter);

JPanel frameContent = new JPanel();
JPanel buttonPanel = new JPanel();

JButton report = new JButton("Report");
JButton reportSql = new JButton("Report SQL");
report.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
		MasterFilter mf = filterPanel.getMasterFilter();
		JOptionPane.showMessageDialog(frame, mf.toString());
reportSql.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
		MasterFilter mf = filterPanel.getMasterFilter();
		// the sql is one long string, to add line wrap to optionpane, we wrap it in html
		String escapedSql = mf.getSqlWhere().replace(">", ">").replace("<", "<");
		JOptionPane.showMessageDialog(frame, "

" + escapedSql + "

"); } }); buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS)); buttonPanel.add(Box.createHorizontalGlue()); buttonPanel.add(report); buttonPanel.add(Box.createHorizontalStrut(5)); buttonPanel.add(reportSql); buttonPanel.setMaximumSize(new Dimension(buttonPanel.getMaximumSize().width, report.getPreferredSize().height)); frameContent.setLayout(new BoxLayout(frameContent, BoxLayout.Y_AXIS)); frameContent.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); frameContent.add(filterPanel); frameContent.add(Box.createVerticalStrut(5)); frameContent.add(buttonPanel); frame.add(frameContent); frame.setVisible(true);

To add values to the filter panel, to edit an existing filter or provide a default list, you would construct the panel as follows. The example values set here are the same as in the screenshot above:

MasterFilter masterFilter = new MasterFilter(ContainerType.ANY);
ContainerFilter lincNameContainer = new ContainerFilter(ContainerType.ALL);
StringFilter sf1 = (StringFilter)firstNameFilter.clone();
sf1.setFilterConditions(StringFilterMode.CONTAINS, "ab"); // abraham, abe
StringFilter sf2 = (StringFilter)lastNameFilter.clone();
sf2.setFilterConditions(StringFilterMode.IS, "lincoln");

ContainerFilter johnNameContainer = new ContainerFilter(ContainerType.ALL);
sf1 = (StringFilter)firstNameFilter.clone();
// note, default filter mode is IS, so sf1.setString("andrew") would be enough, this is just to be safe
sf1.setFilterConditions(StringFilterMode.IS, "andrew");
sf2 = (StringFilter)lastNameFilter.clone();
sf2.setFilterConditions(StringFilterMode.IS, "johnson");

FixedValuesStringFilter colorFilter = (FixedValuesStringFilter)favColorFilter.clone();
colorFilter.setString(colorFilter.getPossibleValues().iterator().next()); // just the first one


final ConditionalFilter filterPanel = new ConditionalFilter(masterFilter, firstNameFilter, lastNameFilter,
		favNumberFilter, favColorFilter, birthdayFilter);

The library jar file contains above method as the main method and can be run to see this very code in action.

Back to contents


Would be nice if you would drop a link back to me if you use it. As for the rest, you're only bound to the licence of JNBT. See the Licence directory in the zip file for the full licence text.

Back to contents


ConditionalFilter sources

Back to contents


Add comment


(Optional, hidden. Only if you want to be notified of replies)

* Spam check: ⑳ + ㊱ =

Back to contents