Results 1 to 11 of 11
  1. #1
    jumpgirl is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Unhappy Passing ArrayList<Character> to a grid of JButtons

    Hi, I'm a 2nd year Computer Science student. My specialization is Instructional Systems Technology that is why I didn't get to practice java that much and now, I already experiencing problems with it.
    My problem is I need to pass an array of random characters to a grid of jbuttons, since we are asked to do a word factory simulation. I thought that making a grid of jbuttons would be good but I can't seem to pass the contents of the array in my random.java in the grid of jbuttons. Can anyone please help me? I'm stuck in this part and I still need to to the searching algorithms for this project.

    This is my code.

    random.java

    Java Code:
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Random;
    
    
    public class random {
    	ArrayList<Character> c1  = new ArrayList<Character>();
    	public char c;
    	
    	public void randomNum(){   
    	    Random rand = new Random(); 
    		
        for (int j = 1; j<=100; j++)
        	
        {
          //pick = rand.nextInt(25)+ 0;
          c =(char)(rand.nextInt('Z'-'A' +1) + 'A'); 
          c1.add(c);
          System.out.println("num " + j + ": " + c1);    
          
        } 
     }
        
        public ArrayList<Character> getArray(){
        	return c1;
           
      }
    	
    	
    	
    }
    grid.java
    Java Code:
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.util.ArrayList;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.CompoundBorder;
    import javax.swing.border.EmptyBorder;
    
    public class grid {
    	JFrame frame = new JFrame("Word Factory");
    	
    	
    	public grid(int width, int height, int hgap, int vgap){
    		
    		((JPanel) frame.getContentPane()).setBorder(new CompoundBorder(
    			        new EmptyBorder(30, 30, 60, 200), new SolidBorder()));
    		 
    		JButton[][] grid;
    		frame.setSize(1024, 710);
    	    Container content = frame.getContentPane();
    	    content.setBackground(Color.white);
    	    content.setLayout(new FlowLayout()); 
    	    
    	    frame.addWindowListener(new ExitListener());
    		
    		frame.setLayout(new GridLayout(width,height, hgap, vgap));
    		
    		
    		random ran = new random();
    		System.out.println(ran.c1);
    		//ArrayList<Character> list = ran.getArray();
    		
    	         grid=new JButton[width][height]; 
            for(int y=0; y<height; y++){
                    for(int x=0; x<width; x++){
                            
                    	grid[x][y]=new JButton("("+x+","+y+")");  
                    	
                    	
                            //ArrayList j = ran.getArray();
                       
                    	//grid[x][y]=new JButton(""+list.get(x)+"");//need to fix
                    	//grid[x][y]=new JButton(""+j.toString()+"");
                    	 
                         frame.add(grid[x][y]); 
                    }
    		
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            frame.setVisible(true);
    		
    	}
    		
    		
    }
    }

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Passing ArrayList<Character> to a grid of JButtons

    Well, here is a pretty fast way to get that done:

    Java Code:
    class RandomButton extends JButton {
      int number;
      RandomButton(String label, int randomNo) {
        super(label);
        number = randomNo;
      }
      public int getNumber() {
        return number;
      }
    }
    Then all you'd have to do is initialise each button like this:
    Java Code:
    RandomButton button = new RandomButton("("+x+","+y+")", rand.nextInt(25));
    int number = button.getNumber();

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Passing ArrayList<Character> to a grid of JButtons

    I just realised the purpose was to pass a char rather than an int... well, you could do it the same way as in the example above.

  4. #4
    jumpgirl is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Default Re: Passing ArrayList<Character> to a grid of JButtons

    thank you for your help :)
    i have another question. How can I display the random generated characters in each of the jbuttons?

  5. #5
    jumpgirl is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Default Re: Passing ArrayList<Character> to a grid of JButtons

    nevermind i solved it :)

  6. #6
    jumpgirl is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Default Re: Passing ArrayList<Character> to a grid of JButtons

    here is my new code
    i got another problem. The grid is supposed to be 10 by 10 but what happens is that the size of the grid also became a bit random like 9x10 or something like that. I defined the size in my main but it is not working properly. Also, I tried hard coding the width and the height part in the loop in the grid.java but it doesn't work

    grid.java

    Java Code:
    //letter-grid
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.util.ArrayList;
    import java.util.Random;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.CompoundBorder;
    import javax.swing.border.EmptyBorder;
    
    public class grid {
    	JFrame frame = new JFrame("Word Factory");
    	
    	
    	public grid(int width, int height, int hgap, int vgap){
    		
    		((JPanel) frame.getContentPane()).setBorder(new CompoundBorder(
    			        new EmptyBorder(30, 30, 60, 200), new SolidBorder()));
    		 
    		JButton[][] grid;
    		frame.setSize(1024, 710);
    	    Container content = frame.getContentPane();
    	    content.setBackground(Color.white);
    	    content.setLayout(new FlowLayout()); 
    	    frame.addWindowListener(new ExitListener());
    		frame.setLayout(new GridLayout(width, height, hgap, vgap));
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            frame.setVisible(true);
            
    		grid=new JButton[width][height]; //allocate the size of grid
    		
    		Random rand = new Random();
    		
            for(int y=0; y<height; y++){
                    for(int x=0; x<width; x++){
                    	              	
                     	//RandomButton button = new RandomButton("("+x+","+y+")", (char)(rand.nextInt('Z'-'A' +1) + 'A'));
                    	RandomButton button = new RandomButton(""+(char)(rand.nextInt('Z'-'A' +1) + 'A')+"");
                    	//char letter = button.getCharacter();
                    	  
                    	frame.add(button);
                     	
                    	//frame.add(grid[x][y]); //adds button to grid
                    }
    		
            
    		
    	}
    		
    		
    }
    }
    main.java
    Java Code:
    public static void main(String args[]){
    				
    	    new main().readFromFile("dictionary.txt");
    		/*random ran = new random();
    		ran.randomNum();*/
    		
    		//ran.randomNum1();
    	    grid g = new grid(10,10, 3, 3);
    	    
    	    	
    	}

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Passing ArrayList<Character> to a grid of JButtons

    Its probably an issue where one of the last buttons are failing to initialise and causing the method to stop.

    Did you have any messages in your debugger?

    I would separate the char code and print it out to see what you are getting, like this:

    Java Code:
    for (...) {
      for (...) {
        char c = (char) rand.nextInt('Z'-'A' +1) + 'A');
        System.out.print(c + ", ");
        frame.add(new RandomButton(c));
      }
      System.out.println();
    }

  8. #8
    jumpgirl is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Default Re: Passing ArrayList<Character> to a grid of JButtons

    i tried your code and there is an error because the parameter in RandomButton is a String
    Java Code:
    frame.add(new RandomButton(c));

  9. #9
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Passing ArrayList<Character> to a grid of JButtons

    Where is your updated RandomButton class?

    If you're accepting a String in the constructor you can convert a char to a String like this:
    Java Code:
    char c = 'A';
    String s = "" + c;

  10. #10
    jumpgirl is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Default Re: Passing ArrayList<Character> to a grid of JButtons

    I already solved my problem. I replaced JButtons with JTextFields. Is it possible to add another JTextField or buttons outside the grid that I made?

  11. #11
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Passing ArrayList<Character> to a grid of JButtons

    You need another class which would be your main window, in that window add the grid and the JTextField.

Similar Threads

  1. passing arraylist
    By dejoere in forum New To Java
    Replies: 5
    Last Post: 05-15-2011, 01:13 AM
  2. Setting sizes to JButtons in grid layout
    By sarah jain in forum AWT / Swing
    Replies: 8
    Last Post: 02-22-2011, 04:08 AM
  3. JFrame: JButtons in a grid
    By jackal in forum New To Java
    Replies: 2
    Last Post: 06-09-2010, 07:56 PM
  4. reading text character by character
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-09-2007, 08:54 PM
  5. Help passing arraylist to another class
    By adlb1300 in forum New To Java
    Replies: 3
    Last Post: 11-06-2007, 08:02 PM

Tags for this Thread

Posting Permissions

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