I have a JSlider in my GUI that goes from 0 to 100. For some reason, there is text above the slider position that displays the current value of the slider and it follows my slider around. I.e., if I move my slider halfway, "50" appears above where my slider currently is. However, I can't figure out what that text field is called, all I know is its part of the slider.
I want to either remove it or be able to change it to something else, how would I do that?
I would have just commented on Etaoin's answer, but I don't have the reputation for it yet.
At any rate, call this before instantiating the JSlider:
UIManager.put("Slider.paintValue", false);
Note the capital 'V' in paintValue.
Furthermore, to print all of the fun things that are currently set by your UIManager, enter this code anywhere after the UIManager has been instantiated:
Iterator<Entry<Object, Object>> it = UIManager.getDefaults().entrySet().iterator();
while(it.hasNext())
System.out.println(it.next());
This bothered me on a project once, and I found the following workaround. Call this once before instantiating your JSlider -- I put it in a static block in my JPanel subclass:
UIManager.put("Slider.paintValue", false);
That'll take care of it.
Do you mean a tooltip? Is it a little box that appears over the slider control and follows your mouse around if you grab the slider?
If this is the case, you can disable it simply by calling setToolTipText( null ), or override the text with setToolTipText( "My Text" ).
As I see there is a setLabelTable method in JSlider with a Dictionary parameter.
I have not used it but I guess that if you set it to an empty dictionary (or empty labels for every possible value) then no labels will appear.
A better solution is the usage of setPaintLabels with a false value.
Related
This is going to be on a form used as a sort of search function. It will be an array of JLabels, with things like first names, last name etc.
The user will add names to the list one by one for submission to a database. I will use a prepared batch statement with some .getText()'s to get my text. It may sound complicated but it seems to be working.
What if the user is looking and wants to remove a name?
I'm thinking a JLabel that is hidden (setVisible(false)) until MouseEnter. Using the mouseClicked action to call a method to do the dirty work.
My question is, if I add a JLabel to a JPanel, and set it's visible property to false, is it still actually there, and can I set it visible with a mouseEnter action?
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
I need help working getFocusOwner(). I have a Sudoku game that I made in Java and I want to add arrow key navigation to the program. I did some research and found that using the focus system would be the best way (If it's not, please give me a better way and I can research that).
Okay, so for testing purposes, I'm trying to set focus to SetField, a custom class extending JTextField(Custom because I wanted to disable any inputs that weren't numbers). It has been set to focusable. I called requestFocusInWindow() on the SetField in the middle of the grid, and the focus is set to that component. The problem arises when I try and retrieve the component that has focus.
This is what I'm doing to test the getFocusOwner() method:
sGrid[40].requestFocusInWindow();
try{
System.out.println(this.getFocusOwner().getClass().getSimpleName());
} catch(NullPointerException e){
e.printStackTrace();
}
No matter what component I try this on, I always get the null pointer exception. I tried it with the JButtons, JLabels, and even on JPanels in my program. The focus is set to the component though. I can see the caret blinking in the intended SetField. Is there something I'm doing wrong? Thanks in advance for any help.
Instead of a custom component to only allow numbers, just us a JFormattedTextField.
Do you need to even determine which component has focus, why not add a KeyListener, MouseListener? I am not exactly sure what you doing with the focus but it seems strange.
I want to add arrow key navigation to the program.
You should be using Key Bindings. Create a basic "ChangeFocusAction". You will need 4 instance of this class that you can map to a KeyStroke.
Assuming you have a array of 81 text fields your Action could be created with an integer value that tells the Action how to change focus. For example:
right = -1
left = 1
up = -9
down = 9
The source of the ActionEvent will contain the text field that has focus. Then you search the arry to find the offset of that text field. Then you add the int value from above to the index and request focus on that component.
Just found out the problem. For anyone else that's having a problem with this, try using (Window).getMostRecentFocusOwner(). This worked for me.
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.
Here's a very specific coding question:
I've recently been asked to maintain some old-ish Java Swing GUI code at work and ran into this problem:
I've attached my own subclass of InputVerifier called MyFilenameVerifier to a JTextField (but it may as well be any JComponent for these purposes). I've overridden the verify() method such that it calls super.verify(input) (where input is the JComponent parameter to verify()). If super.verify(input) comes back false, I do:
input.setBorder(BorderFactory.createLineBorder(Color.RED));
This is a convention used throughout the UI of this application that started long before me, so I don't have a lot of choice as far as using other ways to get the users attention (wish I did). This is just the way it works.
Problem is, once the user goes back and types something valid into the text field, I need a way to set it back to default border (instead of just saying set it to Color.GRAY or whatever, which is a different color from its original border). I need a way to say, "remove the extra decoration and go back to normal" or just set the border to its default, in other words.
Couldn't you just call input.getBorder() and cache it somewhere before setting the border to red?
Or without caching anything, you could tell the JComponent to update its UI back to the look and feel's defaults via component.updateUI. That should make the component reset its colors, borders, fonts, etc to match the original settings.
input.getBorder()
Wouldn't it be awesome if no one ever saw this and I got away free without the ass-beating I deserve for asking this question?
Not sure how your system is build, but I think you can store the original border before changing it. So you can change it back later
// assuming the Border was not null before
if (!super.verify(input)) {
original = input.getBorder();
input.setBorder(...);
} else {
if (original != null) {
input.setBorder(original);
original = null; // not needed
}
}
You need to preserve the existing border when you change it.
One way to do this is to use the methods putClientProperty() and getClientProperty(), which you'll find documented in the API.
Another possibility, if there are only a few input widgets you need this for is to subclass, e.g. JTextField, add setBorderOverride() and modify getBorder() to return "overriddingBorder" if it is not null.
Then you just use setBorderOverride(redBorder) to make it red and setBorderOverride(null) to clear it.
This of course depends on the painting to use getBorder(), which it may or may not do, and which may be implementation specific.
Incidentally, you only need a single static reference to the border-- it's the selfsame border instance used by all JTextFields.