Adding Jlabel on top of canvas - java

Is there any way that i can add a jlabel on top of the canvas? In my code, the constructor of my frame adds the label first before adding the canvas but when i run it it does not show the label.
I am painting the background of my canvas.

Suggestions:
Don't use Canvas objects. You've got a Swing GUI and should use the Swing equivalent -- a JPanel.
Draw the background image in the JPanel's paintComponent method as the tutorials and hundreds of examples on this site will show you.
Add the JLabel to the JPanel not to the JFrame.
Then add the JPanel to the JFrame.
Layout managers and your understanding of them are critical. Understand that a JPanel uses FlowLayout by default, and if you add a single JLabel to it, it will be placed in the center top region of the JPanel. Requisite Layout Manager Tutorial Link

Related

changing a label on a panel at the bottom of my JFrame puts text at the top

I have a JFrame with two panels. The top panel is for displaying graphics. The bottom panel is for displaying status. The two panels are different colors. When I try to change the text of JLabels on the bottom panel, it creates text at the top of the top panel. How do I change my code so it only shows on the bottom. Here is the code:
public void setManInfo(String manNews) {
manInfo.setText(manNews);
}
manInfo is my JLabel. It is on the panel with a GridBagLayout.
The top panel is for displaying graphics.
it creates text at the top of the top panel.
Probably because your custom painting is incorrect.
Custom painting is done by overriding the paintComponent(...) method of your panel. And don't forget to invoke super.paintComponent(...) to make sure the backgrounds get painting properly and you don't have painting artifacts.
Read the section from the Swing tutorial on Custom Painting for more information and working examples.

How to create a rectangle with multiple images inside?

What is the best way to display let's say rectangle (3x5) with icons 20x20 px.? I want to change the image file of every pic icon later (= it's not just static pictures). I tried to make JFrame full of JPanels, but i was able to display only one panel at a time. I don't want to use GridLayout, because I need just small rectangle inside a frame. Any ideas how to do it? Couldn't find any tutorial or solution. I'm completely new to GUI developement. Thanks
You do want to use a GridLayout. Your problem is that the JFrame you put the icons into uses a BorderLayout by default (and really, you shouldn't change the layout of a top level component).
What this means is that, if you add multiple panels to the frame, without using one of the NORTH, EAST, SOUTH, WEST constraints, only one of the panels will be visible and take up all the space. If you use a GridLayout for that one panel you get, the icons will be stretched, because the panel receives all the space due to the frame's BorderLayout. An alternate layout that doesn't stretch its contents is FlowLayout, but the layout to use depends heavily on your context.
To display the icons, a JLabel is handy. Use an ImageIcon for the label's icon. You can later use setIcon() on the label to choose a new icon.
overall, my approach would be this:
use a JFrame which has a BorderLayout
to the frame, add a JPanel to the frame. The default layout is a FlowLayout, which will prevent the stretching
to the panel, add a JPanel with an appropriate GridLayout
to that panel, add the JLabels, each having an appropriate ImageIcon

Java Superpose two JPanels

I'm working on Java 7 for a desktop application using mostly swing.
I would like to Superpose two JPanels. Basically, I have a JPanel (1) in which I would like to draw some stuff (with paintComponent()) and on top of it, I'd like to display another JPanel (2) filled with a JScrollPane (3) filled with a Jtable (4).
The Components (2, 3 & 4) would have a transparent background to let see the painted Component on the JPanel 1.
Any idea how to organize/do/implement this ?
Thank you !
I found out the proper way.
My Jpanel (1) is a borderLayout and has a paintComponent(gg) method which draw several things.
I add to that panel a JScrollPane (3) and inside it a Jtable (4).
The idea is that 3 & 4 have a transparent background.
For a JScrollpane and a JPanel:
jp.setOpaque(false)
For a JTable, it is more difficult. The background of the JT has to be opaque and the background of each cell has to be transparent using R,G,B,A. To make it opaque, precess as with a Jpanel. Then add a CellRenderer to the JTable and (for each cell) setBackground(new Color(0,0,0,0));
I then had some issues when I scrolled in the ScrollPane. You will have to add a visibility listener to the JScrollPane. When to JScrollPane visibility change, repaint() the Jpanel (1).
This way it's workings but it's not fluid. Even with a new generation untrabook(2014).
(I only draw an image from a file in the Jpanel 1)
So, I hope there will be some better solutions.
Update : see this : Add background image in JTable

Sliding hidden sections of a JPanel into view

I want to display a JFrame ( made with the Netbeans GUI Editor ) that has an enclosed panel ( the panel encovers the entire JFrame ). The panel is twice as wide as the frame, so I want it so that when a button is pressed inside of the panel, the panel's visible area slides over ( over about 2 seconds) to the hidden area of the JPanel and the previously visible section of the JPanel becomes invisible. I couldn't find any function how to set the currently visible section of a JPanel, so the function and/or a different solution to this would be helpful.
I suggest that you put the JPanel in a JScrollPane, one that if you wish does not show its scrollbars. Then you could easily use the scrollpane's model and a Swing Timer to create an animation that shows the JPanel sliding.
The solutions is CardLayout based http://java-sl.com/tip_slider.html
You can add 2 (or more) panels into container and rotate them.

resizable Canvas or panel in scrollpane

I want to create java applet having Canvas or Panel. Now when i start entering data using drawstring(), height of Canvas or Panel should increase dynamically and scroll bar should be visible.
How can I implement such applet?
Please let me know even this can be achieved by using control other then Canvas or Panel.
Thank you in advance.
Use Swing. Why paint the text when a JTextArea or JEditorPane can paint it easily?
Drop the JTextComponent into a JScrollPane, put that in a JPanel, add the JPanel to a JApplet. Job done.

Categories

Resources