spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

When JButton Disable, the text shown in JButton(for some selected)


You can put this code on the click event of the button you intend to show the desired text.

Although your purpose isn't very much clear, I guess you need this:

    if(jButton1.isEnabled()==false){
        jButton1.setText("HIT");
    }

Edit as per your code:

Initially set the button's foreground color same as the button's background color.

    if (
ButtonArray[r][c].getText().equalsIgnoreCase("hit"))
{
        game.incCountHit(1);
       
ButtonArray[r][c].setForeground(Color.black);
        if (game.getCountHit() == 3) {
             startBtn.setText("Restart Game "); //
set text
        }

    }

Edit:

Assume a new array for storing the text with the desired rowSize and colSize which will be same as ButtonArray.

String[][] newArray = new String
[rowSize][colSize];

Random class:

 for (int y = 0; y < x; y++) {
    int randomThree = new
java.util.Random().nextInt(gridNo);
    int randomThree2 = new
java.util.Random().nextInt(gridNo);
    if (newArray[randomThree][randomThree2] !=
"hit") { 

        newArray[randomThree][randomThree2]="hit";

    }
 }

Main Class: Button Action:

       
ButtonArray[row][col].addActionListener(new
ActionListener() {
            public void
actionPerformed(ActionEvent arg0) {
               
ButtonArray[r][c].setEnabled(false); 

                if (
newArray[r][c].equalsIgnoreCase("hit")) {
                    game.incCountHit(1);
                    if (game.getCountHit() == 3) {
                        startBtn.setText("Restart
Game "); // set text
                    }
                }
            }
        });randHit.randomNo();

Categories : Java

Related to : When JButton Disable, the text shown in JButton(for some selected)
Painting on a JButton in java
You need to create a custom class which extends from JButton and then override it's paintComponent method See Performing Custom Painting, Painting in AWT and Swing and 2D Graphics for more details Remember though, a button typically has a lot going on, borders, content etc, this makes performing custom painting on them a little more tricky..

Categories : Java
"Unpress" JButton which is looking pressed
Resource sensitive tasks MUST not be performed in UI Thread (the thread which handles UI clicks and other activities visible to user), You need to separate the time consuming tasks to some other thread. Another option will be to show a progress bar or something like that till the action is over.

Categories : Java
Placing JButton on image
Edit Ah yes, as ControlAltDel points out, you're not adding your JButton to your drawing JPanel instance. Just do that, and your problem is solved. I don't see the bug in your code, so it might lie in code not shown. I do see a problem in that you're reading the image in, inside of paintComponent, something that should never be done as it will slow down painting and thus slow down the perceive

Categories : Java
Having problems displaying image within JButton
Actually the code compiles, and you have a runtime exception. That's because you pass "../geek.jpg" to the ImageIcon constructor, and that's not how resource resolution works. You should use "/geek.jpg" instead, provided the image will also be packaged along with your app (check your Eclipse deployment/packaging configuration)

Categories : Java
Move JButton setup to separate class
Your ImageButton class is wrong. You're holding an unnecessary instance to JButton inside. So, the implementation should be: public class ImageButton extends JButton { static final long serialVersionUID = 1; public ImageButton(Image image, Dimension imageSize) { super(new ImageIcon(image)); this.setMinimumSize(imageSize); this.setPreferredSize(imageSize);

Categories : Java
Recently Add
How to convert this for loop into an enhanced loop
Do-while, try-catch loop error
JavaFx use String with Double on table column
Equal Spacing In print statements
Android Poor Image Quality When Saving Image From takePicture callback
Why ConcurrentHashMap put() return null but the item is put successfully?
Executing the java program from command prompt
Query multiple Entity attributes with List of values
Android Collections.sort - doesn't work properly
Gdx.files.internal(...) wrapper not working correctly
How to do character math?
Android - How to stack the views added dynamically?
Java: implement a loop with duplicate values in Array
Graphics paint component and loop trouble
Java: Transfer a file from server to client and from client to server
Problems with struts2 interceptor
Beginners Java Blue J returning a String in a method
How to pass Array of complex objects as a IN parameter in a stored procedure using JPA 2
Android: Finding fundamental frequency of audio input
Generic Java Logger output null.null for class and member name
How to use semaphores correctly
Hystrix: HystrixBadRequestException for failed validations
Sending a file over java socket
How can I make a variable work in more that one class? (Java)
Java - What are the layout managers available in awt.* and swing.* packages?
Reading ints from file and storing value to an array
random integers between 2 values
Maven -- is there a command to download the parent poms of all dependency jars?
Null Pointer Exception in Array when adding ActionListener
Hystrix Request Caching by Example
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.