Manipulating a JToggleButton's ImageIcon while pressed/selected - java

I (very miraculously) answered my own question while writing this question , but it was such a find I wanted to share with everyone. I understand these should be true "questions" but it was a monumental find for me. There is a "question" below that can be answered, however.
I created a custom class that extends a JToggleButton. In this class, I remove all default MouseListeners so it will only accept mouse actions when I deem the object ready. The problem I had was manipulating the JToggleButtons ImageIcon (set with setIcon()). If the button was not displayed on screen (not visible due to a JScrollPane) when I told the button to be selected, the ImageIcon would disappear. Similarly, if I modified the ImageIcon while the button was selected, the ImageIcon would also disappear.
It turns out that in my custom MouseListener, I was using getModel.setSelected(true) instead of getModel.setPressed(true). Apparently I don't have a clear understanding of the difference between selected and pressed, but suffice to say this fixed my issue. My "question" would therefore be clarification on these two terms.
To summarize, don't use setSelected() on a button when you mean to use setPressed(). What a pain to track this one down! I sincerely hope this helps someone else. Email is valid but a spam-catcher.

Ok, I found my own answer again, so I thought I'd share and close this question. A button is "selected" when it has a checkbox/radio icon. A button is "pressed" when the button has been pushed. JToggleButtons appear to use both attributes in combination to properly "click and press" the button.
The reason the ImageIcons were disappearing was because I had inadvertently mixed setPressedIcon()/setPressed() and setSelectedIcon()/setSelected(). Because I had confused the terminology and didn't recognize the difference, I was inconsistent in my usage.

Related

How to create button go to next questions in Quiz Software

I'm creating a Quiz Software with 40+ questions. Problem is when I click the "Next" Button, I would like to make the current question and answer go and a new set come there in the same frame while also keeping the marks user got for previous question. I can make 40+ Jframes Forms but that would too complicated.
UPDATE: Simply when I click "Next" Button, the question in question box and answers of the radio buttons will change. But this has to be done over and over again, by same button. There's also this previous button that will remove the current question and go back to the last one.
To make things more complicated, each time the there will be different IF selections as the the radio button congaing correct answer also changes.
As I said I can design JFrame forms for each but I'm having a hard taking the marks of each question for one frame (called "Final Result") And there's also this timer that countdown from one hour. I don't think I can do either of these by creating 40+JFrame forms.
You can create just one Frame and on the 'Next' Button Handler, Change Label Text, Check Box Text (Assuming Multiple choice questions)

How to completely remove a button's background instead of just hiding it

I'm trying to create this game
in Java for my school A-level course. I'm currently trying to use an individual button for each of the squares and cannot in any way remove the buttons's background. I've tried setBackground(false) which hides the background, but it is still there so still clickable, which you can imagine completely screws up the board.
Ultimately what I'm asking is how to make a button from a picture the same size as the picture, without any extra backgrounds at all, not just hidden.
Here is a picture of what I mean:
Any help/suggestions would really be appreciated, I'm desperate!
There was a question on here for creating custom buttons with java:
Creating a custom button in Java
The answer to this question might solve your problem.

Swing - Changing the content of a panel using UpdateUI

I am going through a legacy application which is using Swing and i am struggling to figure out how the screens are changing when a user clicks a button. One of the reasons i cant figure this out is because this is the first time i am using Swing. I have read a book and got the basics but still struggling.
Basically, the screen i am looking at has a JSplitPane which has a number of shortcut buttons on the left and an empty pane on the right. When i click on the button, the right side pane is populated with a different screen depending on the button pressed.
Going through the code, i was expecting somewhere that there will be something that calls a setVisible() method depending on which button is pressed.
The actionPerformed method for each of the shortcut buttons looks something like this:
void shortCutBtn_actionPerformed(ActionEvent e) {
propertyChangeListeners.firePropertyChange("selectedShortCut", previousShortCutSel, currentShortCutSel);
mainPanel.updateUI();
}
I have gone through most of the code and came to a conclusion that the above code is what is causing the frame switching but i dont understand how that is happening.
Each screen is identified by a numeric constant. In the above code example, previousShortCutSel and previousShortCutSel refer to a numeric value that represents individual screens screen.
I have tried to look for documentation of how updateUI() works but i am lost. How does the above cause the content of the right panel of the JSplitPanel to be updated with a new screen?
This is not an appropriate use of updateUI(), which "Resets the UI property to a value from the current look and feel." As the example itself may be unreliable, consider studying another. GoogleOlympiad, for example, sets a label's icon using a (cached) image.
ImageIcon image = getImage(index);
imageLabel.setIcon(image);
(source: drjohnbmatthews at sites.google.com)
As per comments by ziggy (glad it helped)
Have a look at the PropertyChangeListeners that appear to be added in the code. In particular the propertyChange(PropertyChangeEvent e) method is where the code which changes the content will be present.
+1 to trashgod nice example/advice as always

What is the border type of the digichat login button?

I tried many methods to make a JButton such as the "Connect" button which appears in the digichat applet,
but I failed! Oh, I want know: What is the border type of this button? I attached a photo to explain the button; can any one help me with ideas, or tell me how to create it?
It may very well be a custom border, implemented entirely from scratch or a CompoundBorder which is a combination of several standard borders.
There's no way to tell how they have solved it based on the screen shot.
The appearance is defined by the old Mac OS 9 Look & Feel, as shown here:
Although it's a considerably more laborious alternative, you can implement your own ButtonUI, as illustrated here and here.

How do I prevent button surround from displaying in Java?

Sorry for the odd choice of words for the title, however, "border" seems to be the inappropriate term. While it is true that the visible line surrounding an icon in a JToggleButton can be made invisible by using setBorderPainted(false), the same is not true for JCheckBox and JRadioButton.
I can not use the JToggleButton and therefore need to use either the JCheckBox or JRadioButton (or some derivative of JToggleButton I am not aware of), but need the square or circle, respectively, to be non-visible when there is no icon on the button. Also, using setVisible(false) eliminates the button from the layout, however, I need the space to be reserved and not have the component layout change (using GroupLayout).
Any suggestions? Am I going to have to create a custom renderer? I will be looking at that in the mean time.
The route into this would be through customising the look at feel by changing some of the UI properties in the UImanager (the sort of thing that allows you to make simple tweaks with fonts and colours and presumably the images used for the checkboxes or radiobuttons) -- but it's many years since I last did that sort of thing and can't remember the details.
A little Googling turned up this project to inspect current property values, so might at least help with indicating the right part of the APIs to be looking at.
You have to choices here:
1) Customize Look and Feel as described in previous entry.
2) Create your own custom controls by inheriting from existing ones and overriding component painting.
I found a cheap and easy (read hack) for this. I created an empty transparent icon and used it when I didn't want any item to be displayed.

Categories