Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Java - Problema con ascoltatore actionPerformed javaGUI
Forum - Java - Problema con ascoltatore actionPerformed javaGUI

Avatar
splittik (Normal User)
Rookie


Messaggi: 27
Iscritto: 25/04/2012

Segnala al moderatore
Postato alle 19:05
Giovedì, 10/03/2016
Ciao... devo costruire una GUI con 5 bottoni ..ogni bottone attiva un filechooser che deve accettare solo particolri tipi di file... in base al tipo di file selezionato ne salva il path assoluto.

il filtro sul tipo di file è in private class TxtFileFilter extends FileFilter
la GUI è formata da 5 bottoni + relative etichette per far vedere il nome
del file selezionato.
Pero ho problemi sull'ascoltatore actionPerformed(ActionEvent e)
perche a seconda del file caricato ne salva il path in una variabile diversa.

Il tasto Start controlla che tutti i campi siano stati attivati alrimenti
stampa una stringa di errore..circa
Il mio problema è che il controllo del tasto start fallisce sempre ma
non capisco il perchè...hel plz X(

Codice sorgente - presumibilmente Java

  1. import java.awt.BorderLayout;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.io.BufferedReader;
  5. import java.io.BufferedWriter;
  6. import java.io.File;
  7. import java.io.FileReader;
  8. import java.io.FileWriter;
  9. import javax.swing.BoxLayout;
  10. import javax.swing.JButton;
  11. import javax.swing.JFileChooser;
  12. import javax.swing.JFrame;
  13. import javax.swing.JLabel;
  14. import javax.swing.JPanel;
  15. import javax.swing.JScrollPane;
  16. import javax.swing.JTextArea;
  17. import javax.swing.filechooser.FileFilter;
  18.  
  19. public class FileChooser extends JPanel {
  20.  
  21.     private JTextArea textArea;
  22.     private JButton openFileChooser;
  23.     private JLabel statusbar1;
  24.     private JButton openFileChooser2;
  25.     private JLabel statusbar2;
  26.     private JButton openFileChooser3;
  27.     private JLabel statusbar3;
  28.     private JButton openFileChooser4;
  29.     private JLabel statusbar4;
  30.     private JButton start;
  31.     private JLabel statusbar5;
  32.     private JButton save;
  33.  
  34.     public FileChooser() {
  35.         super.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
  36.         createPanel();
  37.     }
  38.  
  39.     private void createPanel() {
  40.  
  41.         openFileChooser = new JButton("OpenFile");
  42.         statusbar1 = new JLabel("");
  43.         openFileChooser.addActionListener(new OpenFileChooser());
  44.  
  45.         openFileChooser2 = new JButton("OpenFile");
  46.         statusbar2 = new JLabel("");
  47.         openFileChooser2.addActionListener(new OpenFileChooser());
  48.  
  49.         openFileChooser3 = new JButton("OpenFile");
  50.         statusbar3 = new JLabel("");
  51.         openFileChooser3.addActionListener(new OpenFileChooser());
  52.  
  53.         openFileChooser4 = new JButton("OpenFile");
  54.         statusbar4 = new JLabel("");
  55.         openFileChooser4.addActionListener(new OpenFileChooser());
  56.  
  57.         start = new JButton("Start");
  58.         start.addActionListener(new OpenFileChooser());
  59.         statusbar5 = new JLabel("");
  60.  
  61.  
  62.         add(new JScrollPane(save));
  63.         add(new JScrollPane(openFileChooser));
  64.         add(new JScrollPane(statusbar1));
  65.         add(new JScrollPane(openFileChooser2));
  66.         add(new JScrollPane(statusbar2));
  67.         add(new JScrollPane(openFileChooser3));
  68.         add(new JScrollPane(statusbar3));
  69.         add(new JScrollPane(openFileChooser4));
  70.         add(new JScrollPane(statusbar4));
  71.         add(new JScrollPane(start));
  72.         add(new JScrollPane(statusbar5));
  73.     }
  74.  
  75.     private class OpenFileChooser implements ActionListener {
  76.  
  77.         String pathDynamic = "", nameprj = "", pathIndex = "", pathTopology = "", pathTra = "";
  78.  
  79.         public void actionPerformed(ActionEvent e) {
  80.  
  81.             try {
  82.                 JFileChooser fileChooser = new JFileChooser();
  83.                 fileChooser.setFileFilter(new TxtFileFilter());
  84.  
  85.                 if (e.getSource() == openFileChooser) {
  86.                     int n = fileChooser.showOpenDialog(FileChooser.this);
  87.                     if (n == JFileChooser.APPROVE_OPTION) {
  88.                         File f = fileChooser.getSelectedFile();
  89.                         String fname = f.getName().toLowerCase();
  90.                         if (fname.endsWith("gro")) {
  91.                             statusbar1.setText("You chose..." + f.getName());
  92.                             pathDynamic = f.getAbsolutePath();
  93.                         }
  94.                         if (fname.endsWith("ndx")) {
  95.                             statusbar1.setText("You chose..." + f.getName());
  96.                             pathIndex = f.getAbsolutePath();
  97.                         }
  98.                         if (fname.endsWith("tpr")) {
  99.                             statusbar1.setText("You chose..." + f.getName());
  100.                             pathTopology = f.getAbsolutePath();
  101.                         }
  102.                         if (fname.endsWith("xtc")) {
  103.                             statusbar1.setText("You chose..." + f.getName());
  104.                             pathTra = f.getAbsolutePath();
  105.                         }
  106.                     }
  107.                 }
  108.                 if (e.getSource() == openFileChooser2) {
  109.                     int n = fileChooser.showOpenDialog(FileChooser.this);
  110.                     if (n == JFileChooser.APPROVE_OPTION) {
  111.                         File f = fileChooser.getSelectedFile();
  112.                         String fname = f.getName().toLowerCase();
  113.                         if (fname.endsWith("gro")) {
  114.                             statusbar2.setText("You chose..." + f.getName());
  115.                             pathDynamic = f.getAbsolutePath();
  116.                         }
  117.                         if (fname.endsWith("ndx")) {
  118.                             statusbar2.setText("You chose..." + f.getName());
  119.                             pathIndex = f.getAbsolutePath();
  120.                         }
  121.                         if (fname.endsWith("tpr")) {
  122.                             statusbar2.setText("You chose..." + f.getName());
  123.                             pathTopology = f.getAbsolutePath();
  124.                         }
  125.                         if (fname.endsWith("xtc")) {
  126.                             statusbar2.setText("You chose..." + f.getName());
  127.                             pathTra = f.getAbsolutePath();
  128.                         }
  129.                     }
  130.                 }
  131.                 if (e.getSource() == openFileChooser3) {
  132.                     int n = fileChooser.showOpenDialog(FileChooser.this);
  133.                     if (n == JFileChooser.APPROVE_OPTION) {
  134.                         File f = fileChooser.getSelectedFile();
  135.                         String fname = f.getName().toLowerCase();
  136.                         if (fname.endsWith("gro")) {
  137.                             statusbar3.setText("You chose..." + f.getName());
  138.                             pathDynamic = f.getAbsolutePath();
  139.                         }
  140.                         if (fname.endsWith("ndx")) {
  141.                             statusbar3.setText("You chose..." + f.getName());
  142.                             pathIndex = f.getAbsolutePath();
  143.                         }
  144.                         if (fname.endsWith("tpr")) {
  145.                             statusbar3.setText("You chose..." + f.getName());
  146.                             pathTopology = f.getAbsolutePath();
  147.                         }
  148.                         if (fname.endsWith("xtc")) {
  149.                             statusbar3.setText("You chose..." + f.getName());
  150.                             pathTra = f.getAbsolutePath();
  151.                         }
  152.                     }
  153.                 }
  154.                 if (e.getSource() == openFileChooser4) {
  155.                     int n = fileChooser.showOpenDialog(FileChooser.this);
  156.                     if (n == JFileChooser.APPROVE_OPTION) {
  157.                         File f = fileChooser.getSelectedFile();
  158.                         String fname = f.getName().toLowerCase();
  159.                         if (fname.endsWith("gro")) {
  160.                             statusbar4.setText("You chose..." + f.getName());
  161.                             pathDynamic = f.getAbsolutePath();
  162.                         }
  163.                         if (fname.endsWith("ndx")) {
  164.                             statusbar4.setText("You chose..." + f.getName());
  165.                             pathIndex = f.getAbsolutePath();
  166.                         }
  167.                         if (fname.endsWith("tpr")) {
  168.                             statusbar4.setText("You chose..." + f.getName());
  169.                             pathTopology = f.getAbsolutePath();
  170.                         }
  171.                         if (fname.endsWith("xtc")) {
  172.                             statusbar4.setText("You chose..." + f.getName());
  173.                             pathTra = f.getAbsolutePath();
  174.                         }
  175.                     }
  176.                 }
  177.  
  178.                 if (e.getSource() == save) {
  179.                     //JFileChooser fileChooser = new JFileChooser();
  180.                     int n = fileChooser.showSaveDialog(FileChooser.this);
  181.                     if (n == fileChooser.APPROVE_OPTION) {
  182.                         File file = fileChooser.getSelectedFile();
  183.                         nameprj = file.getAbsolutePath();
  184.                     } else {
  185.                         //log.append("Save command cancelled by user." + newline);
  186.                     }
  187.                 }
  188.  
  189.                 if (e.getSource() == start) {
  190.                     try {
  191.  
  192.                         if (pathDynamic.length() != 0 && pathIndex.length() != 0 && pathTopology.length() != 0 && pathTra.length() != 0) {
  193.                             System.out.println(nameprj+"\n"+pathDynamic+"\n"+pathIndex+"\n"+pathTopology+"\n"+pathTra);
  194.                         } else {
  195.                             statusbar5.setText("Complete all fields");
  196.                         }
  197.                     } catch (Exception ed) {
  198.  
  199.                     }
  200.                 }
  201.             } catch (Exception s) {
  202.                 System.out.println("");
  203.             }
  204.  
  205.         }
  206.     }
  207.  
  208.     private class TxtFileFilter extends FileFilter {
  209.  
  210.         public boolean accept(File file) {
  211.             if (file.isDirectory()) {
  212.                 return true;
  213.             }
  214.             String fname = file.getName().toLowerCase();
  215.  
  216.             return fname.endsWith("gro") || fname.endsWith("ndx") || fname.endsWith("tpr") || fname.endsWith("xtc");
  217.         }
  218.  
  219.         public String getDescription() {
  220.             return "*.gro, *.ndx, *.tpr, *.xtc";
  221.         }
  222.     }
  223.  
  224.     public static void main(String[] argv) {
  225.         JFrame frame = new JFrame("JFileChooserDemo");
  226.         FileChooser demo = new FileChooser();
  227.         frame.getContentPane().add(demo);
  228.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  229.         frame.setSize(300, 350);
  230.         frame.setVisible(true);
  231.     }
  232. }


PM Quote
Avatar
pierotofy (Admin)
Guru^2


Messaggi: 6230
Iscritto: 04/12/2003

Segnala al moderatore
Postato alle 14:36
Mercoledì, 16/03/2016
Il problema è che stai creando 5 diversi OpenFileChooser(); di conseguenza ogni bottone ha la propria copia di pathDynamic, nameprj, ecc.

Crea un singolo OpenFileChooser e passalo come argomento a tutti e 5 i bottoni.


Il mio blog: https://piero.dev
PM Quote
Avatar
splittik (Normal User)
Rookie


Messaggi: 27
Iscritto: 25/04/2012

Segnala al moderatore
Postato alle 19:42
Martedì, 22/03/2016
Intanto Grazie per la rispostaXDXDXD !!!!
non so perchè...pero funziona anche cosi
non credo sia la soluzione più bella da vedere,
però almeno qualcosa fa X(

Senza che leggi tutto in questa versione ho un frame con dentro:

-primo bottone con etichetta "Name Prj" che mi serve a prendere
il percorso dove buttare tutti i file di output (il percosro servirà per creare una catella).
Ha un filtro che disabilita la tendina "tipo file" nel file chooser

-secondo bottone con etichetta "Open File" serve a prendere il path di file
con estrensione .gro

-terzo bottone con etichetta "Open File" serve a prendere il path di file
con estrensione .ndx. Una volta selezionato il file lo legge e in base all'output
del comado grep riempie due jcomboBox. Con relativo ascoltatore per prendere
i 2 l'indici selezioanti. NB la ricerca sul file .ndx ho provato a farlo con le regex
ma non ci sono riuscito...

-terzo bottone secondo bottone con etichetta "Open File" serve a prendere il path di file
con estrensione .tpr

-quarto  bottone con etichetta "Open File" serve a prendere il path di file
con estrensione .xtc

-quinto bottone.. controlla che i campi siano stati completati e usa metodi della
classe Support che io uso solo come un contenitore di metodi...

NB ogni bottone collegato a filechooser presenta anche una
Jlabel che mostra il file selezionato

Visto che sono parecchio scarso a programmare e che ci ho
messo quasi una settimana per sta GUI sono abbastanza
soddisfatto di come funziona..però vorrei chiederti consiglio
su un altra cosa: alla pressione del tasto start parte una computazione
che generalemte dura 2 o 3 min...in questo periodo io vedo le scritte alla
matrrix sullo standard output pero in quel periodo la gui è "ferma".
Ho provato a far in modo che alla pressione del tasto start apparisse una
jtext area a cui è reidirizzato quello che vedo da terminale...pero l'aggiornameto
di questa text area avviene a computazione finita e quinidi non mi serve a nulla.

Visto che per me questa soluzione è un po troppo difficile potresti
consigliarmi qualcosa X(


Codice sorgente - presumibilmente Java

  1. import java.awt.BorderLayout;
  2. import java.awt.EventQueue;
  3. import java.awt.Insets;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.io.BufferedReader;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.io.InputStreamReader;
  10. import java.io.OutputStream;
  11. import java.io.PipedOutputStream;
  12. import java.io.PrintStream;
  13. import java.util.Hashtable;
  14. import java.util.LinkedList;
  15. import java.util.logging.Level;
  16. import java.util.logging.Logger;
  17. import javax.swing.BoxLayout;
  18. import javax.swing.JButton;
  19. import javax.swing.JComboBox;
  20. import javax.swing.JFileChooser;
  21. import javax.swing.JFrame;
  22. import javax.swing.JLabel;
  23. import javax.swing.JOptionPane;
  24. import javax.swing.JPanel;
  25. import javax.swing.JScrollPane;
  26. import javax.swing.JTextArea;
  27. import javax.swing.UIManager;
  28. import javax.swing.UnsupportedLookAndFeelException;
  29. import javax.swing.filechooser.FileFilter;
  30.  
  31. public class FileChooser extends JPanel {
  32.  
  33.     // GUI component
  34.     private JButton openGro;
  35.     private JLabel statusbarGro;
  36.     private JButton openNdx;
  37.     private JLabel statusbarNdx;
  38.     private JButton openTpr;
  39.     private JLabel statusbarTpr;
  40.     private JButton openXtc;
  41.     private JLabel statusbarXtc;
  42.     private JButton start;
  43.     private JButton save;
  44.     private JLabel statusbar;
  45.     static Hashtable map;
  46.     private JComboBox cb1;
  47.     private JComboBox cb2;
  48.     JPanel b = new JPanel();
  49.     private JTextArea textArea;
  50.  
  51.     // structure file result of molecular dynamic
  52.     String pathDynamic = "";
  53.  
  54.     // project name (folder that contains all result file)
  55.     String nameprj = "";
  56.  
  57.     // absolute path of index file
  58.     String pathIndex = "";
  59.  
  60.     // absolute path of topology file (.tpr)
  61.     String pathTopology = "";
  62.  
  63.     // absolute path of trajectories (.xtc)
  64.     String pathTra = "";
  65.  
  66.     // selected group
  67.     String group1 = "", group2 = "";
  68.  
  69.     public FileChooser(
  70.             String pathDynamic,
  71.             String nameprj,
  72.             String pathIndex,
  73.             String pathTopology,
  74.             String pathTra) {
  75.         super.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
  76.         this.nameprj = nameprj;
  77.         this.pathIndex = pathIndex;
  78.         this.pathTopology = pathTopology;
  79.         this.pathTra = pathTra;
  80.         createPanel();
  81.     }
  82.  
  83.     public void createPanel() {
  84.         openGro = new JButton("OpenFile");
  85.         statusbarGro = new JLabel("");
  86.         openGro.addActionListener(new OpenFileChooser());
  87.         openNdx = new JButton("OpenFile");
  88.         statusbarNdx = new JLabel("");
  89.         openNdx.addActionListener(new OpenFileChooser());
  90.         openTpr = new JButton("OpenFile");
  91.         statusbarTpr = new JLabel("");
  92.         openTpr.addActionListener(new OpenFileChooser());
  93.         openXtc = new JButton("OpenFile");
  94.         statusbarXtc = new JLabel("");
  95.         openXtc.addActionListener(new OpenFileChooser());
  96.         start = new JButton("Start");
  97.         start.addActionListener(new OpenFileChooser());
  98.         statusbar = new JLabel("");
  99.         save = new JButton("Name Prj");
  100.         save.addActionListener(new OpenFileChooser());
  101.  
  102.         add(new JScrollPane(save));
  103.         add(new JScrollPane(openGro));
  104.         add(new JScrollPane(statusbarGro));
  105.         add(new JScrollPane(openNdx));
  106.         add(new JScrollPane(statusbarNdx));
  107.         add(new JScrollPane(b));
  108.         add(new JScrollPane(openTpr));
  109.         add(new JScrollPane(statusbarTpr));
  110.         add(new JScrollPane(openXtc));
  111.         add(new JScrollPane(statusbarXtc));
  112.         add(new JScrollPane(start));
  113.         add(new JScrollPane(statusbar));
  114.     }
  115.  
  116.     public class cbListener implements ActionListener {
  117.  
  118.         public void actionPerformed(ActionEvent e) {
  119.             if (e.getSource() == cb1) {
  120.                 JComboBox jcb = (JComboBox) e.getSource();
  121.                 group1 = String.valueOf(jcb.getSelectedIndex());
  122.                 System.out.println("group1 " + group1);
  123.             }
  124.             if (e.getSource() == cb2) {
  125.                 JComboBox jcb = (JComboBox) e.getSource();
  126.                 group2 = String.valueOf(jcb.getSelectedIndex());
  127.                 System.out.println("group2 " + group2);
  128.             }
  129.         }
  130.     }
  131.  
  132.     private class OpenFileChooser implements ActionListener {
  133.  
  134.         //private Object ServiceManager;
  135.         public void actionPerformed(ActionEvent e) {
  136.             try {
  137.                 if (e.getSource() == openGro) {
  138.                     JFileChooser fileChooser = new JFileChooser();
  139.                     fileChooser.setFileFilter(new GroFileFilter());
  140.                     int n = fileChooser.showOpenDialog(FileChooser.this);
  141.                     if (n == JFileChooser.APPROVE_OPTION) {
  142.                         File f = fileChooser.getSelectedFile();
  143.                         statusbarGro.setText("You chose..." + f.getName());
  144.                         pathDynamic = f.getAbsolutePath();
  145.                     }
  146.                 }
  147.                 if (e.getSource() == openNdx) {
  148.                     String line = "";
  149.                     JFileChooser fileChooser = new JFileChooser();
  150.                     fileChooser.setFileFilter(new NdxFileFilter());
  151.                     int n = fileChooser.showOpenDialog(FileChooser.this);
  152.                     if (n == JFileChooser.APPROVE_OPTION) {
  153.                         File f = fileChooser.getSelectedFile();
  154.  
  155.                         statusbarNdx.setText("You chose..." + f.getName());
  156.                         pathIndex = f.getAbsolutePath();
  157.  
  158.                         String[] cmd = {"/bin/sh", "-c", " grep '\\[' " + f.getAbsolutePath(),};
  159.                         Process p = Runtime.getRuntime().exec(cmd);
  160.                         p.waitFor();
  161.  
  162.                         BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
  163.                         LinkedList l = new LinkedList();
  164.                         while ((line = in.readLine()) != null) {
  165.                             l.add(line);
  166.                         }
  167.                         String[] array = (String[]) l.toArray(new String[l.size()]);
  168.  
  169.                         cb1 = new JComboBox(array);
  170.                         cb1.addActionListener(new cbListener());
  171.                         b.add(cb1);
  172.                         cb2 = new JComboBox(array);
  173.                         cb2.addActionListener(new cbListener());
  174.                         b.add(cb2);
  175.                         revalidate();
  176.                     }
  177.                 }
  178.  
  179.                 if (e.getSource() == openTpr) {
  180.                     JFileChooser fileChooser = new JFileChooser();
  181.                     fileChooser.setFileFilter(new TprFileFilter());
  182.                     int n = fileChooser.showOpenDialog(FileChooser.this);
  183.                     if (n == JFileChooser.APPROVE_OPTION) {
  184.                         File f = fileChooser.getSelectedFile();
  185.                         statusbarTpr.setText("You chose..." + f.getName());
  186.                         pathTopology = f.getAbsolutePath();
  187.                     }
  188.                 }
  189.                 if (e.getSource() == openXtc) {
  190.                     JFileChooser fileChooser = new JFileChooser();
  191.                     fileChooser.setFileFilter(new XtcFileFilter());
  192.                     int n = fileChooser.showOpenDialog(FileChooser.this);
  193.                     if (n == JFileChooser.APPROVE_OPTION) {
  194.                         File f = fileChooser.getSelectedFile();
  195.                         statusbarXtc.setText("You chose..." + f.getName());
  196.                         pathTra = f.getAbsolutePath();
  197.                     }
  198.                 }
  199.                 if (e.getSource() == save) {
  200.                     JFileChooser fileChooser = new JFileChooser();
  201.                     fileChooser.setAcceptAllFileFilterUsed(false);
  202.                     //fileChooser.setFileFilter(new PrjFileFilter());
  203.                     int returnVal = fileChooser.showSaveDialog(FileChooser.this);
  204.                     if (returnVal == JFileChooser.APPROVE_OPTION) {
  205.                         File file = fileChooser.getSelectedFile();
  206.                         //This is where a real application would save the file.
  207.                         //log.append("Saving: " + file.getName() + "." + newline);
  208.                         nameprj = file.getAbsolutePath();
  209.                         System.out.println("Saving: " + file.getAbsolutePath());
  210.                     } else {
  211.                         //log.append("Save command cancelled by user." + newline);
  212.                     }
  213.                 }
  214.                 if (e.getSource() == start) {
  215.                     if (pathDynamic.length() != 0
  216.                             && nameprj.length() != 0
  217.                             && pathIndex.length() != 0
  218.                             && pathTopology.length() != 0
  219.                             && pathTra.length() != 0
  220.                             && group1.length() != 0
  221.                             && group2.length() != 0) {
  222.                        
  223.                         MindistCall a = null;
  224.                         a = new MindistCall(nameprj, pathTra, pathTopology, pathIndex, group1, group2);
  225.  
  226.                         LinkedList[] d = null;
  227.  
  228.                         d = Support.listTimeDist(a.getmindist(), 0.7000);
  229.  
  230.                         Object[] s = null;
  231.                         s = Support.joinInfo(d, a.getatom(), a.getmindist());
  232.  
  233.                         Hashtable Couple = (Hashtable) s[0];
  234.                         Hashtable P1 = (Hashtable) s[1];
  235.                         Hashtable P2 = (Hashtable) s[2];
  236.  
  237.                         Hashtable occP1 = Support.statistics(P1);
  238.                         Hashtable occP2 = Support.statistics(P2);
  239.  
  240.                         PieChart v = new PieChart(occP1, nameprj, "occP1");
  241.                         PieChart v1 = new PieChart(occP2, nameprj, "occP2");
  242.  
  243.                         Support.print(P1, a.getNamePrj(), "P1", occP1);
  244.                         Support.print(P2, a.getNamePrj(), "P2", occP2);
  245.                         Support.print(a.getNamePrj(), "Couple", Couple);
  246.                     }
  247.                 } else {
  248.                     statusbar.setText("fill all field");
  249.                 }
  250.             } catch (Exception s) {
  251.                 System.out.println(s);
  252.             }
  253.         }
  254.     }
  255.  
  256.     private class GroFileFilter extends FileFilter {
  257.  
  258.         public boolean accept(File file) {
  259.             if (file.isDirectory()) {
  260.                 return true;
  261.             }
  262.             String fname = file.getName().toLowerCase();
  263.             return fname.endsWith("gro");
  264.         }
  265.  
  266.         public String getDescription() {
  267.             return "*.gro";
  268.         }
  269.     }
  270.  
  271.     private class NdxFileFilter extends FileFilter {
  272.  
  273.         public boolean accept(File file) {
  274.             if (file.isDirectory()) {
  275.                 return true;
  276.             }
  277.             String fname = file.getName().toLowerCase();
  278.             return fname.endsWith("ndx");
  279.         }
  280.  
  281.         public String getDescription() {
  282.             return "*.ndx";
  283.         }
  284.     }
  285.  
  286.     private class TprFileFilter extends FileFilter {
  287.  
  288.         public boolean accept(File file) {
  289.             if (file.isDirectory()) {
  290.                 return true;
  291.             }
  292.             String fname = file.getName().toLowerCase();
  293.             return fname.endsWith("tpr");
  294.         }
  295.  
  296.         public String getDescription() {
  297.             return "*.tpr";
  298.         }
  299.     }
  300.  
  301.     private class XtcFileFilter extends FileFilter {
  302.  
  303.         public boolean accept(File file) {
  304.             if (file.isDirectory()) {
  305.                 return true;
  306.             }
  307.             String fname = file.getName().toLowerCase();
  308.             return fname.endsWith("xtc");
  309.         }
  310.  
  311.         public String getDescription() {
  312.             return "*.xtc";
  313.         }
  314.     }
  315.  
  316.     public static void main(String[] argv) {
  317.         JFrame frame = new JFrame("JFileChooserDemo");
  318.         FileChooser demo = new FileChooser();
  319.         frame.getContentPane().add(demo);
  320.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  321.         frame.setSize(330, 360);
  322.         frame.setVisible(true);
  323.     }
  324.  
  325. }


PM Quote
Avatar
pierotofy (Admin)
Guru^2


Messaggi: 6230
Iscritto: 04/12/2003

Segnala al moderatore
Postato alle 15:39
Mercoledì, 23/03/2016
La GUI è "ferma" perchè il lavoro della computazione blocca il thread principale.

Per "sbloccare" la GUI devi eseguire il lavoro della computazione su un thread separato. Dato il fatto che non hai molta padronanza del linguaggio, ti consiglio di leggere un manuale con le basi del linguaggio prima di proseguire.


Il mio blog: https://piero.dev
PM Quote