Ok, let me explain my problem. I am making a program which writes a program in a .txt file. It is run by a GUI. For example if the user wants to add a button or a label they click the "add button" or "add jlabel" buttons and it will add it too an array and then you can finish the program, and in the text file it will be writen in java. One of the functions that you can choose for the button is too dispose of the jframe (f1.dispose();) Just check out the following code, this is a very unique assignment and its hard to explain.
this creates an array with all the stuff thats gunna be written to the txt file
there are 2 check boxes that you can select if you want to change text and/or dispose of the frame. i need a way to where if the user selects only one of the 2 choices in the code writen to the txt file it will ONLY be the choice that was selected, but with the code i have now it just writes them all. this is extreamly hard to explain, but thanks for checking this out.
example:
if i make 3 buttons and each of them has different choices of weather they dispose or change text this happens.
what happens when you write it to a txt file, THATS the txt file that was written
this creates an array with all the stuff thats gunna be written to the txt file
there are 2 check boxes that you can select if you want to change text and/or dispose of the frame. i need a way to where if the user selects only one of the 2 choices in the code writen to the txt file it will ONLY be the choice that was selected, but with the code i have now it just writes them all. this is extreamly hard to explain, but thanks for checking this out.
Code:
listener.add("public void actionPerformed(ActionEvent event); \n {");
for(int i = 0; i< button.size(); i++){
listener.add("if (event.getSource() == b"+i+")");
listener.add("{");
//action events text:a dispose:f
for(int a = 0; a< textcheckarray.size(); a++){
ts = textcheckarray.get(a);
if (ts.equals("selected")) //check box
{
listener.add("textbox.setText(changetext)");
}
}
for(int f = 0; f< disposecheckarray.size(); f++){
ds = disposecheckarray.get(f);
if (ds.equals("selected")); //checkbox
{
listener.add("f1.dispose()");
}
}
listener.add("");
listener.add("}");
}
this is the part that writes the file.
try{
FileWriter outfile = new FileWriter("program.txt");
PrintWriter out = new PrintWriter(outfile);
for(int i = 0; i< importstatements.size(); i++)
out.println(importstatements.get(i));
out.println(classname);
out.println("{");
for(int i = 0; i< variables.size(); i++)
out.println(variables.get(i));
out.println(constructor);
for(int i = 0; i< const_statements.size(); i++)
out.println(const_statements.get(i));
out.println("}");
for(int i = 0; i< listener.size(); i++)
out.println(listener.get(i));
out.println("}");
out.println("}");
out.close();
if i make 3 buttons and each of them has different choices of weather they dispose or change text this happens.
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class fddffd implements ActionListener
{
JFrame f1;
Container c1;
JPanel p1;
JButton b0;
JButton b1;
JButton b2;
JButton b3;
public fddffd()
{
f1 = new JFrame();
f1.setSize(3443,3434)
c1 = f1.getContentPane();
p1 = new JPanel();
b0 = new JButton(" dfdf ")
b1 = new JButton(" fdfd ")
b2 = new JButton(" fd ")
b3 = new JButton(" ffdf ")
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
p1.add(b0);
p1.add(b1);
p1.add(b2);
p1.add(b3);
c1.add(p1);
f1.show();
}
public void actionPerformed(ActionEvent event);
{
if (event.getSource() == b0)
{
textbox.setText(changetext) // this one was spose to only have "f1.dispose"
textbox.setText(changetext)
textbox.setText(changetext)
f1.dispose()
f1.dispose()
}
if (event.getSource() == b1) // this one was only spose to change text
{
textbox.setText(changetext)
textbox.setText(changetext)
textbox.setText(changetext)
f1.dispose()
f1.dispose()
}
if (event.getSource() == b2) // this one was spose to do nothing
{
textbox.setText(changetext)
textbox.setText(changetext)
textbox.setText(changetext)
f1.dispose()
f1.dispose()
}
if (event.getSource() == b3)
{
textbox.setText(changetext)
textbox.setText(changetext)
textbox.setText(changetext)
f1.dispose()
f1.dispose()
}
}
}
Comment