Results 1 to 9 of 9
  1. #1
    java999 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default JButtons In 2D Array

    Hello everyone, I have a [12][15] array with Jbuttons. When a user clicks on any button in a row I would like only the buttons in that row to remain active and all other button to be set to false, so that the user cannot click any other JButton, in any other row, except the first one selected.

    I'm finding it hard to work out a way of doing this, please I need code sample.
    Last edited by java999; 04-22-2011 at 11:54 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It sounds like your action listener should iterate through the 2-D array of JButtons using a pair of nested for loops. In the inner loop, you'll call setEnabled(true/false) depending on if the current row matches the row of the button pressed.

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    ...another option would be to construct the matrix of buttons using Box's.

    Java Code:
    Box matrix = Box.createVerticalBox();
    
    Box row0 = Box.createHorizontalBox();
    row0.add(button00_00);
    ...
    row0.add(button00_14); //obviously, use loops for this
    
    matrix.add(row0);
    
    ...add the other rows
    Then when a button is pressed, you can enable/disable the box that contains all the buttons on that row.

  4. #4
    java999 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    It sounds like your action listener should iterate through the 2-D array of JButtons using a pair of nested for loops. In the inner loop, you'll call setEnabled(true/false) depending on if the current row matches the row of the button pressed.
    Please can you give me a sample code.

  5. #5
    java999 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by toadaly View Post
    ...another option would be to construct the matrix of buttons using Box's.

    Java Code:
    Box matrix = Box.createVerticalBox();
    
    Box row0 = Box.createHorizontalBox();
    row0.add(button00_00);
    ...
    row0.add(button00_14); //obviously, use loops for this
    
    matrix.add(row0);
    
    ...add the other rows

    Then when a button is pressed, you can enable/disable the box that contains all the buttons on that row.
    Will this allow me to disable all Jbuttons on the rows that have not been selected. Also can I add this Matrix to a Gridlayout
    Last edited by java999; 04-22-2011 at 04:44 AM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by java999 View Post
    Please can you give me a sample code.
    It'd be better for you as an exercise to try this on your own first, and then post what you've done.

  7. #7
    java999 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    It'd be better for you as an exercise to try this on your own first, and then post what you've done.
    It just shows me that my thread is tooooooooo advance for this forum!
    Last edited by java999; 04-22-2011 at 12:45 PM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by java999 View Post
    It just shows me that my thread is tooooooooo advance for this forum!
    Nice try, but we've seen this weak attempt before to get others to do your work, and it ain't going to work on us. If you think we don't know how to do this, then it won't bother us, but as for us, we'll only see this as an attempt by a lazy student unwilling and unable to put in the effort.

    I'm going to lock this thread because it's obviously going no where. You'll have better luck finding help cheating elsewhere.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If you have a change of heart and want to work through the problem with us as we've suggested, then private message me, and I'll re-open this thread and delete your comments and mine, but til then this will stay locked.

Similar Threads

  1. Keeping two schema in sink
    By Amod in forum JDBC
    Replies: 4
    Last Post: 02-04-2011, 12:40 PM
  2. Keeping one window above another
    By porchrat in forum AWT / Swing
    Replies: 3
    Last Post: 01-27-2011, 08:11 AM
  3. Keeping data in memory
    By roud9 in forum New To Java
    Replies: 1
    Last Post: 11-02-2010, 12:28 AM
  4. Keeping a graphic central
    By Josisco in forum AWT / Swing
    Replies: 1
    Last Post: 10-24-2010, 06:38 PM
  5. Java Keeping Text in One Place
    By xpngamer in forum New To Java
    Replies: 1
    Last Post: 04-09-2009, 06:27 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
  •