crazy problem..help me.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vikassawant
    New Member
    • Mar 2009
    • 27

    crazy problem..help me.

    hi,
    My problem is some what crazy for me.

    Let's see,

    I declare one java file named as, test1.java.
    In which i designed required GUI for project i.e 8 combobox,two buttons,etc.

    Second file is automatic.java in which I get the selected values of combobox by creating the object of test1.java file.
    like
    test1 tst=new test();

    and access value of combobox like
    String cmb = tst.cmbv1;

    Same as above getting the values of combobox in third file and fourth file.

    but when I create the object of class test1,GUI for test1 is executed.

    And this is what the problem I faced i.e. 3 times GUI frame for test1 is displayed.

    Please tell me how to overcome this problem.Thanks in advance.

    Thanks,
    Vikas Sawant.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    So you must've created three JFrames, i.e. you have called the constructor for your GUI class three times. You didn't show us any code so we can't give you more details. A few System.out.prin tln() statements on strategic places would soon show you your mistake.

    kind regards,

    Jos

    Comment

    • vikassawant
      New Member
      • Mar 2009
      • 27

      #3
      hi,
      My code for test1.java is as fallow
      [code=java]
      import java.awt.Font;

      public class test1 extends JFrame implements ActionListener
      {

      JFrame frame;
      JLabel lbl= new JLabel();
      String[] offense = { "HR", "RBI", "SB", "AVG"};
      String[] pitching = { "W", "S", "ERA", "WHIP"};
      String[] cmbint={"0","10 ","20","30","40 ","50","60","70 ","80","90","10 0"};
      //String[] cmbhr2={"0","10 ","20","30","40 ","50","60","70 ","80","90","10 0"};
      String[] cmbfloat={"0.20 0","0.210","0.2 20","0.230","0. 240","0.250","0 .260","0.270"," 0.280","0.290", "0.300","0.310" ,"0.320","0.330 ","0.340","0.35 0"};
      String[] cmbws={"0","2", "4","6","8","10 ","12","14","16 ","18","20" };
      String[] cmbera = {"1.0","1.5","2 .0","2.5","3.0" ,"3.5","4.0","4 .5","5.0"};
      String[] cmbwhip = {"1.00","1.10", "1.20","1.30"," 1.40","1.50","1 .60","1.70","1. 80","1.90","2.0 0"};

      JButton create,cancel;
      JComboBox Oone,Otwo,Othre e,Ofour,Pone,Pt wo,Pthree,Pfour ;
      JComboBox Oone2,Otwo2,Oth ree2,Ofour2,Pon e2,Ptwo2,Pthree 2,Pfour2;
      public JComboBox hr1,hr2,rbi1,rb i2,sb1,sb2,avg1 ,avg2;
      public JComboBox s1,s2,w1,w2,era 1,era2,whip1,wh ip2;
      public JComboBox rbi11,rbi21;

      static String hrv1,hrv2,rbiv1 ,rbiv2,sbv1,sbv 2,avgv1,avgv2,w v1,wv2,sv1,sv2, erav1,erav2,whi pv1,whipv2;




      public test1()
      {

      frame = new JFrame("Order Of logic");
      frame.setSize(6 75,560);
      addWindowListen er(new WindowAdapter()
      {
      public void windowClosing(W indowEvent e)
      {
      System.exit(0);
      }
      });


      try
      {

      JPanel title = new JPanel();
      title.setLayout (new GridLayout(2,1, 5,30));
      JLabel OLabel = new JLabel("Offense :-",JLabel.CENTER );
      title.add(OLabe l);
      JLabel PLabel = new JLabel("Pitchin g :-",JLabel.CENTER );
      title.add(PLabe l);

      ///////////////////////// offense combo box

      JPanel o = new JPanel();
      o.setLayout(new GridLayout(1,4, 25,30));

      Oone = new JComboBox(offen se);
      o.add(Oone);

      Otwo = new JComboBox(offen se);
      o.add(Otwo);

      Othree = new JComboBox(offen se);
      o.add(Othree);

      Ofour = new JComboBox(offen se);
      o.add(Ofour);

      ///////////////////////////// pitching combo box

      JPanel p = new JPanel();
      p.setLayout(new GridLayout(1,4, 25,30));


      Pone = new JComboBox(pitch ing);
      p.add(Pone);

      Ptwo = new JComboBox(pitch ing);
      p.add(Ptwo);

      Pthree = new JComboBox(pitch ing);
      p.add(Pthree);

      Pfour = new JComboBox(pitch ing);
      p.add(Pfour);



      Oone.setSelecte dIndex(0);
      Otwo.setSelecte dIndex(1);
      Othree.setSelec tedIndex(2);
      Ofour.setSelect edIndex(3);

      Oone.addActionL istener(this);
      Otwo.addActionL istener(this);
      Othree.addActio nListener(this) ;
      Ofour.addAction Listener(this);

      Pone.setSelecte dIndex(0);
      Ptwo.setSelecte dIndex(1);
      Pthree.setSelec tedIndex(2);
      Pfour.setSelect edIndex(3);

      Pone.addActionL istener(this);
      Ptwo.addActionL istener(this);
      Pthree.addActio nListener(this) ;
      Pfour.addAction Listener(this);


      TitledBorder tcriteria;
      tcriteria = BorderFactory.c reateTitledBord er("Select Order Of Logic ");

      ////////////////////////////////// panel for buttons

      JPanel box= new JPanel();


      create=new JButton("Create Team");
      box.add(create) ;
      create.addActio nListener(this) ;

      cancel=new JButton("Cancel ");
      box.add(cancel) ;
      cancel.addActio nListener(this) ;




      /////////////////// Panel for order of Logic
      JPanel m= new JPanel();

      m.setLayout(new GridLayout(2,2, 30,20));
      m.add(o);
      m.add(p);

      JPanel mn = new JPanel();
      mn.setBorder(tc riteria);
      mn.add(BorderLa yout.WEST,title );
      mn.add(BorderLa yout.CENTER,m);



      ///////////////////////////////// arrangment at frame


      frame.add(Borde rLayout.NORTH,m n);

      frame.add(Borde rLayout.SOUTH,b ox);


      frame.setVisibl e(true);
      // frame.setResiza ble(false);


      }catch(Exceptio n e)
      {
      System.out.prin tln(e);
      }


      }

      public void actionPerformed (ActionEvent e)
      {

      try
      {

      if(e.getSource( )==cancel)
      {
      System.exit(0);
      }

      else if(e.getSource( )==create)
      {
      category[0] =(String)Oone.g etSelectedItem( );
      category[1] =(String)Otwo.g etSelectedItem( );
      category[2] =(String)Othree .getSelectedIte m();
      category[3] =(String)Ofour. getSelectedItem ();

      category[4] =(String)Pone.g etSelectedItem( );
      category[5] =(String)Ptwo.g etSelectedItem( );
      category[6] =(String)Pthree .getSelectedIte m();
      category[7] =(String)Pfour. getSelectedItem ();

      final Automatic1 automatic1 = new Automatic1(cate gory[0],category[1],category[2],category[3],category[4],category[5],category[6],category[7]);

      frame.setConten tPane(automatic 1);

      frame.setLocati on(250,150);
      frame.setSize(7 50,650);
      frame.setVisibl e(true);

      }

      }catch(Exceptio n e1)
      {
      System.out.prin tln("hi..."+e1) ;
      }

      }

      public static void main(String[] args)
      {
      test1 tst = new test1();

      }
      }
      [/code]







      This is the code for test1.java.
      when I wanna use the value of combo box in another file.I write code their as

      test1 tst = new test1();
      String s =tst.cmb1;

      and use above code in two files. So when i declare like this test1() constructor called and GUI is displayed. Which I don't want.

      I want only at first GUI displayed and when I clicked on button appropriate operation performed.but when I clicked on button and go for next class in which value of variables of test1 is accessed by object of test1 like given above. but because of this GUI displayed 3 times.

      How can I overcome this problem.?
      Please help me.


      Thanks,
      vikas sawant
      Last edited by r035198x; Mar 13 '09, 08:22 AM. Reason: added code tags

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by vikassawant
        This is the code for test1.java.
        when I wanna use the value of combo box in another file.I write code their as

        test1 tst = new test1();
        String s =tst.cmb1;

        and use above code in two files. So when i declare like this test1() constructor called and GUI is displayed. Which I don't want.

        I want only at first GUI displayed and when I clicked on button appropriate operation performed.but when I clicked on button and go for next class in which value of variables of test1 is accessed by object of test1 like given above. but because of this GUI displayed 3 times.

        How can I overcome this problem.?
        There's more trouble: your test1 class is a JFrame because it extends from it but it also has a JFrame because it has a member variable of that class. IMHO that's one JFrame too many.

        If you want another class to be able to use that (single) instance of your test1 class you should either pass it as a parameter to, say, the constructor of that other class or you should make that test1 object available in another way. That ugly little singleton pattern comes to mind.

        kind regards,

        Jos

        Comment

        Working...