I am writing a code in Java Swing in which a label is added in frame on the click of the button. And the Labels are added such that it overlap each other to create a stack like view.
I am creating my own layout using label.setbouds(x,y,100,100) function.
And each time button is clicked in its action listener a new label is added.
For example
if(e.getSource()==button){
label.setBounds(x+5,y+5,100,100);
frame.add(label);
frame.repaint();
}
Now the problem is when these labels are painted the first label added always remain on the top and newly add Labels are overshadowed by odd label.
I need a help if someone could explain how i can paint the frame such that newly added frames comes on the top and old labels get overlapped.Any suggestion is appreciated
Regards
ACoder
Insert your labels in a JLayeredPane with the proper index. Also make sure to set your JLabel as opaque (setOpaque(true)) so that it will actually obscure the content of the labels below. However, if a label below is bigger than the label on top, you will still see some part of it
Related
Unfortunately, I have seen this question multiple times but unfortunately I do not know what I am doing well enough to be able to interpret what I have to implement in my own code.
I have used the GUI editor in netbeans to create a JScrollPane and a JPanel on top of this. I am aware I can create these components at runtime along with everything else but this proved problematic and simply adding the scroll pane and panel in the editor and adding components to them during runtime has worked for me thus far.
So far, creating components and adding them to the panel is no problem. The problem I face is that the scroll pane will not update itself to enable the user to scroll further down to view the created components at the bottom. I have been generating ‘entries’ each time a button is pressed, that currently creates a JTextField and adds this component to the panel, more components are going to be added later but for now this is just experimentation.
Once the ‘list’ of ‘entries’ exceeds the limit of the window, the window has to be resized in order to view components at the bottom but after a certain number of entries (around 25-30) the components are no longer viewable as the scroll bar does not scroll down the panel.
This is how I am adding components at runtime...
JTextField txtName1 = new JTextField();
txtName1.setLocation(10, 90);
txtName1.setSize(135, 25);
pnlContainer.add(txtName1);
The 'pnlContainer' is attached on top of the 'jspContainer' which is my JScrollPane and I am using the 'repaint()' method to get the components visible on the panel.
So far, adding components at runtime has worked, all of the components are visible and interact-able.
I am fairly new to programming in general with only a few years experience, any documentation that may help, tutorials or anything else is greatly appreciated. Documentation is always welcome as I still have a lot to learn.
JTextField txtName1 = new JTextField();
txtName1.setLocation(10, 90);
txtName1.setSize(135, 25);
pnlContainer.add(txtName1);
So far, adding components at runtime has worked, all of the components are visible and interact-able.
In you above code example you are setting the size/location of each component which implies you are using a null layout. Don't use a null layout!!!
Swing was designed to be used with layout managers and scrolling will work properly when you use panels with a layout manager.
The layout manager is responsible for determining the "preferred size" of the panel. The panel will then display scrollbars automatically when the preferred size of the panel is greater than the size of the scroll panel.
When you use a null layout the preferred size of the panel is 0, so the scrollbars will never appear.
I have positioned a Jlabel on top of a text pane, which acts like a top border to the textpane. My problem is when typing on the textpane, the label get disappeared. Please give me a way to keep the jlabel visible all the time.
Thanks in advance.
---------Edit---------
Giving a code sample would be lengthy. Please refer the images below to get an idea about what happens.
1) before typing in textPane.
2) after typing.
I have made a simple GUI using a GridLayout(5,3) , it is action performed and it implements action listener as well. The are some calculation and algorithms that working according to what inputs or buttons the user provides. Everything works just fine up to this point.
At some point in my code, the user gets a pop up massage that he is correctly logged in to the system using this common method JOptionPane.showMessageDialog(....) . All i want is, after he press the OK button, is to create an additional form that pop ups, and looks similar to the one above i made with GridLayout(5,3) so that my user can store additional info about him.
I really cant get it to work, and i have no idea how to start this.
Any ideas are very welcomed! Cheers and thanks in advance :)
if add this:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
GridLayout grid=new GridLayout(10,1);
pane.setLayout(grid);
it only adds more lines to my gridlayout. And all above buttons and labels remains. How can i get rid of the previous labels and buttons?
You state:
if add this:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
GridLayout grid=new GridLayout(10,1);
pane.setLayout(grid);
it only adds more lines to my gridlayout. And all above buttons and labels remains. How can i get rid of the previous labels and buttons?
You have at least three options if you want to swap "views" on the JFrame.
If you want to use the same GUI with the same JTextComponents but have the components empty of text, then you'll need to go through your text components and call setText("") on all of them. If you want to keep the same JButtons and labels but change their text, then similarly you will need to go through all of them calling setText("something else").
If you want totally new components to replace the old ones, the most straight forward way I believe is to use a CardLayout to hold your JPanel that has all your components. When you want to swap the JPanel for another, make sure that the new JPanel has been added to the CardLayout-using JPanel and then call next() on the CardLayout object.
Another way is to manually swap out JPanels held by the JFrame's contentPane by calling removeAll() on the contentPane, then add(nextJPanel) on it, then revalidate(), then repaint().
I have 3 image so I want, if I drag any image that should appear above all the other images. Which technique can use? please any idea???? By the way I am using "ImageIcon".
Thanks in advance.
If what you want to do is click on an image, lift it above the others and then drag it, consider placing them in ImageIcons and these in JLabels, and displaying them in a JLayeredPane, or a JPanel that is held in a JLayeredPane. You can then lift the clicked label onto the JLayeredPane.DRAG_LAYER while dragging, and then drop it down into the DEFAULT_LAYER (or on a JPanel that's on the DEFAULT_LAYER) when done. For an example of this, please see my code in a related question: dragging a jlabel around the screen
If I'm totally off base, sorry, but please correct my incorrect assumptions.
By the way I am using "ImageIcon".
I assume this means you are adding in image to a JLabel and are then adding the JLabel to a JPanel.
so I want, if I drag any image that should appear above all the other images
You need to add a MouseListener to the JLabel. When you click on the label then you can reset its Z-Order to 0. This will cause the label to be painted last and so it will be on top of all other labels on the panel.
So the basic code in the MouseListener would be:
panel.setComponentZOrder(label, 0);
I have Java application which adds JTextFields # runtime to JPanel. Basically user clicks a button and new JTextField is added, clicks again added again...
Each new JTextField is directly below the previous one. Obviously I run out of space pretty soon so I'm trying to use JScrollPane and thats where the hell begins, because it just doesnt work no matter what I try.
Right click on JPanel and Enclose in Scroll Pane. Didnt work.
After reading some examples I realized I must have JPanel as an argument for JScrollPane constructor. Which I did via right clicking on ScrollPane and CustomizeCode. Because apparently auto-generated code is protected in NetBeans and I cannot just change all those declarations, etc. manually. Still doesnt work.
I did try to set PreferedSize to null for JPanel and/or JScrollPane, didnt help.
JScrollPane is a child of lets call it TabJPanel (which in turn is a tab of TabbedPane). I tried to mess with their relationships, basically trying every possible way of parentship between JFrame, JPanel(holding textfields), TabJPanel and JScrollPane, but nothing worked.
I also made VerticalScrollBar "always visible" just in a case. So I see the scrollbar, it's just that populating that JPanel with JTextFields does not affect it.
When there are too many JTextFields I they go "below" the bottom border of JPanel and I cannot see them anymore.
Code for adding new JTextFields is like this, in a case it's relevant.
JTextField newField = new JTextField( columns );
Rectangle coordinates = previousTextField.getBounds();
newField.setBounds(coordinates.x , coordinates.y + 50, coordinates.width, coordinates.height);
JPanel.add(newField);
JPanel.revalidate();
JPanel.repaint();
Sorry for a long post I'm just trying to provide as much info as possible, because being newbie I dont know whats exactly relevant and whats not. Thanks in advance :)
As there is another answer now, I'm adding my suggestion too.
This sounds exactly like a problem to use a JTable with a single column. JList is not yet editable (and might never be).
JTable would handle the layout problems for you, and you can easily access the values via the table.
Use your own TableModel (a simple Vector should be sufficient in your case), and add values to it.
An option you have is to utilize a LayoutManager, instead of setting the bounds directly on the components. To test this, a simple single column GridLayout with the alignment set to vertical should prove the concept.
panel.setLayout(new GridLayout(0,1));
zero in the rows param allows for rows to be added to the layout as needed.
I do this way to add a scrollpane, create a panel and fill it with few components, then create a scrollpane in the component you want to add it, cut and paste the panel in which all your details will fall in and resize the scrollpane.Because the components take a larger space than the one visible right click on the scrollpane and select design this container, there you can increase the size of the scrollpane and add as many components as you have.