SpringLayout and Regex Tool

D

Derek Clarkson

Hi all,
I've posted the code below of a little tool I use for playing with Regex
expressions. I find it really useful for working them out for coding in a
java app. Apart from it's possible use to you, I need some help with the
SpringLayout it uses.

I've read (and re-read and re-read) all the tutorials I can find on the web,
but I can't get it to work right. I've noticed that if you resize the
window to smaller sizes, the labels will tend to "shake" and as it gets
really small the 'String' label will start to fly towards the bottom of the
window and disappear off it. Considering these labels are supposedly fixed
in position using struts, I'm rather confused as to way they move.

Does anyone have any ideas ?

Enjoy the util.
Derek.

/*===========================================================================*/

import java.util.regex.*;
import javax.swing.*;

/**
* Regular Expression tool.
* @author dclarkson
*/
public class regexTester
extends javax.swing.JFrame {

private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField sourceString;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField tfRegex;
private javax.swing.JTextArea txtResults;

/** Creates new form regexTester */
public regexTester() {
initComponents();
}

private void initComponents() { //GEN-BEGIN:initComponents

//Create the display controls
jLabel1 = new JLabel("String");
jLabel2 = new JLabel("Regex");
jLabel3 = new JLabel("Results");
sourceString = new JTextField("This is some text");
sourceString.setMaximumSize(new java.awt.Dimension(Integer.MAX_VALUE,
(int) sourceString.getPreferredSize().getHeight()));
tfRegex = new JTextField();
tfRegex.setMaximumSize(new java.awt.Dimension(Integer.MAX_VALUE, (int)
tfRegex.getPreferredSize().getHeight()));
txtResults = new JTextArea();
txtResults.setBackground(new java.awt.Color(204, 204, 204));
txtResults.setTabSize(4);
txtResults.setBorder(new
javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.LOWERED));
txtResults.setMargin(new java.awt.Insets(2, 0, 2, 0));
txtResults.setAutoscrolls(false);

//Add listeners.
sourceString.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
keyreleased(evt);
}
});

tfRegex.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
keyreleased(evt);
}
});

//Setup the window.
SpringLayout sl = new SpringLayout();
JPanel panel = new JPanel();
panel.setLayout(sl);
this.setContentPane(panel);
this.setSize(new java.awt.Dimension(400,300));
setResizable(true);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);

//Add the controls.
panel.add(jLabel1);
panel.add(jLabel2);
panel.add(jLabel3);
panel.add(sourceString);
panel.add(tfRegex);
panel.add(txtResults);

jLabel1.setBackground(java.awt.Color.yellow);
jLabel1.setOpaque(true);
jLabel2.setBackground(java.awt.Color.yellow);
jLabel2.setOpaque(true);
jLabel3.setBackground(java.awt.Color.yellow);
jLabel3.setOpaque(true);

//Set springs.
sl.putConstraint(sl.WEST, jLabel1, 5, sl.WEST, panel);
sl.putConstraint(sl.WEST, jLabel2, 5, sl.WEST, panel);
sl.putConstraint(sl.WEST, jLabel3, 5, sl.WEST, panel);

sl.putConstraint(sl.WEST, sourceString, 5, sl.EAST, jLabel3);
sl.putConstraint(sl.WEST, tfRegex, 5, sl.EAST, jLabel3);
sl.putConstraint(sl.WEST, txtResults, 5, sl.EAST, jLabel3);

sl.putConstraint(sl.EAST, panel, 5, sl.EAST, sourceString);

sl.getConstraints(tfRegex).setWidth(sl.getConstraints(sourceString).getWidth());

sl.getConstraints(txtResults).setWidth(sl.getConstraints(sourceString).getWidth());

sl.putConstraint(sl.NORTH, jLabel1, 5, sl.NORTH, panel);
sl.putConstraint(sl.NORTH, sourceString, 5, sl.NORTH, panel);
sl.putConstraint(sl.NORTH, jLabel2, 5, sl.SOUTH, sourceString);
sl.putConstraint(sl.NORTH, tfRegex, 5, sl.SOUTH, sourceString);
sl.putConstraint(sl.NORTH, jLabel3, 5, sl.SOUTH, tfRegex);
sl.putConstraint(sl.NORTH, txtResults, 5, sl.SOUTH, tfRegex);

sl.putConstraint(sl.SOUTH, panel, 5, sl.SOUTH, txtResults);

this.show();

} //GEN-END:initComponents

private void keyreleased(java.awt.event.KeyEvent evt) {
//GEN-FIRST:event_keyreleased
searchString();
} //GEN-LAST:event_keyreleased

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new regexTester();
}

private void searchString() {
Pattern p;
Matcher m;

if (sourceString.getText().trim().length() ==0 ||
tfRegex.getText().trim().length() ==0) {
txtResults.setText("");
return;
}

try {
p = Pattern.compile(tfRegex.getText().trim());
m = p.matcher(sourceString.getText().trim());
}
catch (PatternSyntaxException pse) {
txtResults.setText("Invalid Regex Pattern.");
return;
}

//output the results.
String results = "";
int counter = 0;
while (m.find()) {
counter++;
results += String.valueOf((counter == 1 ? "" : "\n\n") + "Match #" +
String.valueOf(counter) + " found\nGroups detected: " +
m.groupCount());
results += "\nMain Match from " + String.valueOf(m.start()) + " to " +
String.valueOf(m.end());
results += " : " + m.group();
for (int i = 1; i <= m.groupCount(); i++) {
results += "\nGroup(" + String.valueOf(i) + ") from " +
String.valueOf(m.start(i)) + " to " + String.valueOf(m.end(i));
results += " : " + m.group(i);
}
}
if (results.equals("")) {
results = "No match found.";
}
txtResults.setText(results);
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top