The program I am writing has a Swing GUI and several of the components have Tooltips. These are all on JButton, JCheckBox and JRadioButton components, they are set using the setToolTipText method and all work perfectly. I have just tried to add one to a custom component that extends JPanel using the same method and no tool tip appears.
The JPanel contains 2 JLabel components and a JSlider. I tried to override the setToolTipText method and then use it to call setToolTipText on the slider. This didn't work either.
public void setToolTipText(String text) {
super.setToolTipText(text);
slider.setToolTipText(text);
}
Am I doing something wrong? or can't you have a tooltip on a JSlider or JPanel. I would have thought that it should work on anything that extends JComponent.
The control is disabled when the program starts although even after it is enabled there is still no tool tip if that is relevant. It doesn't really need to show it until the control is enabled anyway.
Thanks.
try settings the tooltip to the JLabels as well. they may be obstructing the JPanel.
Try making the following call:
ToolTipManager.sharedInstance().registerComponent(customComponent);
Related
I am developing a game, where you first get to the main screen, there are multiple selections to go, for example, Singleplayer, Twoplayer, Credits, etc.
I have one big problem. If I click a button in the menu, (not JButton) the JPanels switch, but the keyListener is lost. The Keylistener is in the same class as the game code, which implements JPanel. I tried everything to get the Keylistener to work, but it just won't.
Here is how the things are called: Main class --> Menu --> Game. I tried adding the keylistener to the main class, but it's not working.
So, JPanel switching is ok, but the Keylisteners are gone. I was developing the game before with new JFrames, so when I clicked a menu, a new frame was created. I didn't insert a code here, because it's too long (2000+ lines), and the KeyListener is working, but only when it is in a new JFrame. I set the mode int in the Menu class, by clicking a button.
This is currently my panel switch:
public void setJPanel() {
switch (mode) {
case 1:
getContentPane().add(s);
validate();
break;
case 2:
getContentPane().removeAll();
getContentPane().add(sp);
validate();
break;
}
}
Thanks for your help in advance!
Rather than use a KeyListener, have you given thought to or tried using Key Bindings? KeyListeners require that the component being listened to has focus, and focus may be lost for many reasons, especially when swapping views (are you using a CardLayout for this?). Key Bindings on the other hand can be set to be responsive even if the bound component doesn't have focus but when it is only held within a window that has focus. Tutorial: Using a CardLayout
Edit
I see that you're not using a CardLayout, and I suggest that you use this as it can make your view swapping cleaner and easier.
Edit 2
I agree that you don't want to post your entire 2000+ line program here as no one will have the time to read it, but consider condensing your question/problem into a single small class that is compilable and runnable by any and all of us, and demonstrates your problem. In other words, a Short, Self Contained, Compilable, Example or SSCCE .
Remember, the code should be compilable and runnable for many of us to be able to understand it fully.
Cardlayout actually is screwy while refocusing.
#op, try calling requestFocusInWindow() after the new jpanel was added
Try using myPanel.requsetFocusInWindow();
before using setVisible(true);
I'm quite new to Swing, and I'm programming an application with NetBeans' UI designer.
Now I have an JPanel called "editorPanel", and it must be able to display multiple things. (so, sometimes it has to display an image, and sometimes it has to display a text editor)
I have made separate panels for this, so say I'd have a JPanel called ImagePanel and one called TextPanel. It has to switch easily between them, so I tried this:
editorPanel = new ImagePanel();
But that didn't work.
So, what I want to do, is set an empty panel to a defined panel.
How can I make this work?
The proper way to achieve your goal is to using a card layout and switching panels accordingly.
You ca get some idea on how card layout stuff is working in here
I have a Java class that extends JTextField and covers it with a JLayer (new Java 1.7 feature) in order to display custom graphical effects in certain conditions. Without the JLayer it is easy enough to add a tooltip using setToolTipText(String). But with the JLayer in place, nothing happens when I hold my mouse over the box. Perhaps it is blocking the mouse-over event? I also tried calling setToolTipText() on the JLayer object itself but it didn't make any difference. So how can I get the tooltip to work?
Did you try overriding getToolTipText and return getView().getToolTipText()? Doubt it will help though.
I came up with a recently problem that, any component that I add to a JPanel (JTextField, JTextArea, JTable) can't edit even when I force it, in code, to be enabled and editable. I'm using NetBeans for developing the project.
Anyone here faced this problem? Looks like I'll be forced to change all to JFrame. Even though, hope that someone reply this topic with some constructive idea/help.
Try to call setFocusable(true) for the top level container.
I have two reasons/opinions/thoughts as to why you are getting "errors".
You aren't adding the components to the panel correctly.
panel.add(someComponent);
panel.add(anotherComponent);
panel.add(yetAnotherComponent);
frame.add(panel);
You are overriding the default behavior for the components. All components in a JFrame, JWindow, etc. are by default enabled and editable (meaning there is an implied setEnabled(true) and setEditable(true), respectively).
I have run into yet another issue with my program. I have made several JButton sub-classes to do specifically what I need them to do. The problem is that the buttons don't show up until I either click where they are supposed to be or if I hover the mouse over them (when I had setRolloverEnabled() to true). I originally had them all set for setRolloverEnabled() to true. But I understood that when I did hover the mouse over top of them, it had an ugly blue outline of the button which I didn't like at all. So is there any way to make them visible without having to hover over them, or without having to click them?
I have a background on my JFrame (I sub-classed JPanel and overrode the paintComponent() method) allowing JFrame to maintain its role as a container). Also in Adobe Photoshop I have designed the buttons and on the outer edge it has some transparency, I saved the files as .png so the transparency would be kept, but when the buttons are placed in the frame, there is an ugly blue outline still where it should be transparent. Any help on that.
Any suggestions would be appreciated. Below is the code for one of my Button classes.
public class Button extends javax.swing.JButton {
//This Button class is not the AWT Button object.
//It is a custom class designed by me.
public Button(ImageIcon normal){
setRolloverEnabled(false);
setVisible(true);
setIcon(normal);
setSize(normal.getIconWidth(),normal.getIconHeight());
}
public Button(ImageIcon normal, ImageIcon rollover){
this(normal);
setRolloverIcon(rollover);
}
public Button(ImageIcon normal, ImageIcon rollover,ImageIcon selected){
this(normal,rollover);
setSelectedIcon(selected);
}
}
There is a good chance you are not creating your GUI in the Event Dispatch Thread. Swing painting is single threaded by design. If you try to draw your components (even if you dont do it on purpose), the results will be varied. There are well documented methods to properly instruct the jvm to paint your components, including SwingWorker and SwingUtilities.invokeLater(Runnable). Take a look at this tutorial to get more info.