JLabel does not appear over JButton? [duplicate] - java

This question already has an answer here:
How do I get a JLabel to show over a JButton?
(1 answer)
Closed 8 years ago.
I have a grid of 16 buttons which will represent dice. When a score is added, I want a JLabel to pop up in the middle and display the added score. The 16 buttons and one label are all within one JLabel. Why do the buttons always show on top of the JLabel, even when the JLabel is set to visible?
Thank you for any help!
Pictures
JButtons visible, JLabel is not:
When the JButtons are setVisible to false, the JLabel is seen:
Code
Here is the constructor to my code. The label does not show up if the buttons are visible.
public Grid()
{
super();
setLayout(null);
setBounds(125,205,290,290);
setBackground(new Color(139,69,19));
setBorder(new LineBorder(Color.black,5));
for(int a = 0; a < piece.length; a ++)
{
for(int b = 0; b < piece[0].length; b ++)
{
piece[a][b] = new DiceButton(0,0,a,b,null);
piece[a][b].addActionListener(this);
add(piece[a][b]);
}
}
scoringVisual = new JLabel("+ 200");
scoringVisual.setBounds(110, 135, 70, 30);
scoringVisual.setFont(new Font("Arial Rounded MT Bold", Font.BOLD, 20));
scoringVisual.setOpaque(true);
scoringVisual.setBackground(new Color(0,87,0));
scoringVisual.setForeground(new Color(38,224,2));
scoringVisual.setHorizontalAlignment(JLabel.CENTER);
scoringVisual.setBorder(new LineBorder(Color.black,1));
add(scoringVisual);
}

In your last posting I stated:
Basically the last component added is painted first.
However, I then posted example code (which was the same as your code):
c.add(button);
c.add(label);
Of course I meant to post:
c.add(label);
c.add(button);
Of course this is still a terrible way to do what your want. You should NOT be using a null layout. You should not be using setBounds() to position your components on the panel.
I don't have a background with glasspanes or anything,
That is why I gave you the link to the tutorial. It contains working exmaples.
but I wish to do this in the easiest way possible.
That is why I gave you the link to the tutorial. Sometimes you need to learn the basics to take advantage of what Swing has to offer. Since you don't know how Swing works you don't know that drawbacks of your current approach. We are trying to point you in the right direction at the start of your design phase so you don't have to revisit the design in the future when you encounter problems and it will become harder to change.

Related

When updated, JLabel changes order and comes to the front

I'm trying to make a visual novel using Java Swing, and so far it has been going smoothly. I'm using a JLabel that is attached to a JPanel to make the background image, and it starts good: image of a background behind a textbox + choice buttons, but when I try to update the image using Game.backgroundLabel.setIcon(newBackground);, it brings the background label to the very front: image of a background in front of the textbox, with only one of the three buttons showing. I'm new to the entirety of Java Swing, and mediocre at Java, but I'll try to include only the code that I believe to be relevant.
FROM THE MAIN CLASS (Game.java):
Container con;
JPanel backgroundLabel;
JLabel backgroundLabel;
// Creates background
backgroundPanel = new JPanel();
backgroundPanel.setBounds(0, 0, 800, 600);
backgroundLabel = new JLabel();
backgroundPanel.add(backgroundLabel);
con.add(backgroundPanel);
FROM A DIFFERENT CLASS (Story.java):
ImageIcon inCarBackground = new ImageIcon("C:\\Users\\kiwid\\eclipse-workspace\\FatuiBusiness\\backgrounds\\inCarBackground.png");
ImageIcon scaraSprite = new ImageIcon("C:\\Users\\kiwid\\eclipse-workspace\\FatuiBusiness\\sprites\\ScaraSprite.png");
This first update works, and the JPanel stays in the back where I added it in the Game.java class, as shown in the first image.
public void gameStart() {
position = "inCar00";
Game.nameLabel.setText("");
Game.mainTextArea.setText("My name is Lumine. I’ve recently sided with the Fatui - a huge mafia\norganization - through Scaramouche, who I befriended in Teyvat\nUniversity. I’m on a mission for the Fatui to steal a Blue Diamond\nring that once belonged to the Tsaritsa herself.");
Game.backgroundLabel.setIcon(inCarBackground);
Game.choice1.setText("");
Game.choice2.setText(">");
Game.choice3.setText("");
}
However, this update seems to change the order of the JPanel, and brings it to the front of the screen.
public void inCar01() {
position = "inCar01";
Game.mainTextArea.setText("Here with me are Tartaglia, Scaramouche, and Mona. Or, as I’m\nsupposed to call them here, Childe, Balladeer, and the Prophesizer.");
Game.backgroundLabel.setIcon(scaraSprite);
}
I've searched online for a solid 2 hours, but can't find anything good. I've read a little bit about JLayeredPane, but it seems like that revolves around user input, and when I try to use it, it says that I cannot add a JPanel to it.
Thank you all so much in advance! Please tell me if there's any other code I need to include, or if there's any code that I did not need to include (so I know for the next time I need help here).

JButton showing ellipses

I am trying to set up a GUI in Java using the Swing library. I have created a custom button class that has a custom initializer to make things easier. It is below:
public class JTButton extends JButton {
char type;
public JTButton(String title, ActionListener listener, char type) {
super(title);
this.type = type;
addActionListener(listener);
setForeground(Color.BLACK);
setBackground(Color.WHITE);
setFont(new Font("Monospaced", 1, 10));
setBorderPainted(false);
if (type == 'h') {
setFont(new Font("Monospaced", 1, 24));
}
setOpaque(true);
}
public void setColor(Color t, Color b) {
setBackground(b);
setForeground(t);
}
}
However, when I run the program it is showing elipses in my grid of buttons, like below.
16x16 grid
It should look like this (this is the smaller grid):
9x9 grid
I saw some stuff on GetPreferredSize() but haven't been able to make anything work. Let me know what I can do!
Extra info: Running in macOS, the grid is set up in a GridLayout
When I run my program, it shows ellipses in my grid of buttons.
The ellipses are appearing because the buttons are too small for the text to appear.
This is happening because ultimately you're not using Layout Managers correctly, as HovercraftFullOfEels has mentioned. To explain how to use Layout Managers properly is far too big of an answer (and not what I think you're looking for), so instead I will provide you with some quick ways to get rid of the ellipses.
Here are a few ways to remove the ellipses...
"Quick Fix" Solutions:
1. Modify the JButton Margins.
The margins on JButtons are notoriously small. You can resize them to provide extra room for your text to appear.
You can do this in your JTButton constructor by calling...
setMargin(new Insets(0, 0, 0, 0));
Don't forget to import java.awt.Insets;
You can see the documentation for this function here.
2. Make each JButton larger.
Since you are using a GridLayout, you can make each button larger by making your "Grid container" larger. The buttons will stretch out to fill the available space evenly.
Depending on what Layout Manager you're using for the parent of the Grid container, there will be different ways to resize it. It's outside the scope of this answer to go into depth on those.

Java - Draw a circle ON JButton and JTextArea

So I'm trying to draw a sort of oval on a button and textarea. Yes, there is a reason why I can't just use a Panel or Frame.
So the button and textarea already has text on it and needs to stay as a button and textarea for the reason of the program.
So far, I only got one button to have this circle, but none of the other buttons show up with the circles. And the textareas aren't showing any circles at all.
My Code so Far for the buttons:
for (int i = 0; i < (label.length) / 2; i++) {
butt = new JButton(label[i]); // label contains the text on button
butt.setPreferredSize(new Dimension(83, 100));
butt.add(smallPit); //smallpit is the circle graphics
game.add(butt); // game is a panel
// butt.setLayout(null);
}
for (int i = 6; i < label.length; i++) {
butt = new JButton(label[i]);
butt.setPreferredSize(new Dimension(83, 100));
butt.add(smallPit);
game.add(butt);
}
Code for textarea
score2.setBounds(0, 50, 100, 210);
score1.setBounds(700, 50, 100, 210);
score2.add(scorePit);
score1.add(scorePit);
The outcome so far:
My guess is that this butt.add(smallPit); is causing your issues.
My guess is that smallPit extends from something like JComponent (something that extends from JComponent), the problem is a component can only reside within a single container, so each time you call butt.add(smallPit);, smallPit is removed from it's previous container before been added to the new one.
Instead, create an instance of smallPit (or what ever it is) and add it to each button which needs it.
Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify
You should also have a look at Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?

Java JPanel subclass ignores bounds on adding components

I have a subclass of JPanel that I'm trying to add labels to
for(int i = 0; i < 10; i++)
{
JLabel lblPID = new JLabel("" + i);
lblPID.setBounds(55, i * 50, 15, 15);
this.add(lblPID);
}
But when this runs, the labels line up horizontally next to each other at the same y point, ignoring the bounds I'm setting. How do I have the panel lay them out vertically the way they're supposed to appear?
The likely problem is, the container you are adding your label to is using a layout manager, which is making it's own decisions about how your label should be laid out.
You should avoid using setBounds as you can not guarantee that the label will be rendered the same on different computers, even if they are running the same OS. Instead you should make use of appropriate layout managers, which make these decisions for you.
Take a look at Laying out Components within a Container for more details

Changing Font Size and leaving space between button in a JOptionPane

The text in my JOptionPanes are way to small I was wondering how I could change the font of the text inside the pane. Also, I would like to be set a space between two buttons.
Something like
|Canecl| |DoneSave| |Save|
I guess you mean the fonts on your JButtons inside the JOptionPane are too small.
For that I suggest using Swing Utils by Darryl Burke. Now you can easily do e.g.
for (JButton button : SwingUtils.getDescendantsOfType(JButton.class, pane)) {
button.setFont(new Font("Verdana", Font.BOLD, 32));
}
to set the fonts on all JButtons inside of JOptionPane pane.
If you want to set the font of the JOptionPane message itself, use e.g.
UIManager.put(
"OptionPane.messageFont",
new FontUIResource(new Font("Verdana", Font.BOLD, 32))
);
In regard to the "spacing of buttons" question: I don't know if that can be done without extending & make a customized JOptionPane yourself.
You cannot specify a spacing between just 2 buttons out of 3 (as per the OP's question) but you can increase the size of the spacing between ALL the buttons :
JPanel buttonPanel = (JPanel) myOptionPane.getComponent(1);
BasicOptionPaneUI.ButtonAreaLayout lm2 = (BasicOptionPaneUI.ButtonAreaLayout) buttonPanel.getLayout();
lm2.setPadding(20);
lm2.setSyncAllWidths(false); // buttons will vary in size as needed.
or perhaps something like :
lm2.setPadding(lm2.getPadding() * 2) // double the spacing between ALL buttons.
If there were just 2 buttons, then using these LayoutManager calls you could achieve the desired outcome.
However for varying-sized spaces between the buttons you need to implement your own JDialog where you control the layout of its JPanel yourself.
A JOptionPane is made up of 2 JPanels... one for the message (and icon) itself, and one for the buttons, that's why we getComponent(1) above.
I know the question is 4 years old but I made this answer because I had a similar need today and couldn't find the answer ANYWHERE on Stack overflow or elsewhere.

Categories

Resources