I have a JFrame and a JPanel on it. JPanel has a label that works as a picture. On that label I created buttons manually. It works fine when I set its z-order to 1. But when I delete that buttons and re-create, it just does not work and goes back of the label. They come front when I hover my mouse on them. Could anyone help me to solve this pls?
panel.add(buttons[indexButtons],3); This is where I add the buttons manually. This one runs for multiple times after deleting older buttons each time when clicked change month button.
if(buttons[i]!=null)
{
//panel.setComponentZOrder(buttons[i], -1);
panel.remove(buttons[i]);
panel.repaint();
} This is how I remove oldest buttons.
label = new JLabel("");
label.setBounds(0, -11, 372, 309);
panel.add(label); This is the background picture where I place buttons to
. JPanel has a label that works as a picture
So the label is now the background component (not the panel).
But when I delete that buttons and re-create, it just does not work and goes back of the label
The buttons should be added to the JLabel NOT the JPanel so you have a component hierarchy like:
- JPanel
- JLabel (with background image)
- JButtons added to the label
Related
Trying desperately to put a label over another label in netbeans because the one label is acting as the background image and I want the label in the foreground to have a different image.
Every time i drag another label on top, the JFrame gets bigger and the label tries to slot down the side.
Here is what I've got (I want the label where the red circle in the middle is not on the right down the side):
:
If you want to put one component on another, then you can use Layered Panes.
A layered panel will let you specify that some child components should be layered above other child components.
Update:
An example:
JLayeredPane pane = new JLayeredPane();
JLabel lbOne= new JLabel("Label one");
JLabel lbTwo = new JLabel("Label two");
pane.add(lbOne, 0);
pane.add(lbTwo , 1);
You just need to set the JFrame layout to null..
If you use panel below set the panel layout also null hope its work for you .
Is there any way to color a JButton? I want to change it's color after it's pressed to show that it has been pressed. Is there a way that I can do this?
Also, in a JFrame, is there a way to make it so that the whole contents of the window will stretch to the fill the frame when the window is maximized?
Thanks, I appreciate it.
Here's a link for changing JButton color on every state.
How to change a JButton color on mouse pressed?
For the JFrame, you can set the Layout of the JFrame to BorderLayout and add the JPanel in the center of the JFrame, so it will fill all the JFrame even when resized.
Example:
frame.setLayout(new BorderLayout());
frame.add(panel,BorderLayout.CENTER);
So I am making a game, and I want to know if it is possible so when lets say "You created a fire" it deletes that line and then displays "Your fire turns into ashes".
two more,
I want to make a jframe background, and let's say I "login" the background disappears, and a new background comes in(but the game, not a background).
I want to add a image icon( already added) (IMAGE = FIRE) it deletes that image and a new one appears( IMAGE = ASHES), how can I do this?
public class FireLabel extends JPanel {
public LabelDemo() {
super(new GridLayout(3,1)); //3 rows, 1 column
JLabel label1;
//Create the first label.
label1 = new JLabel("You created a Fire", JLabel.CENTER);
//Add the labels.
add(label1);
add(label2);
add(label3);
}
The context is a little light, however.
For swicthing from one view to another, I would suggest using a CardLayout, which would allow you to change from the login screen to the game screen.
If you're using JLabel as you primary output...simple change the text or icon using setText or setIcon as required...
To change that, use JLabel.setText. You will then have to call validate for the change to take effect.
I recommend swapping out the content pane for this. Put the login screen in a JPanel and set that as the content pane, then when needed, change the content pane to a second JPanel for the game.
Use the same technique as #1. Use a JLabel to display the image.
I have a JLabel (say label1) on my JFrame at certain coordinates, then I have another JLabel (say label2) that is placed exactly on top of label1 (i.e. it has exactly the same origin and size as label1).
I have registered both these labels to receive mouseClicked event.
label1 = new JLabel();
label1.setHorizontalAlignment(SwingConstants.CENTER);
label1.setName("label1");
label1.addMouseListener(this);
contentPane.add(label1);
label1.setBounds(0, 0, 40, 40);
label2 = new JLabel();
label2.setHorizontalAlignment(SwingConstants.CENTER);
label2.setName("label2");
label2.addMouseListener(this);
contentPane.add(label2);
label2.setBounds(0, 0, 40, 40);
But when I click on the label, only label1 gets notified (which was added before label2).
I am looking for a solution so that both my label objects get notified.
Any help would be highly appreciated.
But when I click on the label, only label1 gets notified (which was added before label2).
Swing paints components in the reverse order that they were added to the panel. So label 2 gets painted before label 1. This explains why label 1 gets the event.
I am looking for a solution so that both my label objects get notified.
Add the MouseListner to the panel. Then you will need to loop through all the components on the panel to see if the component contains the mouse point. When you find a match you do your processing.
There is probably a better solution. Why do you need this? Why are you using a null layout.
I have a JPanel of null layout called MainPanel. Onclick of a button I am adding JTextpane on mainPanel. On first click I am creating a textpane of background color white. On second click I am creating another textpane of color blue. What I want is to place the blue textpane upon the white textpane, but the blue textpane is going behind the white textpane. How can I place it on white pane?
Code is very simple here. Onclick I am creating a new JTextpane, setting dimensions to it and placing it on the mainPanel.
Placing a sample screenshot which describes the issue better. Here the blue textpane has gone behind white textpane. I want it above white texpane. How do I do that?
If you want to stick with your current Components, (I think you should use kellax's solution, but I don't know if there's an extra requirement that's forcing you to use your current approach) you can look into Container.setComponentZOrder(Component comp, int index) to directly determine the order in which Components are displayed.
You will have to replace your JPanel "MainPanel" with a LayeredPanel.
Then you can say:
JLayeredPane mainPanel = new JLayeredPane();
JTextPane whitePane = new JTextPane("White text pane on top");
JTextPane bluePane = new JTextPane("Blue text pane behind");
mainPanel.add(whitePane, 2, 0);
mainPanel.add(bluePane, 1, 0);
Edit:
You can read more about the LayeredPane here: LayeredPane