10/03/16 18:05
splittik
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(
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(
import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.filechooser.FileFilter; public class FileChooser extends JPanel { private JTextArea textArea; private JButton openFileChooser; private JLabel statusbar1; private JButton openFileChooser2; private JLabel statusbar2; private JButton openFileChooser3; private JLabel statusbar3; private JButton openFileChooser4; private JLabel statusbar4; private JButton start; private JLabel statusbar5; private JButton save; public FileChooser() { super.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); createPanel(); } private void createPanel() { openFileChooser = new JButton("OpenFile"); statusbar1 = new JLabel(""); openFileChooser.addActionListener(new OpenFileChooser()); openFileChooser2 = new JButton("OpenFile"); statusbar2 = new JLabel(""); openFileChooser2.addActionListener(new OpenFileChooser()); openFileChooser3 = new JButton("OpenFile"); statusbar3 = new JLabel(""); openFileChooser3.addActionListener(new OpenFileChooser()); openFileChooser4 = new JButton("OpenFile"); statusbar4 = new JLabel(""); openFileChooser4.addActionListener(new OpenFileChooser()); start = new JButton("Start"); start.addActionListener(new OpenFileChooser()); statusbar5 = new JLabel(""); add(new JScrollPane(save)); add(new JScrollPane(openFileChooser)); add(new JScrollPane(statusbar1)); add(new JScrollPane(openFileChooser2)); add(new JScrollPane(statusbar2)); add(new JScrollPane(openFileChooser3)); add(new JScrollPane(statusbar3)); add(new JScrollPane(openFileChooser4)); add(new JScrollPane(statusbar4)); add(new JScrollPane(start)); add(new JScrollPane(statusbar5)); } private class OpenFileChooser implements ActionListener { String pathDynamic = "", nameprj = "", pathIndex = "", pathTopology = "", pathTra = ""; public void actionPerformed(ActionEvent e) { try { JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileFilter(new TxtFileFilter()); if (e.getSource() == openFileChooser) { int n = fileChooser.showOpenDialog(FileChooser.this); if (n == JFileChooser.APPROVE_OPTION) { File f = fileChooser.getSelectedFile(); String fname = f.getName().toLowerCase(); if (fname.endsWith("gro")) { statusbar1.setText("You chose..." + f.getName()); pathDynamic = f.getAbsolutePath(); } if (fname.endsWith("ndx")) { statusbar1.setText("You chose..." + f.getName()); pathIndex = f.getAbsolutePath(); } if (fname.endsWith("tpr")) { statusbar1.setText("You chose..." + f.getName()); pathTopology = f.getAbsolutePath(); } if (fname.endsWith("xtc")) { statusbar1.setText("You chose..." + f.getName()); pathTra = f.getAbsolutePath(); } } } if (e.getSource() == openFileChooser2) { int n = fileChooser.showOpenDialog(FileChooser.this); if (n == JFileChooser.APPROVE_OPTION) { File f = fileChooser.getSelectedFile(); String fname = f.getName().toLowerCase(); if (fname.endsWith("gro")) { statusbar2.setText("You chose..." + f.getName()); pathDynamic = f.getAbsolutePath(); } if (fname.endsWith("ndx")) { statusbar2.setText("You chose..." + f.getName()); pathIndex = f.getAbsolutePath(); } if (fname.endsWith("tpr")) { statusbar2.setText("You chose..." + f.getName()); pathTopology = f.getAbsolutePath(); } if (fname.endsWith("xtc")) { statusbar2.setText("You chose..." + f.getName()); pathTra = f.getAbsolutePath(); } } } if (e.getSource() == openFileChooser3) { int n = fileChooser.showOpenDialog(FileChooser.this); if (n == JFileChooser.APPROVE_OPTION) { File f = fileChooser.getSelectedFile(); String fname = f.getName().toLowerCase(); if (fname.endsWith("gro")) { statusbar3.setText("You chose..." + f.getName()); pathDynamic = f.getAbsolutePath(); } if (fname.endsWith("ndx")) { statusbar3.setText("You chose..." + f.getName()); pathIndex = f.getAbsolutePath(); } if (fname.endsWith("tpr")) { statusbar3.setText("You chose..." + f.getName()); pathTopology = f.getAbsolutePath(); } if (fname.endsWith("xtc")) { statusbar3.setText("You chose..." + f.getName()); pathTra = f.getAbsolutePath(); } } } if (e.getSource() == openFileChooser4) { int n = fileChooser.showOpenDialog(FileChooser.this); if (n == JFileChooser.APPROVE_OPTION) { File f = fileChooser.getSelectedFile(); String fname = f.getName().toLowerCase(); if (fname.endsWith("gro")) { statusbar4.setText("You chose..." + f.getName()); pathDynamic = f.getAbsolutePath(); } if (fname.endsWith("ndx")) { statusbar4.setText("You chose..." + f.getName()); pathIndex = f.getAbsolutePath(); } if (fname.endsWith("tpr")) { statusbar4.setText("You chose..." + f.getName()); pathTopology = f.getAbsolutePath(); } if (fname.endsWith("xtc")) { statusbar4.setText("You chose..." + f.getName()); pathTra = f.getAbsolutePath(); } } } if (e.getSource() == save) { //JFileChooser fileChooser = new JFileChooser(); int n = fileChooser.showSaveDialog(FileChooser.this); if (n == fileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); nameprj = file.getAbsolutePath(); } else { //log.append("Save command cancelled by user." + newline); } } if (e.getSource() == start) { try { if (pathDynamic.length() != 0 && pathIndex.length() != 0 && pathTopology.length() != 0 && pathTra.length() != 0) { System.out.println(nameprj+"\n"+pathDynamic+"\n"+pathIndex+"\n"+pathTopology+"\n"+pathTra); } else { statusbar5.setText("Complete all fields"); } } catch (Exception ed) { } } } catch (Exception s) { System.out.println(""); } } } private class TxtFileFilter extends FileFilter { public boolean accept(File file) { if (file.isDirectory()) { return true; } String fname = file.getName().toLowerCase(); return fname.endsWith("gro") || fname.endsWith("ndx") || fname.endsWith("tpr") || fname.endsWith("xtc"); } public String getDescription() { return "*.gro, *.ndx, *.tpr, *.xtc"; } } public static void main(String[] argv) { JFrame frame = new JFrame("JFileChooserDemo"); FileChooser demo = new FileChooser(); frame.getContentPane().add(demo); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 350); frame.setVisible(true); } }
aaa