JButton Sub-Class Not Displaying Button until Clicked/Transparency Error - java

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.

Related

Jbutton is moving components in different panel

I have searched for this problem and haven't found anything close. I will try to be specific and post code however this is a large program so I can't post all the code. The problem in general is this: A JButton on one panel causes components on another panel to shift at first click. This only occurs when there is an action listener added to the button. (clicking the button without an action listener doe noting (obviously)). The problem is that the action listener i add only changes the button background, text, and size (to fit new text).
Here is the action listener as of right now. login is the JButton:
private class LoginListener implements ActionListener{
public void actionPerformed(ActionEvent e){
loggedIn = !loggedIn;
if(loggedIn){
login.setText("Logout");
login.setBackground(Constants.RED);
}
else{
login.setText("Login");
login.setBackground(Constants.UPPER_BOUNDARY);
}
login.setSize(login.getPreferredSize());
}
}
The setup is this. The action listener is a subclass of the loginPanel where the login button is located. That loginPanel is added to the main JFrame at the upper 1/4 of the frame. The lower 3/4 of the main JFrame is mainPanel which has other swing components. The loginPanel and mainPanel do not share components or variables or really know of each other's existence (as far as I have coded). Yet when this actionlistener above is added to the login button components in the mainPanel shift from their positions to other positions. The only happens at first click and then they stay where they are at (not where i want them).
Other factors:
- I use absolute positioning (sorry if you don't like it but I like it better)
- I am using a SynthLookAndFeel but have never had this issue with this look and feel before.
Thanks
Other factors: - I use absolute positioning (sorry if you don't like it but I like it better)
There's nothing to be sorry about, and the solution is simple: Don't use absolute positioning, but instead learn about and use the layout managers to there full abilities. One of the reasons to use them is to avoid pernicious bugs like this one. It's quite possible that your code is in fact using a component's default layout manager even now without you knowing about it. You can find out more about them here. One of the keys to using them well is to nest them by using nested JPanels, each using its own layout manager. Then they can do the heavy layout lifting for you automatically.

How can I change colors of components when the mouse is pressed in a JFrame in Java?

I am coding a piano in java using rectangles from the java.awt.graphics library. I am using a mouselistener to play the sound of each individual key when the mouse clicks a certain area on the JFrame.
How would I add a shape to the panel upon clicking, repaint, then repaint the keyboard back over top when the user releases the mouse?
Consider adding JLabels to a JPanel that uses GridLayout. Give each JLabel a MouseListener and either swap ImageIcons on mousePress/mouseRelease or change the JLabel's background with press and release. If you go the latter route, you'll want to make sure that the JLabels opaque property is set to true so that the background colors show.
Then for the black keys, you can add the above JPanel to a JLayeredPane and on top of this, add another JPanel that holds the black keys that function in the same way.
Also, you'll want to take care to "play" any notes in a background thread, such as can be obtained with a SwingWorker so as not to tie up the Swing event thread and completely freeze your program.
Consider solution: source
It might not be exactly what you're after, but it might give you an idea of how to approach your problem. It took me a long time to figure out how to use JLayeredPane without setting a null layout, but in the end this was the best I could come up with. Also, assumed some naming conventions for your sound files. :p

JPanels, Listeners and Threads

For a school project I'm writing a BlackJack card game in JAVA.
After a few steps within the program, my whole computer system slows down/stutters and some areas within the JFrame turn black. No repainting seems te be happening.
I will give you some program details in words rather then submitting the code.
I have done some extensive extending of almost every GUI component to give it the look and feel that I want. Almost every child of JComponent that I use has got its paintComponent rewriten with custom image backgrounds and anti-aliasing where applicable.
In my (custom) JFrame I load an extended version of JPanel. This panel holds all the GUI components for that screen. When I want to switch screen (go to a different part of the program), I switch to another extended version of JPanel. I do this by removing all elements from the JFrame and add the new panel. These different panels implements ActionListeners and FocusListeners.
At this point my thoughts are leaning towards a thread handling issue. My theory for the moment is this: When a panel is constructed for display in the JFrame (each on different stages within te program), the previous constructed panels aren't realy 'destroyed', and the Listeners of those panels remain active.
My question for you people is, wether this theory holds ground... And in that case, is there a way to 'kill' these panels or at least stop the listening?
If my theory doesn't make sense, what else could be causing these symptoms? Could it be the extensive overwriting of the paintComponent methods?
Any ideas would be highly appriciated!
Regards Wilco
When a panel is constructed for display in the JFrame (each on different stages within te program), the previous constructed panels aren't realy 'destroyed', and the Listeners of those panels remain active.
No. Events are only dispatched to the component that has focus. It a comonents doesn't have focus then it won't received system generated events like KeyEvents and MouseEvents. So if a panel isn't visible then it won't receive events.
I switch to another extended version of JPanel. I do this by removing all elements from the JFrame and add the new panel.
This is not the best design. It is better to use a Card Layout which was designed for this purpose.
Almost every child of JComponent that I use has got its paintComponent rewriten with custom image backgrounds and anti-aliasing where applicable
Then you probably have problems with your custom painting. What happens when you just use the standard components without custom painting?

Java tool tip not displaying

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);

Working with frames and Java AWT

I am currently making a program with the AWT GUI and I'm running into a problem. I basically want an image in the top left hand corner of the screen, and a column of buttons on the right of the image. This isn't what's happening though. When I run the applet, I click a popup saying "Start Program" and then the picture I want is in the applet window itself and the column of buttons is in another window by itself. This is what it looks like:
Is there anyway to fix this so that the image and the buttons are in the same window?
Yeah. You're creating a frame but your graphic isn't inside the frame. Can't tell much without the code, but the AWT Tutorial at java.sun.com isn't bad on this stuff.
Okay, a little more (I haven't used AWT in a long time.)
Here's the couple of issues you have. A Frame is a kind of Window -- it wants to be a separate window with its own close button and so forth.
When you create your graphic, you have to tell it was component its parent is; you're somehow parenting it to the Applet. So you have some piece of code that looks like
add(myComponent);
in the context of the Applet as this.
public class myApplet extends Applet {
// lots of stuff here creating your canvas, putting the image in it
// and so forth. There's an example, see fn 1.
// When you're done, you have a component, call it myImage.
add(myImage);
}
You have a Frame, and you're adding your buttons to that.
public class MyFrame extends Frame {
add(new Button(...));
add(new Button(...));
}
You need to move the code that adds your Canvas into the Frame class in some method.
(WARNING: this is not complete Java code, I don't recall the names of the right methods offhand. Probably the init() method in the Applet, at least.
fn1. http://java.sun.com/developer/onlineTraining/awt/contents.html#simpleexample

Categories