473,385 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1. listener.add("public void actionPerformed(ActionEvent event); \n  {");
  2.             for(int i = 0;  i< button.size(); i++){   
  3.             listener.add("if (event.getSource() == b"+i+")");
  4.             listener.add("{");
  5.                 //action events text:a dispose:f
  6.             for(int a = 0;  a< textcheckarray.size(); a++){
  7.                ts = textcheckarray.get(a);
  8.                    if (ts.equals("selected"))   //check box
  9.                    {
  10.  
  11.                       listener.add("textbox.setText(changetext)");
  12.  
  13.  
  14.  
  15.                     }
  16.  
  17.                 }
  18.             for(int f = 0;  f< disposecheckarray.size(); f++){
  19.                ds = disposecheckarray.get(f);
  20.                 if (ds.equals("selected"));   //checkbox
  21.                 {
  22.                     listener.add("f1.dispose()");
  23.  
  24.  
  25.                 }
  26.  
  27.  
  28.             } 
  29.             listener.add("");
  30.             listener.add("}");
  31.             }
  32.  
  33.  
  34.  
  35. this is the part that writes the file.
  36.  
  37.  try{
  38.         FileWriter outfile = new FileWriter("program.txt");
  39.         PrintWriter out = new PrintWriter(outfile);
  40.  
  41.         for(int i = 0;  i< importstatements.size(); i++)
  42.                out.println(importstatements.get(i));
  43.  
  44.         out.println(classname);
  45.         out.println("{");
  46.             for(int i = 0;  i< variables.size(); i++)
  47.                out.println(variables.get(i));
  48.  
  49.         out.println(constructor);
  50.             for(int i = 0;  i< const_statements.size(); i++)
  51.                out.println(const_statements.get(i));
  52.         out.println("}");
  53.              for(int i = 0;  i< listener.size(); i++)
  54.                out.println(listener.get(i));
  55.         out.println("}");
  56.         out.println("}");
  57.  
  58.         out.close();
example:
if i make 3 buttons and each of them has different choices of weather they dispose or change text this happens.

Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. public class fddffd implements ActionListener
  5. {
  6. JFrame f1;
  7. Container c1;
  8. JPanel p1;
  9. JButton b0;
  10. JButton b1;
  11. JButton b2;
  12. JButton b3;
  13. public fddffd() 
  14.  {
  15. f1 = new JFrame();
  16. f1.setSize(3443,3434)
  17. c1 = f1.getContentPane(); 
  18. p1 = new JPanel();
  19. b0 = new JButton(" dfdf ")
  20. b1 = new JButton(" fdfd ")
  21. b2 = new JButton(" fd ")
  22. b3 = new JButton(" ffdf ")
  23. b0.addActionListener(this);
  24. b1.addActionListener(this);
  25. b2.addActionListener(this);
  26. b3.addActionListener(this);
  27. p1.add(b0);
  28. p1.add(b1);
  29. p1.add(b2);
  30. p1.add(b3);
  31. c1.add(p1);
  32. f1.show();
  33. }
  34. public void actionPerformed(ActionEvent event); 
  35.   {
  36. if (event.getSource() == b0)
  37. {
  38. textbox.setText(changetext) // this one was spose to only have "f1.dispose"
  39. textbox.setText(changetext)
  40. textbox.setText(changetext)
  41. f1.dispose()
  42. f1.dispose()
  43.  
  44. }
  45. if (event.getSource() == b1)  // this one was only spose to change text
  46. {
  47. textbox.setText(changetext) 
  48. textbox.setText(changetext)
  49. textbox.setText(changetext)
  50. f1.dispose()
  51. f1.dispose()
  52.  
  53. }
  54. if (event.getSource() == b2) // this one was spose to do nothing
  55. {
  56. textbox.setText(changetext)
  57. textbox.setText(changetext)
  58. textbox.setText(changetext)
  59. f1.dispose()
  60. f1.dispose()
  61.  
  62. }
  63. if (event.getSource() == b3)
  64. {
  65. textbox.setText(changetext)
  66. textbox.setText(changetext)
  67. textbox.setText(changetext)
  68. f1.dispose()
  69. f1.dispose()
  70.  
  71. }
  72. }
  73. }
  74.  
what happens when you write it to a txt file, THATS the txt file that was written
Oct 21 '07 #1
4 1348
JosAH
11,448 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1.                ts = textcheckarray.get(a);
  2.                    if (ts.equals("selected"))   //check box
  3.  
  4. [ ... ]
  5.  
  6.                ds = disposecheckarray.get(f);
  7.                 if (ds.equals("selected"));   //checkbox
  8.  
  9. [ ... ]
  10.  
These tests seem to succeed for every 'ts' and 'ds'. Since I can't see the type
of 'textcheckarray' and 'disposecheckarray' I can't tell anything more.

kind regards,

Jos
Oct 21 '07 #2
Expand|Select|Wrap|Line Numbers
  1.         textcheckarray = new ArrayList<String>();
  2.         disposecheckarray = new ArrayList<String>();
this?
Oct 21 '07 #3
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!

Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.util.ArrayList;
  5. import java.io.*;
  6. import javax.swing.event.*;
  7. public class ClassWriter implements ActionListener
  8. {
  9.    ArrayList<String> importstatements;
  10.    ArrayList<String> variables;
  11.    ArrayList<String> const_statements;
  12.    ArrayList<String> listener;
  13.       ArrayList<String> button;
  14.         ArrayList<String> jlabel;
  15.         ArrayList<String> dispose;
  16.         ArrayList<String> addtext;
  17.         ArrayList<String> textcheckarray;
  18.         ArrayList<String> disposecheckarray;
  19.        JPanel p1;
  20.  
  21.        JButton b2, b3,bsubmit,jsubmit,addbutton,addjlabel;
  22.        JFrame f1;
  23.        JTextField t1,inbutton,inlabel;
  24.        JLabel l1;
  25.        Container c1;
  26.        String classname, constructor,btext,jtext,jXX,jYY,select,ts,ds;
  27.        boolean runb;
  28.        int i = 0;
  29.        int h = 0;
  30.        int a = 0;
  31.        int f = 0;
  32.        JRadioButton disposecheck,textcheck;
  33.        public ClassWriter(String cl,String jX,String jY,boolean runa)
  34.     {
  35.        disposecheck = new JRadioButton("Dispose GUI?");
  36.       disposecheck.setMnemonic(KeyEvent.VK_C);
  37.        disposecheck.setSelected(false);
  38.  
  39.   runb=runa;
  40.  
  41.         textcheck = new JRadioButton("Change Textbox?");
  42.         textcheck.setMnemonic(KeyEvent.VK_C); 
  43.         textcheck.setSelected(false);
  44.         jXX = jX;
  45.         jYY = jY;
  46.        //  ItemListener itemlistener = new ItemListener();
  47.         dispose = new ArrayList<String>();
  48.         addtext = new ArrayList<String>();
  49.        importstatements = new ArrayList<String>();
  50.        variables = new ArrayList<String>();
  51.        const_statements = new ArrayList<String>();
  52.        listener = new ArrayList<String>();
  53.         button = new ArrayList<String>();
  54.         jlabel = new ArrayList<String>(); 
  55.         textcheckarray = new ArrayList<String>();
  56.         disposecheckarray = new ArrayList<String>();
  57.         classname = "public class "+cl;
  58.         constructor = "public "+ cl +"() \n {";
  59.  
  60.           inbutton = new JTextField(20);
  61.        inlabel = new JTextField(20);
  62.        addbutton = new JButton("Add Button");
  63.        addbutton.addActionListener(this);
  64.        addjlabel = new JButton("Add JLabel");
  65.        addjlabel.addActionListener(this);
  66.        bsubmit = new JButton("K");
  67.        bsubmit.addActionListener(this);
  68.        jsubmit = new JButton("K");
  69.        jsubmit.addActionListener(this);
  70.  
  71.        f1 = new JFrame();
  72.        f1.setSize(300,150);
  73.        c1 = f1.getContentPane();
  74.        p1 = new JPanel();
  75.        l1 = new JLabel("Text in Button");
  76.        t1 = new JTextField(20);
  77.  
  78.        b2 = new JButton("Input Data");
  79.         b2.addActionListener(this); 
  80.        b3 = new JButton("Write Program");
  81.         b3.addActionListener(this);
  82.  
  83.       //  textcheck.addItemListener(itemlistener);
  84.       //  disposecheck.addItemListener(itemlistener);
  85.        p1.add(addbutton);
  86.        p1.add(addjlabel);
  87.        p1.add(b2);
  88.        c1.add(p1);
  89.        f1.show();
  90.  
  91. }  
  92.  
  93.  
  94.     public void actionPerformed(ActionEvent event)
  95.     {
  96.  
  97.  
  98.          if (event.getSource() == addbutton)
  99.         {
  100.          p1.remove(addjlabel);
  101.          p1.remove(addbutton);
  102.          p1.remove(b2);
  103.          p1.add(inbutton);
  104.          p1.add(disposecheck);
  105.          p1.add(textcheck);
  106.          p1.add(bsubmit);
  107.          f1.dispose(); 
  108.          f1.show();
  109.         } 
  110.         if (event.getSource() == bsubmit)
  111.         {
  112.  
  113.  
  114.             btext = inbutton.getText();
  115.             button.add(i,btext);
  116.            inbutton.setText(null);
  117.  
  118.            if (textcheck.isSelected()){
  119.  
  120.             select="selected";
  121.  
  122.             }
  123.             else{
  124.                 select="unselected";
  125.             }
  126.             textcheckarray.add(a,select);
  127.  
  128.             if (disposecheck.isSelected()){
  129.  
  130.             select="selected";
  131.  
  132.             }
  133.             else{
  134.                 select="unselected";
  135.             }
  136.             disposecheckarray.add(f,select);
  137.  
  138.  
  139.           p1.remove(disposecheck);
  140.          p1.remove(textcheck);
  141.            p1.remove(jsubmit);
  142.            p1.remove(inlabel);
  143.            p1.remove(bsubmit);
  144.            p1.remove(inbutton);
  145.            p1.add(addbutton);
  146.  
  147.          p1.add(addjlabel); 
  148.          p1.add(b2);
  149.          i++;
  150.          f1.dispose();
  151.          f1.show();
  152.  
  153.       }
  154.        if (event.getSource() == addjlabel)
  155.        {
  156.  
  157.            p1.remove(addbutton);
  158.            p1.remove(addjlabel);
  159.            p1.remove(b2);
  160.            p1.add(inlabel);
  161.            p1.add(jsubmit);
  162.            f1.dispose();
  163.            f1.show();
  164.        }
  165.        if (event.getSource() == jsubmit)
  166.        { 
  167.            jtext = inlabel.getText();
  168.            jlabel.add(h,jtext);
  169.            inlabel.setText(null);
  170.            inbutton.setText(null);
  171.            p1.remove(jsubmit);
  172.            p1.remove(inlabel);
  173.            p1.remove(bsubmit);
  174.            p1.remove(inbutton);
  175.            p1.add(addbutton);
  176.               p1.add(addjlabel);
  177.            p1.add(b2);
  178.  
  179.  
  180.         h++;
  181.         f1.dispose();
  182.          f1.show();
  183.        }
  184.  
  185.  
  186.        if (event.getSource() == b2)
  187.        {
  188.           importstatements.add("import javax.swing.*;");
  189.          importstatements.add("import java.awt.*;");
  190.          importstatements.add("import java.awt.event.*;");
  191.  
  192.  
  193.           classname = classname +" implements ActionListener";
  194.  
  195.          variables.add("JFrame f1;");
  196.          variables.add("Container c1;");
  197.          variables.add("JPanel p1;");
  198.           for(int i = 0;  i< button.size(); i++){  
  199.          variables.add("JButton b"+i+";");
  200.         }
  201.         for(int h = 0;  h< jlabel.size(); h++){  
  202.          variables.add("JLabel L"+h+";");
  203.         }
  204.  
  205.          const_statements.add("f1 = new JFrame();");
  206.          const_statements.add("f1.setSize("+jXX+","+jYY+")");
  207.          const_statements.add("c1 = f1.getContentPane(); ");
  208.          const_statements.add("p1 = new JPanel();");
  209.          for(int i = 0;  i< button.size(); i++){         
  210.          const_statements.add("b"+i+" = new JButton(\" "+ button.get(i) +" \")");
  211.         }
  212.         for(int h = 0;  h< jlabel.size(); h++){         
  213.          const_statements.add("L"+h+" = new JLabel(\" "+ jlabel.get(h) +" \")");
  214.         }
  215.           for(int i = 0;  i< button.size(); i++){  
  216.          const_statements.add("b"+i+".addActionListener(this);");
  217.         }
  218.  
  219.          for(int i = 0;  i< button.size(); i++){ 
  220.          const_statements.add("p1.add(b"+i+");");
  221.         }
  222.          for(int h = 0;  h< jlabel.size(); h++){ 
  223.          const_statements.add("p1.add(L"+h+");");
  224.         }
  225.          const_statements.add("c1.add(p1);");
  226.          const_statements.add("f1.show();");
  227.         listener.add("public void actionPerformed(ActionEvent event); \n  {");
  228.             for(int i = 0;  i< button.size(); i++){   
  229.             listener.add("if (event.getSource() == b"+i+")");
  230.             listener.add("{");
  231.                 //action events text:a dispose:f
  232.             for(int a = 0;  a< textcheckarray.size(); a++){
  233.                ts = textcheckarray.get(a);
  234.                    if (ts.equals("selected"))
  235.                    {
  236.  
  237.                       listener.add("textbox.setText(changetext)");
  238.  
  239.  
  240.  
  241.                     }
  242.  
  243.                 }
  244.             for(int f = 0;  f< disposecheckarray.size(); f++){
  245.                ds = disposecheckarray.get(f);
  246.                 if (ds.equals("selected"))
  247.                 {
  248.                     listener.add("f1.dispose()");
  249.  
  250.  
  251.                 }
  252.  
  253.  
  254.  
  255.             } 
  256.             listener.add("");
  257.             listener.add("}");
  258.             }
  259.  
  260.  
  261.            p1.remove(l1);
  262.            p1.remove(t1);
  263.            p1.remove(b2);
  264.           p1.remove(addbutton);
  265.          p1.remove(addjlabel); 
  266.         p1.add(b3);
  267.         f1.dispose();
  268.         f1.show();  
  269.  
  270.     }
  271.        if (event.getSource() == b3)
  272.        {
  273.         if (runb == true){
  274.            try{
  275.            FileWriter outfile = new FileWriter("runprogram.txt");
  276.         PrintWriter out = new PrintWriter(outfile);
  277.  
  278.        out.println("public class RunProgram");
  279.        out.println("{");
  280.         out.println("public static void main(String[] args)");
  281.         out.println("{");
  282.         out.println("");
  283.         out.println("}");
  284.         out.println("}");
  285.         out.close(); 
  286.         }
  287.        catch (IOException e){}
  288.        }
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.         try{
  298.         FileWriter outfile = new FileWriter("program.txt");
  299.         PrintWriter out = new PrintWriter(outfile);
  300.  
  301.         for(int i = 0;  i< importstatements.size(); i++)
  302.                out.println(importstatements.get(i));
  303.  
  304.         out.println(classname);
  305.         out.println("{");
  306.             for(int i = 0;  i< variables.size(); i++)
  307.                out.println(variables.get(i));
  308.  
  309.         out.println(constructor);
  310.             for(int i = 0;  i< const_statements.size(); i++)
  311.                out.println(const_statements.get(i));
  312.         out.println("}");
  313.              for(int i = 0;  i< listener.size(); i++)
  314.                out.println(listener.get(i));
  315.         out.println("}");
  316.         out.println("}");
  317.  
  318.         out.close();
  319.            p1.add(l1);
  320.            if (runb == true)
  321.            {
  322.                l1.setText("Your Program has been written...with run class");
  323.             }
  324.                else{
  325.            l1.setText("Your Program has been written...without run class");
  326.         }
  327.            p1.remove(b3);
  328.            f1.dispose();
  329.            f1.show();
  330.        }
  331.        catch (IOException e){}
  332.  
  333.        }
  334.  
  335.  
  336.  
  337. }
  338. }
Oct 21 '07 #4
JosAH
11,448 Expert 8TB
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.println() sprinkled in your code at tactical places can
be of much help; give it a try.

kind regards,

Jos
Oct 21 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: pythonUser_07 | last post by:
Is this the correct place to post a jython question? I posted the following in the jython group, but I figured I'd post here too: _________________________________________ I am assuming that...
8
by: ºa¤Ö | last post by:
I find a interesting question, and I cannot solve it @.@ If i want to insert unicode data, I need using recordset.addnew instead of using "insert into table" query or "stored procedure" All...
2
by: Dylan Phillips | last post by:
A strang error is occurring when I run the following code: SqlConnection c = new SqlConnection(); c.ConnectionString = "Initial Catalog=Northwind;user id=sa;password=kat1ie;Data Source=server";...
7
by: git_cs | last post by:
Hey, guys and gals Somedays ago, I had asked for the DES algorithm in C language. Although I have written the algorthim in C myself, I am facing a peculiar problem, which I hope some of u guys and...
7
by: Jax | last post by:
I have this code, behold: string message = "Are you sure? You will lose this customer forever. (Well you'll have re-create from scratch)"; string caption = "Delete"; MessageBoxButtons buttons...
12
by: Dino M. Buljubasic | last post by:
I have two applications App1 and App2. App1 is just checking for new version of App2. If it finds newer version of App2 it will download App1 AND App2. (both of them). That is what I am...
6
by: Claude Yih | last post by:
Hi, everyone. I noticed an interesting thing about fread() this afternoon. Well, I can't see why so I post this message in the hope of getting some explanation. Please help me. I wrote the...
26
by: v4vijayakumar | last post by:
Happened to see my old (last millennium) c code. Almost forgot that I wrote it . It is interesting. :-) int sqrt(int no) { int t; no = t * t; return t; }
5
by: Will Honea | last post by:
I've hit an interesting trap trying to migrate data off an OS/2 server running version 7.2 (fp14) over to 8.2 on Linux. Seems that one table has a column defined in the DDL as "BIGINT NOT NULL...
126
by: jacob navia | last post by:
Buffer overflows are a fact of life, and, more specifically, a fact of C. All is not lost however. In the book "Value Range Analysis of C programs" Axel Simon tries to establish a...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.