Tips&Tricks | I trucchi del mestiere |
![]() |
Gestione avanzata eccezioni! |
/** * Classe dÆutilitα per uso dei TryMethodsà */ public class ExceptionUtil { private ExceptionUtil() {} public static Object execute(TryMethod method,java.util.Collection args){ try { return method.execute(args); }catch (Exception ex) { return ex; } } }//end /** * Interfaccia che stabilisce il contratto di un TryMethod da implementare per le * RuntimeExceptionà */ public interface TryMethod{ public Object execute(java.util.Collection args); } /** * Implementazione di TryMethod per le checked exceptionà */ public class ReflectiveMethod implements TryMethod{ private Object target; private String methodName; private Class[] paramTypes; public void init(Object target, String methodName, Class [] paramTypes){ this.target = target; this.methodName = methodName; this.paramTypes = paramTypes; } public Object execute(java.util.Collection args){ try { Class clazz = target instanceof Class?(Class)target:target.getClass(); return (clazz.getDeclaredMethod(methodName,paramTypes)).invoke( (target instanceof Class?null:target),args.toArray()); }catch (java.lang.reflect.InvocationTargetException iex){return iex.getTargetException();}catch (Exception ex) {return ex;} } }//end |
![]() |
Un semplice blocco note |
import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; public class BloccoNotes implements WindowListener, ActionListener{ //Istanze della classe private Frame finestra; private TextArea areaTesto; //Costruttore della classe public BloccoNotes(Frame f, TextArea t) { finestra = f; areaTesto = t; } //Gestione della grafica // Metodi non implementati dell'interfaccia WindowListener public void windowActivated(WindowEvent evt) {} public void windowDeactivated(WindowEvent evt) {} public void windowDeiconified(WindowEvent evt) {} public void windowIconified(WindowEvent evt) {} public void windowOpened(WindowEvent evt) {} // Metodi implementati dell'interfaccia WindowListener public void windowClosed(WindowEvent evt) { System.exit(0); } public void windowClosing(WindowEvent evt) { finestra.dispose(); } // Metodo acoltatore ha il computo di ascoltare i comandi public void actionPerformed(ActionEvent evt) { String comando = evt.getActionCommand(); //gestione dei vari eventi if (comando.equals("Esci")) { finestra.dispose(); } else if (comando.equals("Apri")) { areaTesto.setText(""); FileDialog d = new FileDialog(finestra, "Apri documento", FileDialog.LOAD); d.setVisible(true); if (d.getFile() != null) { try { String line; BufferedReader in = new BufferedReader(new FileReader(d.getDirectory() + File.separator + d.getFile())); while ((line = in.readLine()) != null) areaTesto.append(line + "\n"); } catch (Exception e) { System.out.println("Errore: " + e); } } } else if (comando.equals("Salva con nome...")) { FileDialog d = new FileDialog(finestra, "Salva documento con nome", FileDialog.SAVE); d.setVisible(true); PrintWriter fout; String Stringa=areaTesto.getText(); try{ fout=new PrintWriter(new FileWriter(d.getDirectory()+File.separator+d.getFile())); StringTokenizer st=new StringTokenizer(Stringa,"\n"); //E' importante per gestire la pressione dell'invio while (st.hasMoreTokens()) fout.println(st.nextToken()); fout.close(); }catch(Exception e){} } } /** Metodo Main */ public static void main(String[] args) { Frame f = new Frame("My blocco note "); TextArea t = new TextArea(); BloccoNotes n = new BloccoNotes(f, t); MenuBar mb = new MenuBar(); // Preparazione men∙ file il menu File Menu menu = new Menu("File"); mb.add(menu); menu.add("Apri"); menu.add("Salva con nome..."); menu.addSeparator(); menu.add("Esci"); menu.addActionListener(n); // Prepara la finestra f.addWindowListener(n); f.setMenuBar(mb); f.add(t); f.setSize(400, 500); f.setVisible(true); } } // BloccoNotes |
![]() |
Connessioni JDBC condivise |
![]() |
Ordinare un array bidimensionale |
import java.io.PrintStream; public class Ordinamento { public Ordinamento() { nome = new String[5][4]; nome[0][0] = "Rossi Paolo "; nome[0][1] = "Garibaldi n\260 34 "; nome[0][2] = "Roma "; nome[0][3] = "00100 "; nome[1][0] = "Rossi Mario "; nome[1][1] = "Mazzini n\260 14 "; nome[1][2] = "Milano "; nome[1][3] = "20100 "; nome[2][0] = "Rossi Francesca "; nome[2][1] = "Napoleone n\260 121 "; nome[2][2] = "Milano "; nome[2][3] = "20100 "; nome[3][0] = "Rossi Giovanni "; nome[3][1] = "Lincolm n\260 12 "; nome[3][2] = "Torino "; nome[3][3] = "10100 "; nome[4][0] = "Rossi Alberto "; nome[4][1] = "Cattaneo n\260 176 "; nome[4][2] = "Bologna "; nome[4][3] = "40100 "; System.out.println(" SCRITTURA DISORDINATA ALFABETICAMENTE"); System.out.println(" "); System.out.println("Cognome e nome Via Citt\340 C.A.P. "); System.out.println(" "); for(x = 0; x < 5; x++) { System.out.println(" "); for(j = 0; j < 4; j++) System.out.print(nome[x][j]); } for(int i = nome.length - 1; i >= 0; i--) { for(int k = 1; k <= i; k++) if(nome[k][0].compareTo(nome[k - 1][0]) < 0) { nomeb = new String[5]; for(int l = 0; l < 4; l++) { nomeb[l] = nome[k - 1][l]; nome[k - 1][l] = nome[k][l]; nome[k][l] = nomeb[l]; nomeb[l] = ""; } } } System.out.println(" "); System.out.println(" "); System.out.println(" "); System.out.println(" SCRITTURA ORDINATA ALFABETICAMENTE"); for(x = 0; x < 5; x++) { System.out.println(" "); for(j = 0; j < 4; j++) System.out.print(nome[x][j]); } } public static void main(String args[]) { Ordinamento ordinamento = new Ordinamento(); } int x; int j; String nome[][]; String nomeb[]; } |
![]() |
Passare valori tra pagine web |
function leggivariabile(variabile) { // variabile contiene il nome della variabile var indirizzo = window.location.toString() + "&"; // indirizzo contiene l'indirizzo della pagina nella forma // "indirizzo?variabile1=valore1&variabile2=valore2&variabile3=valore3&" var posizionenome=indirizzo.indexOf(variabile + "="); // trova la posizione in cui e' memorizzata la variabile if (posizionenome==-1) return ""; // se restituisce -1 la variabile non c'e'! :( var sottostringa=indirizzo.substring(posizionenome); // taglia tutto quello che c'e' prima della variabile var inizio=sottostringa.indexOf("="); // trova l'uguale dopo il quale si trova il valore var fine=sottostringa.indexOf("&"); // trova la & che indica dove finisce il valore var variabile=sottostringa.substring(inizio + 1, fine); // assegna a variabile il valore contenuto tra = e &! return variabile; } |
![]() |
Passare valori tra pagine web |
import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; import javax.swing.text.*; import javax.swing.text.rtf.*; /** * Titolo: RTFView * Descrizione: Visualizza RTF in pagine Web * @author Gianluca Esposito (giaesp@tiscali.it) * @version 0.11.17.03 */ public class RTFView extends JApplet { private boolean isStandalone = false; JScrollPane rtfScroller = new JScrollPane(); JTextPane rtfPane = new JTextPane(); String RTFText; int bground; public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } public RTFView() { } public void init() { try { RTFText = this.getParameter("RTF", "Nessuna informazione da visualizzare."); } catch (Exception e) { e.printStackTrace(); } try { bground = Integer.parseInt(this.getParameter("bground", "FFFFFF"), 16); } catch (Exception e) { e.printStackTrace(); } try { jbInit(); } catch (Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { // this.setSize(new Dimension(width,height)); rtfPane.setEditorKit(new RTFEditorKit()); rtfPane.setEditable(false); rtfPane.setCaretPosition(0); rtfPane.setText(RTFText); // Inserimento del testo RTF this.getContentPane().add(rtfScroller, BorderLayout.CENTER); rtfScroller.getViewport().add(rtfPane, null); rtfPane.setBackground(new Color(bground)); rtfScroller.setBackground(new Color(bground)); this.setVisible(true); } public void start() { } public void stop() { } public void destroy() { } public String getAppletInfo() { return "RTFView 0.11.17.03 (Gianluca Esposito - giaesp@tiscali.it)"; } public String[][] getParameterInfo() { String[][] pinfo = { { "RTF", "String", "Corpo dell'RTF"}, { "bground", "hex int", "Colore di sfondo - Inserire sequenza esadecimale (come nell'HTML, ma senza \"#\")"}, }; return pinfo; } static { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { } } } |