Questo è il form
codice:
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Linux
*/
public class FormCercaCliente extends JFrame {
private static boolean find_least_one;
private static int i;
private JComboBox jCombo;
private JFormattedTextField jFormat;
private JButton jButton;
private JPanel jPanel1;
private JPanel jPanel2;
private JTable jTable;
public FormCercaCliente(JTable jTable)
{
this.jTable=jTable;
}
public void initComponents() {
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//SI SUPPONE CHE IL FRAME SIA UNA MATRICE 2 RIGHE E UNA COLONNA
//IN OGNI CELLA METTO UN JPANEL IN MODO DA SFRUTTARE PER OGNI JPANEL DIVERSI
//LAYOUT MANAGER
String[] values = {"ID", "Name", "Surname", "ZipCode", "E-Mail"};
this.setTitle("Search for ");
GridBagLayout grid = new GridBagLayout();//LAYOUT JFRAME
GridBagConstraints c = new GridBagConstraints();
this.setLayout(grid);
c.weightx = 1;
c.weighty = 1;
c.gridx = 0;
c.gridy = 0;
c.insets.top = 0;
c.insets.left = 0;
c.ipadx = this.getWidth();
c.ipady = (int) (0.85 * this.getHeight());
c.fill = GridBagConstraints.BOTH;
c.gridwidth = GridBagConstraints.REMAINDER;//DOPO IL PRIMO PANNELLO VADO A CAPO PER INSERIRE IL SECONDO
//nella seconda cella che si trova virtualmente sotto la prima
jPanel1 = new JPanel();
jPanel2 = new JPanel();
//INSERISCO IL PRIMO PANNELLO NELLA PRIMA CELLA DELLA GRIGLIA
//LA DIMENSIONE SERVE PER DARE DIMENSIONE ALLA CELLA
grid.setConstraints(jPanel1, c);//ABILITO I CONSTRAINTS
Dimension d = new Dimension(this.getWidth(), c.ipady);
jPanel1.setPreferredSize(d);
d.height = this.getHeight() - c.ipady;
jPanel2.setPreferredSize(d);
this.add(jPanel1);//INSERISCO L'ELEMENTO NEL FRAME
//ELEMENTI PRIMO PANNELLO jPanel1
jCombo = new JComboBox(values);
jFormat = new JFormattedTextField();
jFormat.setPreferredSize(null);
jPanel1.setLayout(new FlowLayout(FlowLayout.LEFT));//AL PRIMO PANNELLO DO UN FLOWLAYOUT
jPanel1.add(new JLabel("Search for "));
jPanel1.add(jCombo);//INSERISCO LA JCOMBOBOX
d.setSize(200, 29);
jFormat.setPreferredSize(d);
jPanel1.add(jFormat);
jFormat.setText(null);
jFormat.setEditable(true);
//DIMENSIONI SECONDO PANNELLO
c.gridy = 1;
c.ipady = this.getHeight() - c.ipady;
grid.setConstraints(jPanel2, c);
this.add(jPanel2);//INSERIMENTO NELLA GRIGLIA SECONDO PANNELLO
jButton = new JButton("Ok");
jPanel2.add(jButton);//INSERIMENTO BOTTONE
//DETERMINO POSIZIONE BOTTONE
jButton.setLocation(jPanel2.getWidth() / 2, jPanel1.getHeight() + jPanel2.getHeight() / 2);
String j = jCombo.getSelectedItem().toString();
jFormat.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
selectInputVerifier();
}
@Override
public void focusLost(FocusEvent e) {
}
});
jCombo.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
selectInputVerifier();
}
});
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
FormCercaCliente.this.findCustomers();
}
} );
this.setVisible(true);
this.pack();
this.setResizable(false);
this.setSize(jCombo.getWidth() + jFormat.getWidth() + 130, 130);//DIMENSIONE JFRAME
}
private void selectInputVerifier(){
String j=jCombo.getSelectedItem().toString();
String msg;
switch (j) {
case "ID":
msg = "Wrong Insert ID" + "\n Id must be numeric of size 4";
jFormat.setInputVerifier(new FieldInputVerifier("[0-9]+", msg, 4, 4,jButton));
break;
case "Name":
case "Surname":
msg = "Insert Error : The following field can only contain letters up to a maximum of 20";
jFormat.setInputVerifier(new FieldInputVerifier("[a-zA-Z]+", msg, 1, 20,jButton));
break;
case "ZipCode":
msg = "Invalid ZipCode: ZipCode must bu numeric of 5 numbers";
jFormat.setInputVerifier(new FieldInputVerifier("[0-9]+", msg, 5, 5,jButton));
break;
case "E-Mail":
msg = "Invalid E-Mail Address";
String r = "[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}";
jFormat.setInputVerifier(new FieldInputVerifier(r, msg, 1, 35,jButton));
break;
}
}
private void findCustomers()
{
DefaultTableModel t=(DefaultTableModel)jTable.getModel();
try{
while(!t.getValueAt(i, jCombo.getSelectedIndex()).toString().toLowerCase().equals(jFormat.getText().toString().toLowerCase()))
i++;
FormCercaCliente.find_least_one=true;
jTable.setRowSelectionInterval(i, i);
i++;
}catch(ArrayIndexOutOfBoundsException e)
{
i=0;
this.dispose();
if(find_least_one)
JOptionPane.showMessageDialog(null, "Nessun altro elemento per la chiave ricercata");
else
JOptionPane.showMessageDialog(null, "Nessun elemento per la chiave ricercata");
FormCercaCliente.find_least_one=false;
}
}
}
Questo l'inputverifier
codice:
import java.util.regex.Pattern;
import javax.swing.InputVerifier;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author linux
*/
public class FieldInputVerifier extends InputVerifier {
private String regex;
private String msg;
private int minLength;
private int maxLength;
private JButton jb;
public FieldInputVerifier(String reg_ex,String message,int minLength,int maxLength)
{
this.regex=reg_ex;
this.msg=message;
this.minLength=minLength;
this.maxLength=maxLength;
}
public FieldInputVerifier(String reg_ex,String message,int minLength,int maxLength,JButton jb)
{
this.jb=jb;
this.regex=reg_ex;
this.msg=message;
this.minLength=minLength;
this.maxLength=maxLength;
}
@Override
public boolean verify(JComponent input) {
boolean a;
Pattern p=Pattern.compile(regex);
JFormattedTextField jft=(JFormattedTextField)input;
a=(Pattern.matches(p.toString(),jft.getText()) && jft.getText().length()<=maxLength && jft.getText().length()>=minLength);
if(!a)
{
JOptionPane.showMessageDialog(null,msg );
}
return a;
}
}
[COLOR=#EEE !important]en → it
l' inputverifier
[/COLOR]