I am trying to create a undecorated JFrame, but I am having some issues with my Closing button, It has this ugly "selected" border around it, is there any way of removing it? (Top right corner of image)
This is what I did to remove all borders and backgrounds:
JButton btnX = new JButton("");
btnX.setIcon(new ImageIcon(GameHubMain.class.getResource("/Resources/Close-icon.png")));
btnX.setForeground(Color.WHITE);
btnX.setOpaque(false);
btnX.setContentAreaFilled(false);
btnX.setBorderPainted(false);
btnX.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.dispose();
}
});
Maybe try this ?
Border emptyBorder = BorderFactory.createEmptyBorder();
btnX.setBorder(emptyBorder);
You should also try adding:
btnX.setFocusPainted(false);
btnX.setMargin(new Insets(0, 0, 0, 0));
Add following line in your code and check
btnX.setBorder(BorderFactory.createEmptyBorder());
Related
I would have a problem with the edges of my jbutton. In practice, in the code below I inserted a button that should not have edges but instead appear as in the photo below.
JButton btnRes = new JButton();
btnRes.setBorderPainted(false);
btnRes.setContentAreaFilled(false);
btnRes.setOpaque(false);
btnRes.setBorder(null);
btnRes.setIcon(new ImageIcon(Main.class.getResource(image1)));
btnRes.setPressedIcon(new ImageIcon(Main.class.getResource(image2)));
btnRes.setRolloverIcon(new ImageIcon(Main.class.getResource(image3)));
btnRes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//TODO
}
});
btnRes.setBounds(496, 342, 138, 48);
frame.getContentPane().add(btnRes);
and this is the result:
Image of this JButton
The border is however visible, how I can fix this?
You see the border that is added to the button because it is selected. Try:
btnRes.setFocusPainted(false);
After reviewing many previous StackOverflow posts, I am still unable to get my JButton to be black instead of the default color. Here is what my button looks like:
And here's my code:
public void setStartButton() {
JPanel panel = this.jPanel1;
panel.setLayout(null);
JButton button = new JButton("START");
// size and location of start button
int res = java.awt.Toolkit.getDefaultToolkit().getScreenResolution();
int length = (int) Math.floor(10.0*res/25.4); // side lengths of square button
int offset = length/2; // because button should be centered...but its x and y location are given by its upper left hand corner
button.setBounds(centerX-offset, centerY-offset, length, length);
// color of start button
button.setBackground(BLACK);
button.setOpaque(true);
button.setContentAreaFilled(false);
// font
button.setFont(new Font("Arial", Font.PLAIN, 8));
button.setVisible(true);
panel.add(button);
}
By the way, when I change setContentAreaFilled to true, it makes no difference.
I know that the function is indeed being called, because the location and font info for my button is working just fine.
Any assistance would be much appreciated! Thanks!
A JButton is made of a series of layers, including the content, border and focus layers. Depending on what you're trying to do, you may need to remove all of them, for example...
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
setStartButton();
}
public void setStartButton() {
JButton button = new JButton("START");
button.setMargin(new Insets(20, 20, 20, 20));
// color of start button
button.setOpaque(true);
button.setContentAreaFilled(true);
button.setBorderPainted(false);
button.setFocusPainted(false);
button.setBackground(BLACK);
button.setForeground(WHITE);
// font
button.setFont(new Font("Arial", Font.PLAIN, 8));
add(button);
}
}
I'd also strongly encourage you to consider making use of an appropriate layout manager and using the properties of it and the JButton to generate the desired padding you need, these will work together with the font metrics, which tend to be different between systems, to generate an appropriate size for the button
I have simple JPanel that serves as tabComponent of JTabbedPane. It is MainTabComponent class:
public class MainTabComponent extends JPanel {
private JLabel label;
public MainTabComponent(String title, MainTabbedPane mainTabbedPane) {
// unset default FlowLayout gaps
super(new FlowLayout(FlowLayout.LEFT, 0, 0));
setOpaque(false);
label = new JLabel(title, Images.tabIconSaved, JLabel.LEADING);
add(label);
// add more space between the label and the button
label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
// tab button
JButton button = new TabButton();
add(button);
// add more space to the top of the component
setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
}
}
Next I need to allow user to drag tabs(that is MainTabComponent).
The problem is that method tabForCoordinate of TabbedPaneUI doesn't return correct tab index.
It returns always 0.
So I need to find out tab index on mouse click, but how to do this with custom tab?
Thank you!
I am having trouble styling a JScrollPane. I just want to be able to change the color of both the thumb and the background (and also remove the increase/decrease buttons). So far I have tried the following:
this.setBackground(new Color(0,0,0));
this.viewport.setBackground(new Color(0,0,0));
this.getViewport().setBackground(Color.BLACK);
this.setOpaque(false);
this.getVerticalScrollBar().setUI(new BasicScrollBarUI()
{
#Override
protected JButton createDecreaseButton(int orientation) {
return createZeroButton();
}
#Override
protected JButton createIncreaseButton(int orientation) {
return createZeroButton();
}
#Override
protected void configureScrollBarColors(){
}
});
this.getHorizontalScrollBar().setUI(new BasicScrollBarUI()
{
#Override
protected JButton createDecreaseButton(int orientation) {
return createZeroButton();
}
#Override
protected JButton createIncreaseButton(int orientation) {
return createZeroButton();
}
});
}
private JButton createZeroButton() {
JButton jbutton = new JButton();
jbutton.setPreferredSize(new Dimension(0, 0));
jbutton.setMinimumSize(new Dimension(0, 0));
jbutton.setMaximumSize(new Dimension(0, 0));
return jbutton;
}
Also
UIManager.put("ScrollBar.trackHighlightForeground", (new Color(57,57,57)));
UIManager.put("scrollbar", (new Color(57,57,57)));
UIManager.put("ScrollBar.thumb", new ColorUIResource(new Color(57,57,57)));
UIManager.put("ScrollBar.thumbHeight", 2);
UIManager.put("ScrollBar.background", (new Color(57,57,57)));
UIManager.put("ScrollBar.thumbDarkShadow", new ColorUIResource(new Color(57,57,57)));
UIManager.put("ScrollBar.thumbShadow", new ColorUIResource(new Color(57,57,57)));
UIManager.put("ScrollBar.thumbHighlight", new ColorUIResource(new Color(57,57,57)));
UIManager.put("ScrollBar.trackForeground", new ColorUIResource(new Color(57,57,57)));
UIManager.put("ScrollBar.trackHighlight", new ColorUIResource(new Color(57,57,57)));
UIManager.put("ScrollBar.foreground", new ColorUIResource(new Color(57,57,57)));
UIManager.put("ScrollBar.shadow", new ColorUIResource(new Color(57,57,57)));
UIManager.put("ScrollBar.highlight", new ColorUIResource(new Color(57,57,57)));
With all of the above code I get a darkened thumb with a white background. Funnily enough if I remove the setUI functions, I get a default thumb with a darkened background..
Any ideas?
Thanks
SOLVED******
the configureScrollBarColors function above can be used in the following way:
#Override
protected void configureScrollBarColors(){
this.thumbColor = Color.GREEN;
}
That changes the color of the thumb to green.
Just in case someone out there is still falling on this thread looking for help:
The following line can be used to change to look of your scrollbar. The fields surrounded by "_" characters can be changed (key and COLOR).
UIManager.put("ScrollBar._key_", new ColorUIResource(_COLOR_));
The most relevant keys for changing the color of your bar are:
thumb (General color of thumb)
thumbDarkShadow (Remaining shadowed part of thumb)
thumbShadow (Essentially a border for the thumb which is split in half with highlight)
thumbHighlight (Second half of said border kind of)
track (background)
So an example would be:
UIManager.put("ScrollBar.thumb", new ColorUIResource(Color.black));
This would make the main part of the thumb black.
If you are going for a full single color thumb then use all keys except track and set them to the same color.
If you are going for an outline set the first two as color1 and the second two as color2.
I fell on this while trying to ameliorate my own GUI and after some minor tweaking thought I'd share my findings.
For more keys click me!
I want to add an image on the screen when an event occurs. In my case, this would be from a button being clicked. I attempted to solve this myself, but the image isn't appearing. I don't know what is wrong.
My code:
JButton button2 = new JButton("+");
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
System.out.println("button #2 working");
label2 = new JLabel(new ImageIcon(this.getClass().getResource("50.png")));
label2.setLocation(10, 60);
label2.setSize(300, 532);
add(label2);
//? Not working
}
});
button2.setSize(50, 40);
button2.setFont(new Font("Arial", 1, 20));
button2.setLocation(110, 592);
button2.setBackground(Color.ORANGE);
content.add(button2);
I actually have done something similar to what you are trying to achieve. However, I achieved this by using a different method instead. You could for example create a class MyPanel which extends JPanel and override the paintComponent() method. In the paintComponent() method you can call g.drawImage(yourImage, 0, 0,null); to add the image to your panel. Then it is a matter of creating an instance of MyPanel and adding it wherever you want on the GUI. Also, do not forget to call repaint() after you have clicked the button or you will not see the change.