JButton shows light blue background on Windows - java

I have the following code to make a custom looking JButton
ImageIcon icon = createImageIcon(
CommonUtils.class.getClassLoader().getResource("images/wright.png")
);
RightSlide.setIcon( icon );
ImageIcon icon2 = createImageIcon(
CommonUtils.class.getClassLoader().getResource("images/right_selected.png")
);
RightSlide.setPressedIcon( icon2);
RightSlide.setSelectedIcon(icon2);
RightSlide.setRolloverEnabled(true); // turn on before rollovers work
RightSlide.setRolloverIcon(icon2);
RightSlide.setBorderPainted(false);
RightSlide.setFocusPainted(false);
RightSlide.addActionListener(new ActionListener(){
The code generates a custom button. The button behaves as expected when hover over, pressed, clicked, and selected. This works on MacOS and Linux (Ubuntu). But the same code has a light blue background on Windows. Where does this come from and how do I get rid of it ?
Thanks

I think that you missing JButton#setContentAreaFilled(false); example here

Related

Codename one Tabs Button Styling

I'm stuck with this Codename one UI component for two days and still couldn't get a proper solution...I added tabs in my application but when I run the application, the tabs I added don't look as I expected.
The height of my tab is too big and I want to reduce it.
See the screenshot of how the tab currently looks below:
And here is the code I'm using in my application:
Tabs t = new Tabs();
Style s = UIManager.getInstance().getComponentStyle("Tab");
FontImage icon1 = FontImage.createMaterial(FontImage.MATERIAL_QUESTION_ANSWER, s);
Container container1 = BoxLayout.encloseY();
Container container2 = BoxLayout.encloseY();
t.addTab("Tab1", icon1, container1);
t.addTab("Tab2", container2);
hi.add(BorderLayout.SOUTH, t);
hi.show();
Open your GUI Builder and style the Tab Uiid to reduce the padding as necessary:
Style the Unselected, Selected and Pressed to have the same padding size, see the image below to know the key elements to touch:

Java (JFace Application Window) Setting external label text

I am looking to figure out how to set the text of a label on an external Application Window.
What I have:
I have two windows so far. The first one is the main application window that will appear when the user starts the program. The second window is another separate window that I have created specifically to display a custom error window.
The problem: I seem to be unable to call the label that I have created on the error window and set the text to something custom. Why? I want to be able to reuse this window many times! This window is aimed for things like error handling when there is invalid input or if the application cannot read/save to a file.
I was going to post screen shots but you need 10 rep for that. It would have explained everything better.
Here is the code for the label on the Error_dialog window:
Label Error_label = new Label(container, SWT.NONE);
Error_label.setBounds(10, 10, 348, 13);
Error_label.setText("Label I actively want to change!");
Here is the condition I would like to fire off when it is met:
if(AvailableSpaces == 10){
//Set the label text HERE and then open the window!
showError.open();
}
I have included this at the top of the class as well:
Error_dialog showError = new Error_dialog();
Just save the label as a field in your dialog class and add a 'setter' method. Something like:
public class ErrorDialog extends Dialog
{
private Label errorLabel;
... other code
public void setText(String text)
{
if (errorLabel != null && !errorLabel.isDisposed()) {
errorLabel.setText(text);
}
}
You will need to use your dialog like this:
ErrorDialog dialog = new ErrorDialog(shell);
dialog.create(); // Creates the controls
dialog.setText("Error message");
dialog.open();
Note: you should stick to the rules for Java variable names - they always start with lower case.
Further learn to use Layouts. Using setBounds will cause problems if the user is using different fonts.

Why is the application not showing upon adding icon to JMenuItem?

I've been running into a problem lately where I try to se a JMenuItems icon which when I define and point to the icon the application it self don't start/show.
I started looking for errors, but there where none; started looking for write occurencies, which typically pretty much does appear when I add the icon and after that as pointed above the application doesn't start/show.
When the icon is set but commented:
Code
mntmMaximize = new JMenuItem();
mntmMaximize.setText("Maximize Window");
mntmMaximize.setActionCommand("maximize");
mntmMaximize.addActionListener(this);
mntmMaximize.setMnemonic(KeyEvent.VK_PLUS);
mntmMaximize.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, ActionEvent.CTRL_MASK));
// mntmMaximize.setIcon(new ImageIcon(Vision.class.getResource("xray/fullscreen16x.png")));
mnWindow.add(mntmMaximize);<br>
Picture:
Screen Shot Of Visible Application
After the icon is set and trying to execute application:
Code:
mntmMaximize = new JMenuItem();
mntmMaximize.setText("Maximize Window");
mntmMaximize.setActionCommand("maximize");
mntmMaximize.addActionListener(this);
mntmMaximize.setMnemonic(KeyEvent.VK_PLUS);
mntmMaximize.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, ActionEvent.CTRL_MASK));
mntmMaximize.setIcon(new ImageIcon(Vision.class.getResource("xray/fullscreen16x.png")));
mnWindow.add(mntmMaximize);<br>
Picture:
The window not created and application terminated
Note that when the window is not created in this picture the application is therefor terminated.
Please try to answer nice, and if you need the whole source file it is possible.
Edit:
Also if needed i can make a video where i show when i start the application when the icon is set but not commented.
getResource uses the relative path with respect to the package (folder), like in
Vision.class.getResource("fullscreen16x.png")
or absolute like in:
Vision.class.getResource("/xray/fullscreen16x.png")

Jpanel not showing up in safari

I have an applet I have written that has a JLabel (containing an ImageIcon) and a custom ImagePanel inside a JPanel. For some reason the JLabel NEVER shows up in safari and firefox on mac os on first run/load but on other OSes (windows,linux) it appears fine. Now in the same applet there's a button that flips the image to another image. On safari/firefox on mac os, when the button is clicked, the second image shows, the when clicked again, the first image now appears!! Any idea what could be causing this issue? Even on safari for windows the applet works fine.. i.e. first image loads and appears.
UI code
public void createUI(){
mainpanel = new JPanel();
mainpanel.setMaximumSize(new Dimension(154, 212));
mainpanel.setMinimumSize(new Dimension(154, 212));
mainpanel.setName("mainPanel");
mainpanel.setLayout(new BorderLayout());
lcdpanel = new ImagePanel(bgLcdImage);
lcdpanel.setBounds(22, 22, 110, 28);
bgImage = Toolkit.getDefaultToolkit().createImage(bytes);//BufferedImage
label = new JLabel(new ImageIcon(bgImage));
mainpanel.add(lcdpanel);
mainpanel.add(label);
mainpanel.invalidate();
getContentPane().add(mainpanel);
repaint();
}
Button click code
private void flipImage()
{
label.setIcon(new ImageIcon(backImg));
label.repaint();
lcdpanel.setVisible(false);
lcdpanel.repaint();
mainpanel.repaint();
this.repaint();
}
Any help would be appreciated.Thanks
I even made the jlabel as a imagepanel, set the layout of jpanel to null
Setting the layout to null is the worst thing you can do. That will generally cause more problems than solve a problem.
The issue is not where i want the jlabel to be shown but WHY its not showing on Mac OS X firefox/safari browsers when it shows on windows/linux firefox/safari browsers.
How do we know when only a few lines of code are posted? Post a proper SSCCE when you have a problem.
On safari/firefox on mac os, when the button is clicked, the second image shows, the when clicked again, the first image now appears!!
The general format when adding/removing components on a visible GUI is to do:
panel.add(...);
panel.revalidate();
panel.repaint(); // sometimes needed
You never need to invoke repaint when you change the property of a component. Swing is smart enough to do the repaint for you.
label.setIcon(new ImageIcon(backImg));
//label.repaint();

How to Set the Background Color of a JButton on the Mac OS

Normally with Java Swing you can set the background color of a button with:
myJButton.setBackground(Color.RED);
which would cause the button to be red. But on the Mac OS, this method seems to be ignored. The button just stays the default color.
How can the color of a JButton be set on the Mac OS?
Have you tried setting JButton.setOpaque(true)?
JButton button = new JButton("test");
button.setBackground(Color.RED);
button.setOpaque(true);
Have you tried setting the painted border false?
JButton button = new JButton();
button.setBackground(Color.red);
button.setOpaque(true);
button.setBorderPainted(false);
It works on my mac :)
If you are not required to use Apple's look and feel, a simple fix is to put the following code in your application or applet, before you add any GUI components to your JFrame or JApplet:
try {
UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );
} catch (Exception e) {
e.printStackTrace();
}
That will set the look and feel to the cross-platform look and feel, and the setBackground() method will then work to change a JButton's background color.
I own a mac too! here is the code that will work:
myButton.setBackground(Color.RED);
myButton.setOpaque(true); //Sets Button Opaque so it works
before doing anything or adding any components set the look and feel so it looks better:
try{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}catch(Exception e){
e.printStackTrace();
}
That is Supposed to change the look and feel to the cross platform look and feel, hope i helped! :)
Based on your own purposes, you can do that based on setOpaque(true/false) and setBorderPainted(true/false); try and combine them to fit your purpose

Categories