Results 1 to 16 of 16
  1. #1
    Mokomi is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Default creating unknown number of JFrame with an unknown number of JComboBox in each JFrame

    hello, i'm working on perfecting my dnd style game from excel into java. right now i'm working on the separate party windows.
    right now i'm using a simple program with while loops to create each party and add the proper ActionListeners. Right now it correctly adds the correct number of JComboBox and JLabels (which is a JTextField in my code i'm going to show you) but only for the first JFrame. the second and beyond does not have any JComboBox nor JTextFields.
    current code
    Java Code:
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.lang.reflect.Type;
    import java.util.Locale;
    import static java.lang.System.out;
    import static java.lang.System.err;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.util.Arrays;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.AdjustmentEvent;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Scanner;
    import java.util.Scanner;
     
    import javax.swing.*;
     
    public class bucky  extends JFrame{
        
    	public static void main(String[] args){
    		//all the members in a single String[]
    		String[] AllPossibleMembers= {null,"Olivia","Roton","Presti","Zren","demi"};
    		String ConveringString="1"; 	//for String conversion.
    		boolean IsPartyComplete =false;
           	int NumOfParties =0; 	//counter for the parties    
    		int NumOfMembers = 0;	//counter for the members
    	       
    		//makes the total number of parties. for this example it is 2 seperate parties.
            String[] parties = new String[2];
            //with a maximum of 3 members in each party. this is for showing you reasons.
            String[] member = new String[3];
            
    		while(NumOfParties<parties.length)//starts the while loop for each party.
    	       	{
    				//the NumOfParties to match up the parties.	
           			NumOfParties++; ConveringString = ("Party "+ NumOfParties);
           			final JFrame PartyWindow = new JFrame(ConveringString);
           			//now onto making the party window.
           			IsPartyComplete=false;
    		while(IsPartyComplete==false){
    			/*for each party you make a name party 1 and party 2.
    			*make the party FJrame and name it.
    			*for each member you add a JComboBox*/
    			
    			while(NumOfMembers<member.length){
    	NumOfMembers++;		//add counter for each member.					
    	//adds the JCombobox and the JTextField.
    	//just discovered final makes it so each JComboBox and JTextField are related. really cool
    	final JComboBox MemberComboBox = new JComboBox(AllPossibleMembers);
    	final JTextField ComboBoxResult = new JTextField("Please pick for member "+NumOfMembers,15);
    	MemberComboBox.addItemListener// the added listener for the MemberComboBox		
    					(
    						new ItemListener(){					
    						public void itemStateChanged(ItemEvent event) {
    						//to have the JComboBox effect the JTextField.
    								if(event.getStateChange()==ItemEvent.SELECTED); 
    								ComboBoxResult.setText((String) MemberComboBox.getSelectedItem());
    								}});
    	PartyWindow.add(MemberComboBox);	
    	PartyWindow.add(ComboBoxResult);
    	PartyWindow.repaint();
    				}
    			
    				IsPartyComplete=true;
    				//to actually make the party visable.
    				PartyWindow.setLayout(new FlowLayout());
    				PartyWindow.setSize(300,200);
    				PartyWindow.setVisible(true);
    				PartyWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           	}		}
    				
    		} }
    if you know of a better way to do this by all means please tell me.
    or have any more questions onto what i'm trying to do.
    Last edited by Mokomi; 06-24-2012 at 08:32 PM. Reason: bad // descriptions.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: creating unknown number of JFrame with an unknown number of JComboBox in each JFr

    Fix your indents and whitespace, that code's an unreadable mess.
    Code Conventions for the Java Programming Language: Contents

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Mokomi is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Default Re: creating unknown number of JFrame with an unknown number of JComboBox in each JFr

    edited the code. still reading more for code conventions. hope you can understand it now.

  4. #4
    Mokomi is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Default Re: creating unknown number of JFrame with an unknown number of JComboBox in each JFr

    Java Code:
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.lang.reflect.Type;
    import java.util.Locale;
    import static java.lang.System.out;
    import static java.lang.System.err;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.util.Arrays;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.AdjustmentEvent;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Scanner;
    import java.util.Scanner;
     
    import javax.swing.*;
     
    public class bucky  extends JFrame{
        
    	public static void main(String[] args){
    		//all the members in a single String[]
    		String[] All_Possible_Members= {null,"Olivia","Roton","Presti","Zren","demi"};
    		String ConveringString="1"; 	//for String conversion.
    		boolean IsPartyComplete =false;
           	int Num_Parties =0; 	//counter for the parties    
    		int Num_Members = 0;	//counter for the members
    	       
    		//makes the total number of parties. for this example it is 2 seperate parties.
            String[] Total_parties = new String[2];
            //with a maximum of 3 members in each party. this is for showing you reasons.
            String[] Total_member = new String[3];
            
    		while(Num_Parties<Total_parties.length)//starts the while loop for each party.
    	       	{
    				//the Num_Parties to match up the parties.	
           			Num_Parties++; ConveringString = ("Party "+ Num_Parties);
           			final JFrame PartyWindow = new JFrame(ConveringString);
           			//now onto making the party window.
           			IsPartyComplete=false;
    		while(IsPartyComplete==false){
    			/*for each party you make a name party 1 and party 2.
    			*make the party FJrame and name it.
    			*for each member you add a JComboBox*/
    			
    			while(Num_Members<Total_member.length){
    	Num_Members++;		//add counter for each member.					
    	//adds the JCombobox and the JTextField.
    	//just discovered final makes it so each JComboBox and JTextField are related. really cool
    	final JComboBox MemberComboBox = new JComboBox(All_Possible_Members);
    	final JTextField ComboBoxResult = new JTextField("Please pick for member "+Num_Members,15);
    	MemberComboBox.addItemListener// the added listener for the MemberComboBox		
    					(
    						new ItemListener(){					
    						public void itemStateChanged(ItemEvent event) {
    						//to have the JComboBox effect the JTextField.
    								if(event.getStateChange()==ItemEvent.SELECTED); 
    								ComboBoxResult.setText((String) MemberComboBox.getSelectedItem());
    								}});
    	PartyWindow.add(MemberComboBox);	
    	PartyWindow.add(ComboBoxResult);
    	PartyWindow.repaint();
    				}
    			
    				IsPartyComplete=true;
    				//to actually make the party visable.
    				PartyWindow.setLayout(new FlowLayout());
    				PartyWindow.setSize(300,200);
    				PartyWindow.setVisible(true);
    				PartyWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           	}		}
    				
    		} }
    this is my current code. right now i'm trying to figure out why each JFrame does not have it's own JComboBox and JTextFields. If you know a better way on having multiple JFrames with the same JComboBox and JTextField. if you want a picture of what I want I am able to do that for you.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,251
    Rep Power
    25

    Default Re: creating unknown number of JFrame with an unknown number of JComboBox in each JFr

    You need to compile your code using the javac command's -Xlint option. There is an important compiler warning you need to see.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Mokomi is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Default Re: creating unknown number of JFrame with an unknown number of JComboBox in each JFr

    attempting to fix a javac not recognized error.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,251
    Rep Power
    25

    Default Re: creating unknown number of JFrame with an unknown number of JComboBox in each JFr

    The javac.exe file is in the JDK's bin folder.

    What program are you using to compile the program? Does it have options?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Mokomi is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Default Re: creating unknown number of JFrame with an unknown number of JComboBox in each JFr

    fixed the javac problem. i'm using eclipse.
    I do not know what -Xlint option is.
    got it javac -Xlint bucky.java is giving me errors for my JComboBox and it's ActionListener
    Last edited by Mokomi; 06-24-2012 at 10:39 PM.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,251
    Rep Power
    25

    Default Re: creating unknown number of JFrame with an unknown number of JComboBox in each JFr

    is giving me errors
    Please post the full text of the error messages if you need help with them.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    Mokomi is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Default Re: creating unknown number of JFrame with an unknown number of JComboBox in each JFr

    now I have to figure why my own program compiling with javac with -Xlint option and running fine. meanwhile this is not.
    o hey it is working. java bucky is running fine.

    here are the errors it is giving me when I try to compile it.
    missing type arguments for generic class JComboBox<E>
    where E is a type-variable:
    E extends Object declared in class JComboBox
    bucky.java:39: warning: [rawtypes] found raw type: JComboBox
    final JComboBox MemberComboBox = new JComboBox(All_Possible_Members);
    ^
    missing type arguments for generic class JComboBox<E>
    where E is a type-variable:
    E extends Object declared in class JComboBox
    bucky.java:39: warning: [unchecked] unchecked call to JComboBox(E[]) as a member
    of the raw type JComboBox
    final JComboBox MemberComboBox = new JComboBox(All_Possible_Members);
    ^
    where E is a type-variable:
    E extends Object declared in class JComboBox
    bucky.java:46: warning: [empty] empty statement after if
    if(event.getStat
    eChange()==ItemEvent.SELECTED);

    ^
    bucky.java:8: warning: [serial] serializable class bucky has no definition of se
    rialVersionUID
    public class bucky extends JFrame{
    ^
    5 warnings

    (odd I can run it via java bucky just fine.)
    Last edited by Mokomi; 06-24-2012 at 10:48 PM.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,251
    Rep Power
    25

    Default Re: creating unknown number of JFrame with an unknown number of JComboBox in each JFr

    Those messages are warnings and won't stop the compiler from creating a class file that can be executed.

    Did you look at all of them to make sure none of them were important?
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    Mokomi is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Default Re: creating unknown number of JFrame with an unknown number of JComboBox in each JFr

    I do not know what they mean.
    Judging how my problem is none of my JComboBox is going to the next JFrame it might be important. but either is my JTextField. the error does not mention the JTextField "ComboBoxResult". which is also not going on the next JFrame.

  13. #13
    Mokomi is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Default Re: creating unknown number of JFrame with an unknown number of JComboBox in each JFr

    I think I have a solution. I can
    Java Code:
    while(Counter<2) //starts the while loop for each party.
    	       	{
    		
    counter++;
           		final JFrame PartyWindow = new JFrame("Party "+counter);
    final JButton Add_Member = new JButton("Add Member");
    }
    That gives me multiple JFrame with the JButton.
    meanwhile adding a while loop to it.

    Java Code:
    while(counter<2){
    counter++;
      		final JFrame PartyWindow = new JFrame("Party "+counter);
    while(counter 2<3){
    counter2++;
    final JButton AddMember = new JButton("Add Member");
    }}
    gives me everything I need but only in 1 JFrame. going to go with the first option then add an actionListener to add more JComboBox's.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,251
    Rep Power
    25

    Default Re: creating unknown number of JFrame with an unknown number of JComboBox in each JFr

    Why are you using while() loops where most people would use a for loop?

    What about this warning:
    warning: [empty] empty statement after if
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    Mokomi is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Red face Re: creating unknown number of JFrame with an unknown number of JComboBox in each JFr

    it doesn't like this part
    Java Code:
    new ItemListener(){                 
                            public void itemStateChanged(ItemEvent event) {
                            //to have the JComboBox effect the JTextField.
                                    if(event.getStateChange()==ItemEvent.SELECTED); 
                                    ComboBoxResult.setText((String) MemberComboBox.getSelectedItem());
                                    }});
    it does like it if I add a {} after the if statement
    Java Code:
       new ItemListener(){                 
                            public void itemStateChanged(ItemEvent event) {
                            //to have the JComboBox effect the JTextField.
                                    if(event.getStateChange()==ItemEvent.SELECTED){
                                    ComboBoxResult.setText((String) MemberComboBox.getSelectedItem());
                                  }  }});
    now only gives 4 errors.
    but anyway I solved the issue. now onto a new problem.
    Java Code:
    		final JFrame PartyWindow = new JFrame(ConveringString);
           	final JButton Add_Member = new JButton("Add Member");
    are going to be together with an action listener.
    Java Code:
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    
    
     
    public class bucky  extends JFrame{
    	   
    	public static void main(String[] args){
    		final int size=75;
    		final String[] All_Possible_Members= {null,"Olivia","Roton","Presti","Zren","demi"};
    		String Name_Of_Parties="1"; 	//for String conversion.
    		 	int Num_Parties =0; 	//counter for the parties    
    	    String[] Total_parties = new String[2];
            while(Num_Parties<Total_parties.length){		  
            	Num_Parties++; Name_Of_Parties = ("Party "+ Num_Parties);
           		final JFrame PartyWindow = new JFrame(Name_Of_Parties);
           	final JButton Add_Member = new JButton("Add Member");
           		Add_Member.addActionListener(
    				new ActionListener(){					
    					public void actionPerformed(ActionEvent event) {
    						final JComboBox test = new JComboBox(All_Possible_Members);			
    						System.out.println("test");
    						PartyWindow.add(test);
    						PartyWindow.setSize(300,size);
    					// I need this PartyWindow.setSize to resize by adding 25 to the total value
    					}});
    PartyWindow.add(Add_Member);
    PartyWindow.setLayout(new FlowLayout());
    PartyWindow.setSize(300,75);
    PartyWindow.setVisible(true);
    PartyWindow.setDefaultCloseOperation(EXIT_ON_CLOSE);
     
    	       	}
    	
    	}
    }
    everytime I hit add member. It should resize the window a little bit to include the new member.
    I can
    Java Code:
     int window = size+15;
    PartyWindow.setSize(300,window);
    but it is only for the first time. every time after that it wouldn't resize because I cannot edit size.
    Last edited by Mokomi; 06-25-2012 at 01:27 AM.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,251
    Rep Power
    25

    Default Re: creating unknown number of JFrame with an unknown number of JComboBox in each JFr

    if(event.getStateChange()==ItemEvent.SELECTED);
    The ; makes for an empty statement for the if


    The formatting of your code makes it very hard to read. The indentations should follow from the nesting level of the statements.
    Last edited by Norm; 06-25-2012 at 02:14 AM.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 8
    Last Post: 01-21-2012, 01:14 AM
  2. Replies: 7
    Last Post: 12-27-2011, 03:06 AM
  3. Dice help. posting the number of times a number is rolled.
    By cookiejarvus in forum New To Java
    Replies: 13
    Last Post: 12-04-2011, 11:08 PM
  4. Replies: 1
    Last Post: 10-28-2011, 08:18 AM
  5. Replies: 2
    Last Post: 06-01-2010, 09:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •