Results 1 to 11 of 11
- 03-11-2012, 03:04 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 18
- Rep Power
- 0
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
grid.javaJava 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; } }
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); } } }
-
Re: Passing ArrayList<Character> to a grid of JButtons
Well, here is a pretty fast way to get that done:
Then all you'd have to do is initialise each button like this:Java Code:class RandomButton extends JButton { int number; RandomButton(String label, int randomNo) { super(label); number = randomNo; } public int getNumber() { return number; } }
Java Code:RandomButton button = new RandomButton("("+x+","+y+")", rand.nextInt(25)); int number = button.getNumber();
-
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.
- 03-11-2012, 03:50 AM #4
Member
- Join Date
- Mar 2012
- Posts
- 18
- Rep Power
- 0
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?
- 03-11-2012, 04:34 AM #5
Member
- Join Date
- Mar 2012
- Posts
- 18
- Rep Power
- 0
Re: Passing ArrayList<Character> to a grid of JButtons
nevermind i solved it :)
- 03-11-2012, 04:53 AM #6
Member
- Join Date
- Mar 2012
- Posts
- 18
- Rep Power
- 0
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
main.javaJava 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 } } } }
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); }
-
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(); }
- 03-12-2012, 07:55 AM #8
Member
- Join Date
- Mar 2012
- Posts
- 18
- Rep Power
- 0
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));
-
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;
- 03-18-2012, 08:33 AM #10
Member
- Join Date
- Mar 2012
- Posts
- 18
- Rep Power
- 0
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?
-
Similar Threads
-
passing arraylist
By dejoere in forum New To JavaReplies: 5Last Post: 05-15-2011, 01:13 AM -
Setting sizes to JButtons in grid layout
By sarah jain in forum AWT / SwingReplies: 8Last Post: 02-22-2011, 04:08 AM -
JFrame: JButtons in a grid
By jackal in forum New To JavaReplies: 2Last Post: 06-09-2010, 07:56 PM -
reading text character by character
By bugger in forum New To JavaReplies: 2Last Post: 11-09-2007, 08:54 PM -
Help passing arraylist to another class
By adlb1300 in forum New To JavaReplies: 3Last Post: 11-06-2007, 08:02 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks