dafcon
25-06-2014, 15:29
Salve a tutti ho un problema con un evento associato ad un bottone. Nella classe ApriVista ho un costruttore che riceve dei parametri da una classe Main (es File dove andare a leggere). All'interno di ApriVista costruisco l'interfaccia grafica e istanzio la classe LeggiArff che mi legge un file e mi ritorna la sua analisi come String. Quando premo sul pulsante ok della classe ApriVista viene ritornato come String l'analisi del file che devo aggiungere alla textArea. Il problema che riscontro e che ogni volta all'interno del metodo actionPerformed mi ritorna sempre lo stesso risultato, come se l'oggetto istanziato fosse sempre lo stesso. Di seguito vi posto il codice magari vi è più chiaro... qualcuno sa aiutarmi? Grazia in anticipo
package Vista;
import javax.swing.JPanel;
public class ApriVista extends JPanel {
private JTextField algoritmoT;
/**
* Create the panel.
*/
public ApriVista(String relazione,Enumeration attributi, Enumeration istanze, File file)
{
setLayout(new BorderLayout(0, 0));
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
add(tabbedPane, BorderLayout.CENTER);
try
{
this.brFile=new BufferedReader(new FileReader(file));
}
catch (FileNotFoundException e)
{
System.out.println("file non trovato");
}
//pannello file
JPanel filePanel = new JPanel();
filePanel.setBorder(new LineBorder(new Color(0, 0, 0)));
tabbedPane.addTab("File", null, filePanel, null);
filePanel.setLayout(null);
//pannello caratteristiche
JPanel caratteristichePanel = new JPanel();
caratteristichePanel.setLayout(null);
caratteristichePanel.setBorder(new TitledBorder(null, "caratteristiche", TitledBorder.LEADING, TitledBorder.TOP, null, null));
caratteristichePanel.setBounds(21, 29, 539, 89);
filePanel.add(caratteristichePanel);
//etichette
JLabel label = new JLabel("Relazione:");
label.setBounds(10, 22, 71, 24);
caratteristichePanel.add(label);
JLabel label_1 = new JLabel("Istanze: ");
label_1.setBounds(274, 54, 71, 24);
caratteristichePanel.add(label_1);
JLabel label_2 = new JLabel("Attributi:");
label_2.setBounds(10, 54, 71, 24);
caratteristichePanel.add(label_2);
//etichette da settare
JLabel relazioneT = new JLabel("");
relazioneT.setBounds(85, 22, 260, 24);
relazioneT.setText(relazione);
caratteristichePanel.add(relazioneT);
JLabel istanzeT = new JLabel("");
istanzeT.setBounds(349, 54, 146, 24);
caratteristichePanel.add(istanzeT);
JLabel attributiT = new JLabel("");
attributiT.setBounds(85, 54, 130, 24);
caratteristichePanel.add(attributiT);
JPanel arffPanel = new JPanel();
arffPanel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "File arff", TitledBorder.LEADING, TitledBorder.TOP, null, null));
arffPanel.setBounds(21, 158, 539, 289);
filePanel.add(arffPanel);
arffPanel.setLayout(new BorderLayout(0, 0));
//area di testo che legge dal file con JScrollPane
JTextArea testoArff = new JTextArea();
JScrollPane scroll=new JScrollPane(testoArff,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll.setLayout(new ScrollPaneLayout());
arffPanel.add(scroll, BorderLayout.CENTER);
int count=0;
while(attributi.hasMoreElements())
{
testoArff.append(attributi.nextElement().toString()+"\n");
count++;
}
attributiT.setText(String.valueOf(count));
count=0;
testoArff.append("@data \n");
while(istanze.hasMoreElements())
{
testoArff.append(istanze.nextElement().toString()+"\n");
count++;
}
istanzeT.setText(String.valueOf(count));
JPanel associativePanel = new JPanel();
associativePanel.setBorder(new LineBorder(new Color(0, 0, 0)));
tabbedPane.addTab("Regole associative", null, associativePanel, null);
associativePanel.setLayout(null);
JPanel regoleAssociative = new JPanel();
regoleAssociative.setLayout(null);
regoleAssociative.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Regole associative", TitledBorder.LEADING, TitledBorder.TOP, null, null));
regoleAssociative.setBounds(21, 29, 621, 57);
associativePanel.add(regoleAssociative);
algoritmoT = new JTextField();
algoritmoT.setBounds(146, 26, 465, 20);
regoleAssociative.add(algoritmoT);
algoritmoT.setColumns(10);
JComboBox comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(new String[] {"Apriori", "FilteredAssociator", "FPGrowth", "PredictiveApriori", "Tertius"}));
comboBox.setBounds(10, 26, 126, 20);
regoleAssociative.add(comboBox);
JPanel operazioniPanel = new JPanel();
operazioniPanel.setBorder(new TitledBorder(null, "Operazioni", TitledBorder.LEADING, TitledBorder.TOP, null, null));
operazioniPanel.setBounds(21, 97, 126, 381);
associativePanel.add(operazioniPanel);
operazioniPanel.setLayout(new BorderLayout(0, 0));
JScrollPane scrollOperazioni = new JScrollPane();
scrollOperazioni.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
operazioniPanel.add(scrollOperazioni, BorderLayout.CENTER);
JPanel outputAssociativoP = new JPanel();
outputAssociativoP.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Output associativo", TitledBorder.LEADING, TitledBorder.TOP, null, null));
outputAssociativoP.setBounds(160, 97, 554, 381);
associativePanel.add(outputAssociativoP);
outputAssociativoP.setLayout(new BorderLayout(0, 0));
JScrollPane scrollPane = new JScrollPane();
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setEnabled(true);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
outputAssociativoP.add(scrollPane, BorderLayout.CENTER);
final JTextArea textArea= new JTextArea();
textArea.setEditable(false);
scrollPane.setViewportView(textArea);//inserire qui
scroll.setLayout(new ScrollPaneLayout());
JButton startButton = new JButton("start");
startButton.setBounds(646, 52, 63, 23);
associativePanel.add(startButton);
//fin qui tutto ok; ora inizia la parte critica
leggi=new LeggiArff(file);
final String read=leggi.getAnalisi();
startButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
textArea.append(leggi.getAnalisi()); //stampa sempre la stessa stringa
textArea.append("\n");
}
int intero=0;
String read;
});
}
private BufferedReader brFile;
private int scelta;
private LeggiArff leggi;
}
package Modello;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import weka.associations.Apriori;
import weka.core.Instances;
public class LeggiArff
{
public LeggiArff(File file)
{
try
{
BufferedReader brFile=new BufferedReader(new FileReader(file));
System.out.println("leggi file:"+file);
Instances ist=new Instances(brFile);
Apriori apriori=new Apriori();
apriori.buildAssociations(ist);
istanza=apriori.toString();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public String getAnalisi()
{
return istanza;
}
private String istanza;
}
package Vista;
import javax.swing.JPanel;
public class ApriVista extends JPanel {
private JTextField algoritmoT;
/**
* Create the panel.
*/
public ApriVista(String relazione,Enumeration attributi, Enumeration istanze, File file)
{
setLayout(new BorderLayout(0, 0));
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
add(tabbedPane, BorderLayout.CENTER);
try
{
this.brFile=new BufferedReader(new FileReader(file));
}
catch (FileNotFoundException e)
{
System.out.println("file non trovato");
}
//pannello file
JPanel filePanel = new JPanel();
filePanel.setBorder(new LineBorder(new Color(0, 0, 0)));
tabbedPane.addTab("File", null, filePanel, null);
filePanel.setLayout(null);
//pannello caratteristiche
JPanel caratteristichePanel = new JPanel();
caratteristichePanel.setLayout(null);
caratteristichePanel.setBorder(new TitledBorder(null, "caratteristiche", TitledBorder.LEADING, TitledBorder.TOP, null, null));
caratteristichePanel.setBounds(21, 29, 539, 89);
filePanel.add(caratteristichePanel);
//etichette
JLabel label = new JLabel("Relazione:");
label.setBounds(10, 22, 71, 24);
caratteristichePanel.add(label);
JLabel label_1 = new JLabel("Istanze: ");
label_1.setBounds(274, 54, 71, 24);
caratteristichePanel.add(label_1);
JLabel label_2 = new JLabel("Attributi:");
label_2.setBounds(10, 54, 71, 24);
caratteristichePanel.add(label_2);
//etichette da settare
JLabel relazioneT = new JLabel("");
relazioneT.setBounds(85, 22, 260, 24);
relazioneT.setText(relazione);
caratteristichePanel.add(relazioneT);
JLabel istanzeT = new JLabel("");
istanzeT.setBounds(349, 54, 146, 24);
caratteristichePanel.add(istanzeT);
JLabel attributiT = new JLabel("");
attributiT.setBounds(85, 54, 130, 24);
caratteristichePanel.add(attributiT);
JPanel arffPanel = new JPanel();
arffPanel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "File arff", TitledBorder.LEADING, TitledBorder.TOP, null, null));
arffPanel.setBounds(21, 158, 539, 289);
filePanel.add(arffPanel);
arffPanel.setLayout(new BorderLayout(0, 0));
//area di testo che legge dal file con JScrollPane
JTextArea testoArff = new JTextArea();
JScrollPane scroll=new JScrollPane(testoArff,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll.setLayout(new ScrollPaneLayout());
arffPanel.add(scroll, BorderLayout.CENTER);
int count=0;
while(attributi.hasMoreElements())
{
testoArff.append(attributi.nextElement().toString()+"\n");
count++;
}
attributiT.setText(String.valueOf(count));
count=0;
testoArff.append("@data \n");
while(istanze.hasMoreElements())
{
testoArff.append(istanze.nextElement().toString()+"\n");
count++;
}
istanzeT.setText(String.valueOf(count));
JPanel associativePanel = new JPanel();
associativePanel.setBorder(new LineBorder(new Color(0, 0, 0)));
tabbedPane.addTab("Regole associative", null, associativePanel, null);
associativePanel.setLayout(null);
JPanel regoleAssociative = new JPanel();
regoleAssociative.setLayout(null);
regoleAssociative.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Regole associative", TitledBorder.LEADING, TitledBorder.TOP, null, null));
regoleAssociative.setBounds(21, 29, 621, 57);
associativePanel.add(regoleAssociative);
algoritmoT = new JTextField();
algoritmoT.setBounds(146, 26, 465, 20);
regoleAssociative.add(algoritmoT);
algoritmoT.setColumns(10);
JComboBox comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(new String[] {"Apriori", "FilteredAssociator", "FPGrowth", "PredictiveApriori", "Tertius"}));
comboBox.setBounds(10, 26, 126, 20);
regoleAssociative.add(comboBox);
JPanel operazioniPanel = new JPanel();
operazioniPanel.setBorder(new TitledBorder(null, "Operazioni", TitledBorder.LEADING, TitledBorder.TOP, null, null));
operazioniPanel.setBounds(21, 97, 126, 381);
associativePanel.add(operazioniPanel);
operazioniPanel.setLayout(new BorderLayout(0, 0));
JScrollPane scrollOperazioni = new JScrollPane();
scrollOperazioni.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
operazioniPanel.add(scrollOperazioni, BorderLayout.CENTER);
JPanel outputAssociativoP = new JPanel();
outputAssociativoP.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Output associativo", TitledBorder.LEADING, TitledBorder.TOP, null, null));
outputAssociativoP.setBounds(160, 97, 554, 381);
associativePanel.add(outputAssociativoP);
outputAssociativoP.setLayout(new BorderLayout(0, 0));
JScrollPane scrollPane = new JScrollPane();
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setEnabled(true);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
outputAssociativoP.add(scrollPane, BorderLayout.CENTER);
final JTextArea textArea= new JTextArea();
textArea.setEditable(false);
scrollPane.setViewportView(textArea);//inserire qui
scroll.setLayout(new ScrollPaneLayout());
JButton startButton = new JButton("start");
startButton.setBounds(646, 52, 63, 23);
associativePanel.add(startButton);
//fin qui tutto ok; ora inizia la parte critica
leggi=new LeggiArff(file);
final String read=leggi.getAnalisi();
startButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
textArea.append(leggi.getAnalisi()); //stampa sempre la stessa stringa
textArea.append("\n");
}
int intero=0;
String read;
});
}
private BufferedReader brFile;
private int scelta;
private LeggiArff leggi;
}
package Modello;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import weka.associations.Apriori;
import weka.core.Instances;
public class LeggiArff
{
public LeggiArff(File file)
{
try
{
BufferedReader brFile=new BufferedReader(new FileReader(file));
System.out.println("leggi file:"+file);
Instances ist=new Instances(brFile);
Apriori apriori=new Apriori();
apriori.buildAssociations(ist);
istanza=apriori.toString();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public String getAnalisi()
{
return istanza;
}
private String istanza;
}