How do I display the width of a JPanel after using getWidth();? - java

How would I display the width of a panel after using getWidth()? Can I put it in a JTextField/JTextArea? I don't think so, but I'm not sure.

It is certainly possible to display the panel size in a text component. You can use setText(String t) method to set the display text of a JTextField or a JTextArea. Try this:
String width = myPanel.getWidth();
myTextField.setText(width);
It will display the width of the panel to the user in a JTextField. (Or you can replace the text field to any text components you like)
You can also take a look at these two sections in the Java Tutorial: How to use Top-Level containers and Using Text Components. The Java Tutorial is a great official document for Swing developers.

Related

chat room whit gif and emoji and image Java

I want to build a chat room whit java swing.
I want to have a component to show text, image, gif and emoji.
please help me to select a good component.
In first I choose JtextPane for this.Is it good?
In first I choose JtextPane for this.Is it good?
Yes, I would use a JTextPane, with normal text (not HTML). With a JTextPane you can:
text with attributes.
align text to the right/left of the text pane
display icons using the insertIcon(...) method.
Read the section from the Swing tutorial on Text Component Features for some examples that use attributes when adding text.

Position image in a JTextPane (like using setBounds to position JPanel in a JFrame)

I'm new to Java and I would like to know how to position an image within a JTextPane. Is it possible? Is there any other methods for positioning an image in a JLabel?
The gridBagLayout can be used to position components within a JFrame.
Take a look here: http://docs.oracle.com/javase/7/docs/api/java/awt/GridBagLayout.html
I usually use gridLayout,it is less flexible, but easy to use for simple layouts.
You can create a JLabel for the image, call setBounds() for the image and call jTextPaneInstance.add(theImageLabel).Or you can specify any desired LayoutManager for the jTextPaneInstance and just add the label with appropriate constraint.
If you mean something like text flow around the image it's much much more difficult to implement but also possible (depends on EditorKit you use there).

Java Swing JLabel Text longer than bounds

I have a problem with Java Swing JLabel.
The text i want to display on the JLabel exceeds the bounds of the JLabel. I want to display it via a Marqueeeffect. I already implemented the effect but when there is a string that exceeds the bounds of the JLabel it gets cut off and the rest gets replaced with "...".
My question is, if there is any opportunity to set the textlength for a JLabel individually, not depending on the bounds, that it doesnt get cut off?
Hope somebody got an answer for me.
I dont use any LayoutManagers and i dont want the JLabel to get resized, it should only can contain text longer than the bounds of it.
I want to display it via a Marqueeeffect.
Check out the Marquee Panel.
In this LayoutTest, you can see how the label's UI delegate uses layoutCompoundLabel() to elide the text when label's size falls below the preferred size.
In this MarqueeTest, MarqueePanel has a default FlowLayout, which adopts the display label's preferred size.
The Swing JLabel was not designed to do marquee scrolling.
Here's the source code for JLabel. You can modify the text handling routines to do a marquee scroll rather than compressing the text with an ellipsis.
Oh, you'd better use a layout manager. Your marquee JLabel won't layout correctly without a layout manager.

Trying to recreate Skype conversation panel with Swing

I am trying to recreate the Skype handles instant messaging using Swing components. I am using JList with a custom ListCellRenderer to render each cell in the list. The ListCellRenderer extends a JPanel, the JPanel simply contains a label (where I will put the username) and a JTextArea which is where the users' messages will go. The JTextArea is what Im having problems with.
Here's an image of what I have a the moment -
Ive removed the scrollpane that automatically comes with the textarea in netbeans.
I am showing the Navigator, the Design view and the actual program (the list has two elements) in this image.
The the text in textarea is actually much longer than in this image but it is not word wrapping. I have set lineWrap and wrapStyleWord to true in the properties box for this textarea but it doesn't seem to take any effect. I then tried to set maximum size using the properties box and that doesn't have any effect either.
Is there any way to control the padding/margins around components with netbeans gui designer. The automatic placement it gives me for spacing between components is either several pixels too small or two large. I need exact placement on the list's cell components.
For reference here is how skype's convesation panel looks (ive added in the red "Brian cs"'s as thats how I will be doing it in my program). As you can see the sentences wrap and there is an appropriate amount of space between cells. So anyone know how to achieve this using Swing?
The JTextarea is not the problem. The JList sets the heigth for each row. See JList.setFixedCellHeigth or setPrototypeCellValue

how to make java run time sizable image box

I'm making a Unicode translator in Java. I did all hard parts, but now I want to add a resizable, relocatable image to the textpane. The user must be able to resize image with its corners and drag & drop the image within the textpane where he likes. (like Microsoft Word or Photoshop)
Something like this:
I tried the Styled Document properties. But I couldn't find way except inserting only an ImageIcon.
May be a better choice would be not to use textPane.
If you have an custom editable label, that can be edited by double clicking on it to show a text box to edit the contents, and change the text of the label when enter key is pressed.
Also give a shot of JDesktoppane, JLayeredPane, and check what components can be added to it.
try this
http://mgeisler.net/downloads/browser/src/ImageBox.java.html
try loading the image inside a JLabel component in JFC Swing.
Otherwise try some other non editable components in the same technology with resizable property.
try this http://sourceforge.net/projects/ird/
the iRD is a component for Resize and move(drag&drop) compoennts on runtime in java.

Categories