Ho raggruppato in questa pagina qualche 'chicca' di programmazione che ho raccolto durante gli anni di lavoro come ingegnere informatico.
I linguaggi di programmazione utilizzati sono principalmente Java e SQL anche se conto di aggiungere tips in altri linguaggi, magari anche con i vostri contributi. Ho voluto mette solo quelle routine che permettono di avere un risultato apprezzabile utilizzando pochissime linee di codice...che sono le mie preferite!

I have gathered here some 'gem' of programming that I have collected over the years working as a software engineer. The programming languages used are mainly Java and SQL even if I hope to add tips in other languages, even with your contributions. I posted only those routines that allow for a successful result using very few lines of code ... which are my preferred!


Visualizzazione post con etichetta java. Mostra tutti i post
Visualizzazione post con etichetta java. Mostra tutti i post

mercoledì 22 aprile 2009

[JAVA] LIMITARE IL NUMERO DI CARATTERI INSERIBILI IN UNA TEXT AREA - FIXED LENGHT TEXT AREA FIELD

  JTextField jtf=new JTextField(10);
  jtf.setDocument(new PlainDocument(){
   public void insertString(int offset, String string, AttributeSet a)
   throws BadLocationException {
    if(getLength() + string.length() <= 10)
    super.insertString(offset, string, a);
   else
    throw new BadLocationException(string, offset);
    });
  });


10 è il massimo numero di caratteri che possiamo inserire
10 is the maximum leght of the string we can insert

[JAVA] EFFETTUARE CONFRONTI SU STRINGHE UTILIZZANDO '*' - MATCH STRING USING LIKE OPERATOR '*'

private boolean like(String stringa,String pattern){
  String sstringa=stringa.toLowerCase();
  String spattern=pattern.toLowerCase();
  spattern=spattern.replaceAll("\\*","\\.*");
  return Pattern.matches(spattern,sstringa);
}

[JAVA] RIMUOVERE DUPLICATI DA UNA LISTA - REMOVE DUPLICATE ON ARRAY LIST

public List removeDuplicate(List sourceList){
  java.util.Set setPmpListArticle=new HashSet(sourceList);
  return new ArrayList(setPmpListArticle);
}

[JAVA] CENTRARE UN COMPONENTE GRAFICO SULLO SCHERMO - CENTER A GUI COMPONENT ON THE SCREEN

public void centerComponentOnScreen(Component component){
  Dimension screenSize=java.awt.Toolkit.getDefaultToolkit().getScreenSize();
  component.setLocation((screenSize.width - component.getWidth()) / 2,screenSize.height - component.getHeight()) / 2);
}

[JAVA] TROVARE INTERSEZIONE TRA DUE LISTE - FIND ARRAY LIST INTERSECTION

public java.util.List intersect(List a, List b){
  java.util.List lstIntersectAB=new ArrayList(a);
  lstIntersectAB.retainAll(b);
  return lstIntersectAB;
}