Interesting Question.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • carlos123
    New Member
    • Sep 2007
    • 53

    #1

    Interesting Question.

    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.

    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();
    example:
    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()
     
    }
    }
    }
    what happens when you write it to a txt file, THATS the txt file that was written
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by carlos123
    Code:
                   ts = textcheckarray.get(a);
                       if (ts.equals("selected"))   //check box
    
    [ ... ]
    
                   ds = disposecheckarray.get(f);
                    if (ds.equals("selected"));   //checkbox
    
    [ ... ]
    These tests seem to succeed for every 'ts' and 'ds'. Since I can't see the type
    of 'textcheckarray ' and 'disposecheckar ray' I can't tell anything more.

    kind regards,

    Jos

    Comment

    • carlos123
      New Member
      • Sep 2007
      • 53

      #3
      Code:
              textcheckarray = new ArrayList<String>();
              disposecheckarray = new ArrayList<String>();
      this?

      Comment

      • carlos123
        New Member
        • Sep 2007
        • 53

        #4
        i know this is annoying, but for the sake of giving you all the elements you need to solve my problem and the fact that i am confused, i will show you all my code. Thanks a lot. I really appreciate your help!

        Code:
        import javax.swing.*;
        import java.awt.*;
        import java.awt.event.*;
        import java.util.ArrayList;
        import java.io.*;
        import javax.swing.event.*;
        public class ClassWriter implements ActionListener
        {
           ArrayList<String> importstatements;
           ArrayList<String> variables;
           ArrayList<String> const_statements;
           ArrayList<String> listener;
              ArrayList<String> button;
                ArrayList<String> jlabel;
                ArrayList<String> dispose;
                ArrayList<String> addtext;
                ArrayList<String> textcheckarray;
                ArrayList<String> disposecheckarray;
               JPanel p1;
           
               JButton b2, b3,bsubmit,jsubmit,addbutton,addjlabel;
               JFrame f1;
               JTextField t1,inbutton,inlabel;
               JLabel l1;
               Container c1;
               String classname, constructor,btext,jtext,jXX,jYY,select,ts,ds;
               boolean runb;
               int i = 0;
               int h = 0;
               int a = 0;
               int f = 0;
               JRadioButton disposecheck,textcheck;
               public ClassWriter(String cl,String jX,String jY,boolean runa)
            {
               disposecheck = new JRadioButton("Dispose GUI?");
              disposecheck.setMnemonic(KeyEvent.VK_C);
               disposecheck.setSelected(false);
        
          runb=runa;
                
                textcheck = new JRadioButton("Change Textbox?");
                textcheck.setMnemonic(KeyEvent.VK_C); 
                textcheck.setSelected(false);
                jXX = jX;
                jYY = jY;
               //  ItemListener itemlistener = new ItemListener();
                dispose = new ArrayList<String>();
                addtext = new ArrayList<String>();
               importstatements = new ArrayList<String>();
               variables = new ArrayList<String>();
               const_statements = new ArrayList<String>();
               listener = new ArrayList<String>();
                button = new ArrayList<String>();
                jlabel = new ArrayList<String>(); 
                textcheckarray = new ArrayList<String>();
                disposecheckarray = new ArrayList<String>();
                classname = "public class "+cl;
                constructor = "public "+ cl +"() \n {";
               
                  inbutton = new JTextField(20);
               inlabel = new JTextField(20);
               addbutton = new JButton("Add Button");
               addbutton.addActionListener(this);
               addjlabel = new JButton("Add JLabel");
               addjlabel.addActionListener(this);
               bsubmit = new JButton("K");
               bsubmit.addActionListener(this);
               jsubmit = new JButton("K");
               jsubmit.addActionListener(this);
               
               f1 = new JFrame();
               f1.setSize(300,150);
               c1 = f1.getContentPane();
               p1 = new JPanel();
               l1 = new JLabel("Text in Button");
               t1 = new JTextField(20);
              
               b2 = new JButton("Input Data");
                b2.addActionListener(this); 
               b3 = new JButton("Write Program");
                b3.addActionListener(this);
            
              //  textcheck.addItemListener(itemlistener);
              //  disposecheck.addItemListener(itemlistener);
               p1.add(addbutton);
               p1.add(addjlabel);
               p1.add(b2);
               c1.add(p1);
               f1.show();
            
        }  
        
             
            public void actionPerformed(ActionEvent event)
            {
              
        
                 if (event.getSource() == addbutton)
                {
                 p1.remove(addjlabel);
                 p1.remove(addbutton);
                 p1.remove(b2);
                 p1.add(inbutton);
                 p1.add(disposecheck);
                 p1.add(textcheck);
                 p1.add(bsubmit);
                 f1.dispose(); 
                 f1.show();
                } 
                if (event.getSource() == bsubmit)
                {
                 
                
                    btext = inbutton.getText();
                    button.add(i,btext);
                   inbutton.setText(null);
                    
                   if (textcheck.isSelected()){
                    
                    select="selected";
                    
                    }
                    else{
                        select="unselected";
                    }
                    textcheckarray.add(a,select);
                    
                    if (disposecheck.isSelected()){
                    
                    select="selected";
                    
                    }
                    else{
                        select="unselected";
                    }
                    disposecheckarray.add(f,select);
        
              
                  p1.remove(disposecheck);
                 p1.remove(textcheck);
                   p1.remove(jsubmit);
                   p1.remove(inlabel);
                   p1.remove(bsubmit);
                   p1.remove(inbutton);
                   p1.add(addbutton);
                   
                 p1.add(addjlabel); 
                 p1.add(b2);
                 i++;
                 f1.dispose();
                 f1.show();
               
              }
               if (event.getSource() == addjlabel)
               {
                  
                   p1.remove(addbutton);
                   p1.remove(addjlabel);
                   p1.remove(b2);
                   p1.add(inlabel);
                   p1.add(jsubmit);
                   f1.dispose();
                   f1.show();
               }
               if (event.getSource() == jsubmit)
               { 
                   jtext = inlabel.getText();
                   jlabel.add(h,jtext);
                   inlabel.setText(null);
                   inbutton.setText(null);
                   p1.remove(jsubmit);
                   p1.remove(inlabel);
                   p1.remove(bsubmit);
                   p1.remove(inbutton);
                   p1.add(addbutton);
                      p1.add(addjlabel);
                   p1.add(b2);
                   
              
                h++;
                f1.dispose();
                 f1.show();
               }
               
                
               if (event.getSource() == b2)
               {
                  importstatements.add("import javax.swing.*;");
                 importstatements.add("import java.awt.*;");
                 importstatements.add("import java.awt.event.*;");
              
                 
                  classname = classname +" implements ActionListener";
                  
                 variables.add("JFrame f1;");
                 variables.add("Container c1;");
                 variables.add("JPanel p1;");
                  for(int i = 0;  i< button.size(); i++){  
                 variables.add("JButton b"+i+";");
                }
                for(int h = 0;  h< jlabel.size(); h++){  
                 variables.add("JLabel L"+h+";");
                }
                 
                 const_statements.add("f1 = new JFrame();");
                 const_statements.add("f1.setSize("+jXX+","+jYY+")");
                 const_statements.add("c1 = f1.getContentPane(); ");
                 const_statements.add("p1 = new JPanel();");
                 for(int i = 0;  i< button.size(); i++){         
                 const_statements.add("b"+i+" = new JButton(\" "+ button.get(i) +" \")");
                }
                for(int h = 0;  h< jlabel.size(); h++){         
                 const_statements.add("L"+h+" = new JLabel(\" "+ jlabel.get(h) +" \")");
                }
                  for(int i = 0;  i< button.size(); i++){  
                 const_statements.add("b"+i+".addActionListener(this);");
                }
                
                 for(int i = 0;  i< button.size(); i++){ 
                 const_statements.add("p1.add(b"+i+");");
                }
                 for(int h = 0;  h< jlabel.size(); h++){ 
                 const_statements.add("p1.add(L"+h+");");
                }
                 const_statements.add("c1.add(p1);");
                 const_statements.add("f1.show();");
                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"))
                           {
                               
                              listener.add("textbox.setText(changetext)");
                               
                               
                               
                            }
                            
                        }
                    for(int f = 0;  f< disposecheckarray.size(); f++){
                       ds = disposecheckarray.get(f);
                        if (ds.equals("selected"))
                        {
                            listener.add("f1.dispose()");
                            
                            
                        }
                      
                            
                     
                    } 
                    listener.add("");
                    listener.add("}");
                    }
                    
                
                   p1.remove(l1);
                   p1.remove(t1);
                   p1.remove(b2);
                  p1.remove(addbutton);
                 p1.remove(addjlabel); 
                p1.add(b3);
                f1.dispose();
                f1.show();  
                   
            }
               if (event.getSource() == b3)
               {
                if (runb == true){
                   try{
                   FileWriter outfile = new FileWriter("runprogram.txt");
                PrintWriter out = new PrintWriter(outfile);
                
               out.println("public class RunProgram");
               out.println("{");
                out.println("public static void main(String[] args)");
                out.println("{");
                out.println("");
                out.println("}");
                out.println("}");
                out.close(); 
                }
               catch (IOException e){}
               }
                   
                   
                   
                   
                   
                   
                   
                     
                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();
                   p1.add(l1);
                   if (runb == true)
                   {
                       l1.setText("Your Program has been written...with run class");
                    }
                       else{
                   l1.setText("Your Program has been written...without run class");
                }
                   p1.remove(b3);
                   f1.dispose();
                   f1.show();
               }
               catch (IOException e){}
                
               }
               
            
                
        }
        }

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Please don't do that; you just posted a whole lot of wallpaper for us to just find
          out what you couldn't find yourself? Please try to narrow it down as much as
          possible. Hint: System.out.prin tln() sprinkled in your code at tactical places can
          be of much help; give it a try.

          kind regards,

          Jos

          Comment

          Working...