Java - Icon/image is grey on a disabled JButton - java

I'm making minesweeper for a school project. When the player wins or loses, the mines are revealed. Their buttons are disabled, and icons of flags/mines will appear. The problem is that the icons turn grey when the buttons are disabled. Is there a way around this?
I have also tried setting the text of the JButton to something like "<html><img src=\"res\\mine.png\"/></html>" but it showed some weird image.
Update:
I tried using setDisabledIcon() but nothing's showing up. Here's some pseudo-code
The buttons I use for the minefield is a class called Field, which extends JButton
mouseReleased(mouseEvent e) {
Field fieldClicked = (Field)e.getSource();
if fieldClicked is mine {
fieldClicked.setEnabled(false);
gameTimer.stop();
setLost(true);
loop through 2D array of fields {
if field is a mine {
field.setDisabledIcon(Field.mineIcon);// public static final icon of Field. mineIcon = new ImageIcon("res\\mine.png")
field.setEnabled(false);
}
}
}
}

Figured this out in a test
For some reason
clickedButton.setDisabledIcon(mineIcon)
Alone doesn't do anything.
But:
clickedButton.setIcon(mineIcon)
clickedButton.setDisabledIcon(mineIcon)
Will show whatever icon I wanted

a JButton actually allows seven associated images: the main image (use
setIcon to specify it if not supplied in the constructor), the image
to use when the button is pressed (setPressedIcon), the image to use
when the mouse is over it (setRolloverIcon, but you need to call
setRolloverEnabled(true) first), the image to use when the button is
selected and enabled (setSelectedIcon), the image to use when the
button is disabled (setDisabledIcon), the image to use when it is
selected but disabled (setDisabledSelectedIcon), and the image to use
when the mouse is over it while it is selected
(setRolloverSelectedIcon).
- http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JButton.html
so use setDisabledIcon(ImageIcon)

The greyed image is the automatically generated one, in case you want a different icon, use setDisabledIcon()
Icon disabledIcon = new ImageIcon("youricon.gif");
button.setDisabledIcon(disabledIcon);

Related

Edit "app" attribute of xml element

So I got following problem:
I'm currently developing a sensor app and I'd like to check if the sensors are available or not. If they aren't, I want to change the color of a button (which starts an activity where the value of the sensor is being displayed) to grey.
Sadly, I can't just change the background color of the button because I'm using the Circle Button Library by Markushi.
This button looks like this:
<at.markushi.ui.CircleButton
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/gyroscope"
app:cb_color="#color/colorMain"
app:cb_pressedRingWidth="8dip"
android:id="#+id/gyroscope"
android:layout_alignLeft="#+id/temperature"
android:layout_alignStart="#+id/temperature"
android:layout_below="#+id/title"/>
As you can see this attribute defines the color.
app:cb_color="#color/colorMain"
My Question is: How can I change this color programmaticly in this method?
public void testSensors() {
if (testManager.getDefaultSensor(Sensor.TYPE_TEMPERATURE) == null) {
}
}
EDIT:
setColor doesn't really work. There is this border left. (see image)
Also: I'm using #616161 as the new color.
EDIT2:
Found out, that the border is caused by the transparent circle thing which gets bigger, when the button is pressed. So basically removing this circle thing will be fine. I try to find an answer on my own :)
CircleButton button;
button = (Button)findViewById(R.id.buttonId);
public void testSensors() {
if (testManager.getDefaultSensor(Sensor.TYPE_TEMPERATURE) == null) {
button.setColor(Color.parse("#000000"));
}
}
If you are using normal button then
button.setBackgroundColor(ContextCompat.getColor(context,R.color.colorAccent));
You can use
1) Color.parse("#000000")
2) ContextCompat.getColor(context,R.color.yourColor)
You can use the setColor() method of the circleButton, as you can see from the source code here .
So basically what you need to do is get a reference to your circle Button, using the findViewById method of your activity. then
public void testSensors() {
if (testManager.getDefaultSensor(Sensor.TYPE_TEMPERATURE) == null) {
myCircleButton.setColor(Color.parse("#000000"));
}
}
Also if you are seeing the pressed ring, then that means the state of the button is somehow in the pressed state, so try setting it o false using the setPressed(false) method.
To answer this problem (if anyone has the same):
You can set the animation to 100. This somehow disables the circle.
For some reason the color of the button has now a fixed color (dark grey) but scince this is fine to me and I don't want to cry under my table, I just let it be and move on with my project.
mCircleButton.setAnimationProgress(100);
Thank you all so much for your help! :)

Displaying a JFrame as the result of a JButton click?

I am trying to create a JPanel to display when a user clicks a button within my main JFrame. In Netbeans I first used the wizard to add a new JPanel to my project, I then used the GUI creator to fill in all the content. I am not trying to display the JPanel with the following code
private void m_jbShowSelAccResultsActionPerformed(java.awt.event.ActionEvent evt)
{
Account selAcc = getSelectedAccount();
if(selAcc != null)
{
AccountView accPanel = new AccountView(Account.getDeepCopy(selAcc));
accPanel.setVisible(true);
}
else
ShowMessage("Please select an account to view");
}
But nothing happens, no error is thrown and the JPanel is not shown. So I then changed the JPanel to a JFrame (Netbeans didn't complain). When I try again with the same code I receive the error GroupLayout can only be used with one Container at a time.
How can I display my JPanel/JFrame?
To change views within a Swing GUI, use a CardLayout as this is a much more robust and reliable way to do this.
Don't try to blindly "change a JPanel to a JFrame". It looks like you're just guessing here.
GroupLayout can't be reused as the error message is telling you. Likely this error comes from the point above. If you avoid trying to make a JFrame out of a JPanel, the error message will likely go away. As an aside, GroupLayout is not easily used manually, especially if you're trying to add components to an already rendered GUI.
So for instance, if your program had a JPanel say called cardHolderPanel, that used a CardLayout, this held by a variable say called cardLayout, and you've already added a "card" JPanel to this holder for accounts, say called accPanel, and if the accPanel had a method to set its currently displayed account, say setAccount(Accoint a), you could easily swap views by calling the CardLayout show(...) method, something like:
private void m_jbShowSelAccResultsActionPerformed(java.awt.event.ActionEvent evt) {
Account selAcc = getSelectedAccount();
if(selAcc != null) {
accPanel.setAccount(Account.getDeepCopy(selAcc));
cardLayout.show(cardHolderPanel, "Account View");
}
else {
showErrorMessage("Please select an account to view");
}
}

Popup Menu Appears Somewhere Else on JFrame

I have Jframe with multiple textfields and textareas , I wanted to add copy paste functionalty to Jtextfields and Jtextareas . as you can see in the picture when I right click on Product Name field , it shows copy paste somewhere else on frame.
https://drive.google.com/file/d/0B2tIFybzjEheNTRUSTB1dTNPdEU/edit?usp=sharing
this is the event i have added to textfield
private void jTextField1MouseReleased(java.awt.event.MouseEvent evt) {
if(evt.isPopupTrigger())
{
jPopupMenu1.show(this,evt.getX(),evt.getY());
}
}
MouseEvents are contextual, that is, the location specified by the MouseEvent is local to the component that generated the event...
Try using...
jPopupMenu1.show(evt.getComponent(), evt.getX(),evt.getY());
instead
JComponent.setComponentPopupMenu(meu) also can be used.

Text Missing On Checkbox Component for Swing After Adding Action

I'm writing a simple Swing app. I tried adding a checkbox as listed below. Once I added the actionHandler loadPickers the name Foo disappeared from where it was sitting next to the right of chckbxNewCheckBox. I tried adding a call to setHideActionText(), but now nothing displays.
JCheckBox chckbxNewCheckBox = new JCheckBox("Foo");
chckbxNewCheckBox.setToolTipText("");
chckbxNewCheckBox.setName("");
chckbxNewCheckBox.setHideActionText(true);
chckbxNewCheckBox.setAction(loadPickers);
mainPanel.add(chckbxNewCheckBox, "flowy,cell 0 1");
If I change it to this it works properly. I see the text "Foo".
JCheckBox chckbxNewCheckBox = new JCheckBox("Foo");
chckbxNewCheckBox.setToolTipText("");
chckbxNewCheckBox.setName("");
chckbxNewCheckBox.setHideActionText(true);
chckbxNewCheckBox.setAction(loadPickers);
chckbxNewCheckBox.setText("Foo"); //THIS DOES NOT WORK IF IT COMES BEFORE SET ACTION
mainPanel.add(chckbxNewCheckBox, "flowy,cell 0 1");
I've included the action here for completeness. Why does it work this way? Am I missing something here? Currently I'm using the WindowBuilder plugin for Eclipse with the Mig layout system (which I really like). Unfortunately I haven't figure out if there's a way to make WindowBuilder use the .setText() method instead of using the constructor. Any help on what I'm doing wrong, any insight on why this behavior exists like this, or a good workaround for WindowBuilder would be great.
private class LoadPickers extends AbstractAction {
public LoadPickers() {
//putValue(NAME, "SwingAction_2");
putValue(SHORT_DESCRIPTION, "Some short description");
}
public void actionPerformed(ActionEvent e) {
}
}
As explained in the JavaDoc of AbstractButton.setAction:
Setting the Action results in immediately changing all the properties described in Swing Components Supporting Action. Subsequently, the button's properties are automatically updated as the Action's properties change.
So all the following properties can be impacted by setting an action:
enabled
toolTipText
actionCommand
mnemonic
text
displayedMnemonicIndex
icon (NA for JCheckBox)
accelerator (NA for JCheckBox)
selected

Setting up a Gallery of Images with NetBeans

I want to display a set of images (with associated text) on my window. I want to iterate through them using a previous and a next button. So far, I have only been able to associate the image with a JLabel. =/
How do I go about doing the rest? Should I use a different container for the complete set? Should I load the images on a data structure like an ArrayList, or is it enough to keep them on a folder? How can I add the event handling so that pushing the button displays the next or previous image?
Here is a screenshot of what I have so far.
Are you still here ?
I assume that you have found how to load the path of each of your images (if they are inside the same folder). You should store the path of the directory in a global variable, and then the name of each image into a Vector if you want to iterate through them. Just store the name of the files, not the entire images.
You also have to store the index of the current image as a global variable.
If you use a JFrame as your main window, you have to specify that it implements the class ActionListener this way:
public class MyClass extends JFrame implements ActionListener
Then you have to attach the event handler to your buttons (JButton). This must be placed inside the constructor of your window (MyClass):
nextButton.addActionListener(this);
previousButton.addActionListener(this);
Having implemented ActionListener, your class has to define the method actionPerformed. Inside it, you must change the content of the image according to the button that has been pressed.
public void actionPerformed(ActionEvent e)
{
Object o = e.getSource();
if(o == nextButton)
{
currentIndex++;
if(currentIndex == vectorImages.size())
{
currentIndex = 0;
}
//Change the image in the JLabel
label.setIcon(new ImageIcon(vectorImages.get(currentIndex)));
}
else
{
//Iterate backwards
}
}
Hope this helps...

Categories

Resources