JPanel Containts inside JFrame - swing

I am Having a JTextField in JFrame having mouselistener on it,and i want to display JPanel with some contents when mouse event is fired.JPanel is displaying but the contents in it are not visible.

you cannot write text directly inside JPanel. Use a JLabel and put the text inside that JLabel and then place the JLabel on the JPanel

You have to call revalidate() after adding new panel, which will instruct the layout manager to recalculate the layout and panel.repaint(); if necessary.
If this didn't solve your problem, post an SSCCE

Related

Manage ImageIcon in jPanel

I have a problem when loading a image using jLabel to Imageicon into a Jpanel. When The image have big size (width and height), it makes a Jpanel in my Splitpane so big too in display. How to make this image have default size?
And, how to implement mouse or mouse wheel when big size image is loaded. So ,It makes the image can be dragged or pull up and down... ?
My code like this...
public FileImage() {
try {
image = ImageIO.read(new File("/media/Data/workspace/final_tugas_akhir/tmp/hasil/akhirnya.png"));
} catch (IOException ex) {
System.out.println("Failed to Load Image...!!! ");
}
JLabel jLabel = new JLabel(new ImageIcon(image));
jLabel.setPreferredSize(new Dimension(10, 10));
jPanel3.add(jLabel); // jpanel3 is my panel in main class
}
and in main class just use this
pnlProsesAlgoritma.add(new FileImage()); // pnlProsesAlgoritma is panel to "stick" jpanel3
this is my screenchot before image not yet loaded :https://www.dropbox.com/s/euslx1jj3n21x7j/before.png
after the image loaded looked like this https://www.dropbox.com/s/0mghkg0bs5cmo9d/after.png
P.S : I am using splitPanel to separate two panel. When the image is loaded, the other panel is shifted...
Thanks 4 the help
Maybe you can use the Stretch Icon. It will automatically resize itself to fill the space available.
Insert your JPanel inside a JScrollPane.
This way, your sizing problem will be solved, and you can navigate large image easily too.

Make a child panel top of the parent panel and all other siblings

I am having one parent JPanel and on this i have added these components.
First JTable
Seconde JTable
First Child JPanel
Steps I am doing now.
I am dragging the data from one JTableto another JTable.
Creating one runtime JPanel that will show the dragged data.
Adding a JLabel value in runntime created JPanel
Reference link DNDCode
I am using GridBayLayout for the Parent Panel.
Problem -
It is hiding my runntime created JPanel under the JTable i.e runntime created JPanel is not coming top of the added First JTable.

Text of JLabel moves out of JPanel

I have a JPanel with specific size. I have added a JLabel on it. Now the text of JLabel is dynamic and comes from the database. When I add this data to JLabel, instead of it staying in the JPanel, it spreads out.
When it happens, the data inside the JPanel is visible but the one outside the JPanel is not visible (which is obvious). I want to know if there is a way by which the data instead of going out of JPanel, will stay in JPanel itself (means it automatically adds a newline and moves to newline instead of going out).
JLabel doesn't have a method for that. But you have a few options:
Use HTML tags in text you set "Hello<br/>World".
Use JTextArea or JEditorPane and disable editing. JTextArea have the setLineWrap(true) method, and JEditorPane wraps lines by default.
You'll want to use something more robust than a simple JLabel, like a JTextArea. Your best place to learn how to achieve the desired effect would be to read the Swing tutorial here. Since you know the size of your JPanel you can set the size of your JTextArea and it should automatically line-wrap to accommodate for your variable-sized strings.

Scrollable html JLabel

I have a Jlabel packed with html. I would like to scroll this content as you would with overflow : auto; in css. I can't seem to get this to work. Has anyone come across this? I'd like to keep the content in HTML - for mark up - and use something light to scroll though it.
BTW: The Jlabel is in a popup - I can use something else other than a JLabel if needs be but would like to keep the html.
Cheers,
slotishtype
Put that JLabel in a JScrollPane instanciated that way :
JScrollPane scroller = new JScrollPane(myJLabel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
Notice that, as this constructor states, those parameters can be changed afterwards.

swing: appropriate layout manager for simple situation?

I have a JPanel that I want to use to contain 3 vertical components:
a JLabel
a JTextField
a JScrollPane
I want all 3 components to fill the JPanel's width. I want the JLabel and JTextField to use their normal heights and the JScrollPane to use the rest.
BoxLayout almost works, except it seems like the JTextField and JScrollPane share the "extra" space when the JPanel is made large.
What can I do?
Create a BorderLayout. Put the JScrollPane in its center.
Create a JPanel with a BoxLayout. Put the JLabel and JTextField in that, vertically. Put that JPanel into the NORTH side of the BorderLayout.
GridBagLayout is pretty handy. You can control anything you need and you can control only what you need. You're probably going to be interested in only the vertical parameters.
You could also use DesignGridLayout as follows:
DesignGridLayout layout = new DesignGridLayout(thePanel);
layout.row().center().fill().add(theLabel);
layout.row().center().fill().add(theTextField);
layout.row().center().fill().add(theScrollPane);
This should exactly behave as you describe.
Each call to row() creates a new row in the panel.
The calls to fill() make sure that each component uses the whole available width.
A few advantages of using DesignGridLayout here are:
only one LayoutManager for the whole
panel
automatic borders and inter-rows spacing