resizable Canvas or panel in scrollpane - java

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.

Related

Adding Jlabel on top of canvas

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

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.

How to make a JFrame blurred in java?

In my java program, when the user clicks on a button in the main JFrame another JFrame becomes visible on top of the main frame. Now how to make the previous frame blurred?
Exactly like the background in the image :
Now how to make the previous frame blurred?
have look at JLayer(Java7) based on JXLayer(Java6),
similair effect is possible with GlassPane, or with JViewport (not proper of ways, but good Swing GUI contains JScrollPane as 1st. JComponent in JFrame / JDialog / JWindow)
I'm assuming that you are coding using Java Swing, and the picture is the effect you want.
You would use a JDialog to create the top window, rather than a JFrame. A JDialog is designed to be modal. In other words, lock the JFrame until the JDialog is closed.
The default behavior of a JDialog is to not blur or darken the underlying JFrame. In order to darken the underlying JFrame, you're going to have to paint a black image on the glass pane of the JFrame.
This tutorial shows you how to manipulate the root panes of a JFrame.
There is the setOpacity(float opacity) method that is available in JFrame (inherited from Frame actually).

How to get x and y position of JPane in JPanel

I have a JEditorPane in a JPanel. I want to know how far from the left and the right the JEditorPane is from the edge of the JPanel (or the frame as they are the same).
Is there a way to do this?
The Reason Is: I want to put a gradient on the JEditorPane that aligns to the edge of the JPanel in stead of the JEditorPane. The reason I want to do this is because there is a gradient on the JPanel and I don't want the JEditorPane background to appear to cover up the gradient. I would set the background to transparent but I got screen paint issues that I was unable to resolve.
Just call getLocation on your JEditorPane. The location of a Component is relative to its parent.

Categories

Resources