We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, i'm trying to write data from JOptionPane to a txt file, but this method doesn't work. The Name of the Method is CreaProfilo(), i have to add every data i give frome JOptionPane; Thanks.
import g4p_controls.*;
import javax.swing.JOptionPane;
import java.io.FileWriter;
import java.io.BufferedWriter;
GPanel pnl, pnl2;
GButton NuovoProfilo, CaricaProfilo, MonitorazioneManuale, VisualizzaAllarmi,ClearText,EliminaProfilo;
GTextArea txaSample;
boolean notificheBool ;
String notifiche,profilo,Comando;
File f;
void setup() {
size(displayWidth/2, displayHeight/2);
frame.setTitle("Computer Monitoring Kinect");
// inizializzazione pannello di destra che è 2/3 dello schermo, pannello delle notifiche
pnl = new GPanel(this, width-(width*2/3), 0, width*2/3, height, "Notices");
pnl.setCollapsible(false);
pnl.setDraggable(false);
// inizializzazione pannello di sinistra che è il pannello dei bottoni
pnl2 = new GPanel(this, 0, 0, width-(width*2/3), height, "Commands");
pnl2.setCollapsible(false);
pnl2.setDraggable(false);
AggiungiBottoni();
Comando="Nome del profilo :";
notifiche="Nessuna nuova notifica";
notificheBool = true;
txaSample = new GTextArea(this, 8000, 1000, width*2/3,height, G4P.SCROLLBARS_BOTH | G4P.SCROLLBARS_AUTOHIDE);
f = new File(dataPath("C:/Users/Luca/Documents/Processing/VideoSorveglianza/Profili.txt"));
if(!f.exists()){
CreaProfilo();
}
}
void draw() {
if(notificheBool){
// inizializzazione notifiche, impostando la stringa notifiche che sarà cambiata ogni volta.
txaSample.setText(notifiche, 3100);
txaSample.setLocalColorScheme(1);
pnl.addControl(txaSample,0,20);
notificheBool=false;
}
}
void AggiungiBottoni(){
NuovoProfilo = new GButton(this, 0, 0, 150, 20, "Crea un Nuovo Profilo");
pnl2.addControl(NuovoProfilo, 0, 20);
CaricaProfilo = new GButton(this, 0, 0, 150, 20, "Carica un Profilo");
pnl2.addControl(CaricaProfilo, 0, 50);
MonitorazioneManuale = new GButton(this, 0, 0, 150, 20, "Monitorazione manuale");
pnl2.addControl(MonitorazioneManuale, 0, 170);
VisualizzaAllarmi = new GButton(this, 0, 0, 150, 20, "Visualizza Allarmi");
pnl2.addControl(VisualizzaAllarmi, 0, 110);
ClearText = new GButton(this, 0, 0, 150, 20, "Pulisci schermo");
pnl2.addControl(ClearText, 0, 140);
EliminaProfilo = new GButton(this, 0, 0, 150, 20, "Elimina Profilo");
pnl2.addControl(EliminaProfilo, 0, 80);
}
void handleButtonEvents(GButton button, GEvent event) {
if (button == NuovoProfilo){
CreaProfilo();
}
// else if (){}
}
void CreaProfilo(){
FileWriter output;
if(!f.exists()){
try{
profilo= JOptionPane.showInputDialog("Non esistono profili,Immettere il nome del nuovo Profilo :");
output = new FileWriter("Profili.txt");
output.write(profilo);
output.close();
}
catch (IOException e) {
println("It Broke");
e.printStackTrace();
}
}
else {
profilo= JOptionPane.showInputDialog(Comando);
String lines[] = loadStrings("Profili.txt");
for(int i=0;i<lines.length;i++){
if(lines[i].equals(profilo))
{
Comando="Esiste già un profilo con questo nome! Immettere nome profilo:";
CreaProfilo();
}
else{
try{
output = new FileWriter("Profili.txt");
output.write(profilo);
output.close();
Comando="Nome del profilo :";
println("ciao");
break;
}
catch (IOException e) {
println("It Broke");
e.printStackTrace();
}
}
}
}
}
Answers
What exactly do you mean when you say it doesn't work? Does it throw an Exception? Does it create the file in the wrong place? Does it put the wrong info in the file? Something else?
Please consider shortening your code to an MCVE instead of your whole project, as all the extra code makes it harder for us to help you.
this is the wrong method. This cannot create file :(
Ok i correct the method but now the problem is that the data is not added to the file.
Like I said, it's pretty hard to help without seeing an MCVE.
Try putting println() here and there, eg. to see what is in profilo, the value of i and lines[i], and so on.