Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,589 members, 7,809,133 topics. Date: Friday, 26 April 2024 at 12:41 AM

Keeping Only The Jbuttons In A Selected Row Active In A 2d Array - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Keeping Only The Jbuttons In A Selected Row Active In A 2d Array (1931 Views)

Using 2-dimensional Array In C++ / Complete Array Of Countries And Nigerian States [open Source] / Converting Picture To Byte Array(blob) And Back To Picture (2) (3) (4)

(1) (Reply) (Go Down)

Keeping Only The Jbuttons In A Selected Row Active In A 2d Array by csharpjava(m): 4:23am On Apr 22, 2011
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.

Please I need your contributions.
Re: Keeping Only The Jbuttons In A Selected Row Active In A 2d Array by segsalerty(m): 8:33am On Apr 23, 2011
U knw one funny thing? i have improved in the way i help dudes here  wink wink, We need to do this together and help ourselves ,  i just helped u with smth that should guide u in solving your real problem, all u need to do is understand my code( oh sorry, i dnt like comments coz i expect anyone to be able to read and interprete my codes  shocked shocked) , 
Codes below attached  try compile and run it
You didnt even tell us the laguage u want it solved in ,  well, this is JAVA !

smiley smiley wink wink cheesy grin

Re: Keeping Only The Jbuttons In A Selected Row Active In A 2d Array by csharpjava(m): 11:07am On Apr 23, 2011
Thank you for your help. Sorry I did not say the language should be in Java. Because of the sensitive nature of my work it won't be possible for me to post my code here. I have testing your code, what it does is to turn all the buttons in the selected row to false.

I want only the Buttons in the row selected to be enabled and all other Buttons in all the other rows changed to false. I think I'm getting close to finding the solution.

Thanks once again for contribution.
Re: Keeping Only The Jbuttons In A Selected Row Active In A 2d Array by segsalerty(m): 9:34pm On Apr 23, 2011
Yeah , u re so much welcome, Someone post here on facebook abt posting codes as a solution to people's problem that we only need to assist not SOLVE wink wink
Thats why i did what i did with the code i posted and i hope it will help u in solving your problem !
grin grin cool cool
Re: Keeping Only The Jbuttons In A Selected Row Active In A 2d Array by Fayimora(m): 10:57pm On Apr 23, 2011
@segs,  Nice way to explain,  However, it would have been better if you explained without any code then when he/she gets PROPER stuck, then you pop in a lil part of code where necessary.

@csharp,   He gave you code that sets all the buttons in a row false and the rest remain true. But you want the opposite. I just wanted to ask, what should be the initial state of the buttons??

To achieve this, all you need do is this:

pseudo-code:
    loop through the whole multi-dimensional array
    check if the button you currently are on is one in the selected column
    if true then set false
Re: Keeping Only The Jbuttons In A Selected Row Active In A 2d Array by csharpjava(m): 11:43pm On Apr 23, 2011
pseudo-code:
    loop through the whole multi-dimensional array
    check if the button you currently are on is one in the selected column
    if true then set false

This pseudo-code will not yield the desired result, try it for yourself, what I'm trying to archieve is very complex.
Re: Keeping Only The Jbuttons In A Selected Row Active In A 2d Array by Fayimora(m): 12:03am On Apr 24, 2011
If I understand what you are saying(i already have my code that works), when a button is clicked(all buttons are set to true at first) all other buttons apart from the buttons on the row that contains the clicked button is set to false. Isin't this what you want to do?? Can't see whats complex about it.
Re: Keeping Only The Jbuttons In A Selected Row Active In A 2d Array by csharpjava(m): 12:26am On Apr 24, 2011
If I understand what you are saying(i already have my code that works), when a button is clicked(all buttons are set to true at first) all other buttons apart from the buttons on the row that contains the clicked button is set to false. Isin't this what you want to do?? Can't see whats complex about it.

No! when a button is clicked all buttons are set to false, only the buttons in the row that was clicked should remain true.
Re: Keeping Only The Jbuttons In A Selected Row Active In A 2d Array by Fayimora(m): 12:29am On Apr 24, 2011
Yeah we are saying the same thing, lol So wats the biggie, Use the pseudo code above and it would yield your desired result.lol
Good luck,

Spoiler: You dont need to add more than 8 lines of code to what you already have. hehe wink
Re: Keeping Only The Jbuttons In A Selected Row Active In A 2d Array by csharpjava(m): 12:38am On Apr 24, 2011
Fayimora

Can you be quiet, you don't have a clue of what I'm trying to do, you think this is childs play.
Re: Keeping Only The Jbuttons In A Selected Row Active In A 2d Array by Fayimora(m): 12:43am On Apr 24, 2011
Hahaha cheesy cheesy  Funny you. You are the one who has to cool down and look at what you want to do. LOCALIZE THE PROBLEM.

Just so you dnt think am blabbin poo. Look at  the image below


Its funny how you have a problem and you decide to insult who is helping you. CHILDS PLAY Wow!!! thats deep for a "programmer". As for me, I believe it is smaller than child's play. Every problem is child's play when you learn to localize it. Think about that.
Re: Keeping Only The Jbuttons In A Selected Row Active In A 2d Array by segsalerty(m): 2:57am On Apr 24, 2011
grin grin
csharpjava:

This pseudo-code will not yield the desired result, try it for yourself, what I'm trying to archieve is very complex.
Sorry to say , nothing is too/very complex in programming  wink (Just make ur approach simple as possible --- as if its sand play cheesy),  the solution i gave u is damn gonna solve the problem u have at hand, just wana get it twisted for u so that u can show how much u are gonna learn solving this problem.
******this is one of the 1,001 ways of solving the problem, this is quite very very simple, nothing is complex, okay ? just study it and imagine how local i took the problem before solving it this way.
I inlcuded a new method as follows

    private void disableAll_andEnableBack_in10_Seconds(final int r, final int colCount){
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                final int count  = 10;
                for(int i = 0; i < buttons.length; i++){
                    for(int j = 0; j < buttons[i].length; j++){
                        buttons[i][j].setEnabled(false);
                    }
                }
               
                for(int x = 0; x < colCount; x++){
                    buttons[r][x].setEnabled(true);
                }
               
                t = new Timer(1000, new java.awt.event.ActionListener() {
                    int c = 0;
                    public void actionPerformed(java.awt.event.ActionEvent e) {
                        if(c >= count){
                            for(int i = 0; i < buttons.length; i++){
                                for(int j = 0; j < buttons[i].length; j++){
                                    buttons[i][j].setEnabled(true);
                                }
                            }                           
                            t.stop();                         
                        }
                        c++;
                    }
                });
                if(!t.isRunning()){
                    t.start();
                }
            }
        });
    }

******and i modified this existing function body to do like this ->

    private void loadAction(final int r, final int k, final int colCount){
        buttons[r][k].addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent e){
                disableAll_andEnableBack_in10_Seconds(r, colCount);
            }
        });       
    }

********Note: i introduced a new global variable
private Timer t;

so that i can track the timer instance and not abuse it in the program, a timer must stop before another
u grab?

I attached the new full length of code,  just download and re-run it ,  then, do urs in ur own way

Hope this Helps ?

Fayimora:

Hahaha cheesy cheesy  Funny you. You are the one who has to cool down and look at what you want to do. LOCALIZE THE PROBLEM.

Just so you dnt think am blabbin poo. Look at  the image below


Its funny how you have a problem and you decide to insult who is helping you. CHILDS PLAY Wow!!! thats deep for a "programmer". As for me, I believe it is smaller than child's play. Every problem is child's play when you learn to localize it. Think about that.

Please, u dnt have to talk that way to him, U shld beta have helped him more if ur pseudo-code couldnt help him
I guess u were the one that even mentioned about codes provided on problems posted on NAiraland ,  anywayz,  u should have tried beta helping him !  wink wink

Nobody knows all or is beta or perfect,  if someone finds what is so simple for u to solve tasking sometimes doesnt mean the person is OLODO!  okay? wink
Thanks for the understanding ! grin

Re: Keeping Only The Jbuttons In A Selected Row Active In A 2d Array by Fayimora(m): 3:13am On Apr 24, 2011
Hahaha i like you man. Anyway it was not my intention as he "bullshitted" my pseudo-code without even giving it enough thought. I used the pseudo-code, implemented it and it worked. I wont post somefin am not 90% sure that it would work. Programming is easy once you learn to localize your problem.

I tried to make him see that but ,

I used you code and added about 7 lines to it. You can see it in full below.

import java.awt.FlowLayout;
import javax.swing.*;
 
  public class DisableRowButton extends JFrame{

    public static void main(String []args){
      new DisableRowButton();
    }
   


  private JButton buttons[][] = new JButton[12][15];
private JButton activebuttons[] = new JButton[15];

    public DisableRowButton(){
        super("Array of Buttons Controller"wink;
        this.setSize(1250, 400);
        this.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setPreferredSize(new java.awt.Dimension(1250, 400));
        this.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
        this.setResizable(false);
        initComponents();
        this.setVisible(true);
    }

    private void initComponents(){
        for(int i = 0; i < buttons.length; i++){
            for(int j = 0; j < buttons[i].length; j++){
                buttons[i][j] = new JButton();//initialize each Jbutton
                buttons[i][j].setText(i + " * " + j);
                buttons[i][j].setPreferredSize(new java.awt.Dimension(80, 30));
                loadAction(i, j, buttons[i].length);
                this.getContentPane().add(buttons[i][j]);
            }
        }
    }

    private void loadAction(final int r, final int k, final int colCount){
        buttons[r][k].addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent e){
                for(int x = 0; x < colCount; x++){
activebuttons[x] = buttons[r][x];
                    // buttons[r][x].setEnabled(false);
                }
for(int i = 0; i < buttons.length; i++)
{
            for(int j = 0; j < buttons[i].length; j++)
{
if(buttons[i][j] != activebuttons[j]){
buttons[i][j].setEnabled(false);
}
}
}
            }
        });       
    }

}



The main difference lies here:

private void loadAction(final int r, final int k, final int colCount){
        buttons[r][k].addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent e){
                for(int x = 0; x < colCount; x++){
activebuttons[x] = buttons[r][x];
                    // buttons[r][x].setEnabled(false);
                }
for(int i = 0; i < buttons.length; i++)
{
            for(int j = 0; j < buttons[i].length; j++)
{
if(buttons[i][j] != activebuttons[j]){
buttons[i][j].setEnabled(false);
}
}
}
            }
        });       
    }


and an additional variable
private JButton activebuttons[] = new JButton[15];


It should be obvious what exactly am doing here. I have a new array of type JButton that is eventually hold the whole row that you want enabled when disabling others.

The previous code segs posted was disabling, now instead of disabling you store the row into the new matrix. Then you loop through every button and disable those that are not in the new matrix.

Thats exactly what my pseudo-code says but no one asked for more explanation on any part of it. Rather it was "bullshitted".

Fayimora
Re: Keeping Only The Jbuttons In A Selected Row Active In A 2d Array by segsalerty(m): 3:24am On Apr 24, 2011
Fayimora:


It should be obvious what exactly am doing here. I have a new array of type JButton that is eventually hold the whole row that you want enabled when disabling others.

The previous code segs posted was disabling, now instead of disabling you store the row into the new matrix. Then you loop through every button and disable those that are not in the new matrix.

Thats exactly what my pseudo-code says but no one asked for more explanation on any part of it. Rather it was "bullshitted".

Fayimora

Excellent , making 2 ways of solving this problem for our brother in Programming. lets see, we've still got 999 more ways of solving this problem ! let him try and show us his coding approach on here !
I think there is more sense in ur idea of Helping people with problems they've got on here. if at all there is gonna be need for Helper to show sample, it shouldnt be pointing to the Result Gan Gan (It will so much help the Helpee wink wink wink wink), abi what do u think ? cool
Re: Keeping Only The Jbuttons In A Selected Row Active In A 2d Array by Fayimora(m): 3:40am On Apr 24, 2011
Yope thats exactly what I said and meant. I participate in about 10 different forums online and thats the way it goes. You have a problem, then start it, brea it down yourself until you understand where exactly the problemis rom. Someone without analytical skills can never be a programmer. He/She just knows how to write code.

Now, of course you might not be dat strong but you have tried. the next thing is for you to post your possible solution(which would be incomplete) and state what problem you are having. Now someone should be able to tell you what you need to know or how to go about it without direct code. From there you think again and solve the problem. If at l you have any problem along the line, you come back.

Correct me if am wrong but am sure this forum is not meant to teach you how to use a language but how to program efficiently. You wanna put four buttons in a row? Thats fine but do you even know how to create the button? No, the go back to the java tutorials and check it out.lol

Its like a doing recursion and having loops and if's in-between.
Re: Keeping Only The Jbuttons In A Selected Row Active In A 2d Array by csharpjava(m): 10:12am On Apr 24, 2011
segsalerty

Thanks for the good work you are doing for people on this forum, please keep it up. I have found a way to get the Buttons the way I want them, but I will go through your code step by step and compare it with mine to see how you went about yours. Because of the nature of my project I cannot post my code yet, but, later I will come back and post the full code here for anyone that might want to build an application like mine.
Re: Keeping Only The Jbuttons In A Selected Row Active In A 2d Array by csharpjava(m): 10:33am On Apr 24, 2011
Fayimora
Correct me if am wrong but am sure this forum is not meant to teach you how to use a language but how to program efficiently. You wanna put four buttons in a row? Thats fine but do you even know how to create the button? No, the go back to the java tutorials and check it out.lol

People come here for help on coding and not for you to lecture them in programming, lectures should take place in a learning environment like in a class room or an online classroom.

So please try and give the right answer to the right person,  I mean if someone wants a piece of code let him have it and if you are not willing to do so please let others provide the code.

If someone has asked for a pseudo-code fine let him have the pseudo-code.

Please stop calling the shots here. After all you are free to start your own java forum and practice what you want and lets see if that forum will be popular.

This is not a forum for researchers it is for users, and if you have done Usability Engineering, you will know you are expected to build an information system that will provide the information that the users want.
Re: Keeping Only The Jbuttons In A Selected Row Active In A 2d Array by segsalerty(m): 3:49pm On Apr 24, 2011
csharpjava:

segsalerty

Thanks for the good work you are doing for people on this forum, please keep it up. I have found a way to get the Buttons the way I want them, but I will go through your code step by step and compare it with mine to see how you went about yours. Because of the nature of my project I cannot post my code yet, but, later I will come back and post the full code here for anyone that might want to build an application like mine.


u re so much welcome bro
All the best
Re: Keeping Only The Jbuttons In A Selected Row Active In A 2d Array by Fayimora(m): 4:07pm On Apr 24, 2011
funny  cheesy
Re: Keeping Only The Jbuttons In A Selected Row Active In A 2d Array by csharpjava(m): 12:58pm On Apr 26, 2011
segsalerty

Can you then from the row you have enabled above, only keep 5 Jbuttons including the one the one the user has selected.

Or is this impossible to do?
Re: Keeping Only The Jbuttons In A Selected Row Active In A 2d Array by segsalerty(m): 11:37pm On Apr 26, 2011
csharpjava:

segsalerty

Can you then from the row you have enabled above, only keep 5 Jbuttons including the one the one the user has selected.

Or is this impossible to do?

cant understand u , can u make urself clearer?
Re: Keeping Only The Jbuttons In A Selected Row Active In A 2d Array by csharpjava(m): 7:06am On Apr 27, 2011
segsalerty

From the row you have enabled above, I want only 5 Jbuttons including the one user has selected to be enabled.

Can this be done.
Re: Keeping Only The Jbuttons In A Selected Row Active In A 2d Array by segsalerty(m): 6:44pm On Apr 27, 2011
Sure, very well, U shld be able to do that irrespective of weather enbling 4buttons + the 1clicked on the row or 4random buttons + the 1clicked, Whatever, its just so easy to accomplish if u have being following the tasks from the scratch! I demand u post ur code here pls, Lets see if we are really helping u guyz. Lets work together and make all of us pro!
Re: Keeping Only The Jbuttons In A Selected Row Active In A 2d Array by csharpjava(m): 9:51pm On Apr 27, 2011
segsalerty

The project I'm working on at present needs to be kept secret for now, but later I will post the full code here, after the project has been completed.

Thanks for your help so far.
Re: Keeping Only The Jbuttons In A Selected Row Active In A 2d Array by segsalerty(m): 10:23pm On Apr 27, 2011
csharpjava:

segsalerty

The project I'm working on at present needs to to be kept secret :-Xfor now, but later I will post the full code here, after the project has been completed.

Thanks for your help so far.

You are so much welcome , no problem ! cheesy
Re: Keeping Only The Jbuttons In A Selected Row Active In A 2d Array by Nmeri17: 9:15pm On Jul 21, 2018
csharpjava:
Fayimora

Can you be quiet, you don't have a clue of what I'm trying to do, you think this is childs play.
cheesy grin cheesy grin

(1) (Reply)

How Can I Connect Php Script To Active Directory With XAMPP Server / Why It's Important To Be Very Well-rounded Today / How To Create Android App Like Instagram,twitter,2nd Steap In This Tutorial!!

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 65
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.