JSeparator with Title - java

How do I create a horizontal JSeparator with a title label in Java Swing?
Something like this:
--- Title XYZ --------------------

I found a solution: SwingX JXTitledSeparator. We already use SwingX in our project. I didn't know that SwingX provides a titled separator.
#Bombe Thank you for your help.

Hmm… without any testing and completely from the top of my head:
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder("Title"));
menu.add(panel);
Most Swing containers eat anything so that might even work.
Another approach would be to create a custom component (maybe with a horizontal BoxLayout) and add a JSeparator, a JLabel, and another JSeparator to it, then add it to the menu.

Related

Setting GroupBoxes in Java

I am kind of newbie em Java and right now I am trying to program a simple GUI. Recently, I was looking for some kind of GroupBox (for WindowBuilder if possible), like the "Search" and "Overview" Groupboxes in the following image: http://imagizer.imageshack.us/a/img812/7239/jxsv.jpg
So, which graphical object would be nice for it?
Take a look at using a TitledBorder that includes a lowered etched (EtchedBorder.LOWERED) border on a separate JPanel container using Swings BorderFactory. Read How to Use Borders
You would simply place the controls onto a JPanel with a TitledBorder.
Further reading: http://docs.oracle.com/javase/tutorial/uiswing/components/border.html

JTextArea in JTextPane - Want new line

I want to add text to a JTextArea, and to have an auto scrollbar on vertical.
but when typing horizonally, I want an auto new line when there is no space in line..
If I use only JTextArea it's OK, but when I put it in a JScrollPane, it is not
make a new line when needed.
How can I do that?
Thanks!
you have to configure the textArea to wrap:
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
You might consider reading a basic tutorial to get you started effectively :-)
By default, a JTextArea will not wrap text so you have to manually define that behavior:
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
Also, make sure you're setting up the JScrollPane correctly:
JScrollPane sp = new JScrollPane(textArea);
//JScrollPanes are just like JPanels (except for the scrollbars) so be careful not to just add the JComponent to your frame; add the container instead.
frame.add(sp);
As a side note, read the tutorial #kleopatra so helpfully suggested to get a good solid base on textareas.
Isn't JTextArea implementing the Scrollable interface? So why you need JScrollPane?
Edit to your comment, this one works for me:
JScrollPane sP= new JScrollPane(txtarea);
sP.setBounds(10,60,780,500);
sP.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

jTextfield display problem in java

I have made a frame in which i have put two jTextfield boxes where the user can see the path of the loaded file. Problem is that if the path is too long , the textfield expands to accomodate the full path which again leads to display problems. I would like to keep the textfield's length constant and instead , display the full path of file as a tooltip instead.
How can this be done?
Code for layout manager of jinternal Frame:
javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
I never use an IDE so I don't know how the GroupLayout works.
But when using the other layout managers I always use:
JTextField textField = new JTextField(10); // or whatever size your want
This will give the text field a preferred size and the layout manager can use that information when laying out the component.
Code the GUI by hand instead. You will avoid problems like this and it will be much easier to make changes to your code.
you need to choose a layout manager to manage the proportions of your JComponents.
Try to put your textfiels on a JPanel so you can select a layout useful for you
Later you can use JTextField. setToolTip("full path") to set a tool tip
I solved my problem:
Anybody having the same problem can set the Property Columns using Netbeans. The default is 0, so the textfield cannot accomodate the full text. Use some value like 3 to achieve it.

Two JPanels overlapping?

This is the code I'm dealing with: http://pastie.org/1501054 When you run this, for some reason, the two panels overlap. Why is this so? Any way I can fix it?
The ActionListener that is provided as an argument is irrelevant to this part of the program.
Also, where can I find a good swing tutorial that uses Eclipse?
I am not seeing overlapping panes when I run your code. I wonder if you can post a screenshot of the effect that you see.
As for tutorials in Eclipse. I would suggest using Windows Builder pro in Eclipse for building Java GUI. This is an excellent product (free) and very good documentation on the site.
While I'm not sure what you want the final result to look like, here's a few of my suggestions.
In the snippet where you add the warning panel to the JFrame
warningPanel.add(warningLabel);
contentPane.add(warningPanel);
pack();
contentPane being the container returned by JFrame.getContentPane()
JFrames by default use the BorderLayout, and so,
contentPane.add(<someComponent>)
is identical to
contentPane.add(<someComponent>, BorderLayout.CENTER)
You also add the mainPanel the same way, and you can't have two components with the same constraints, so instead set the warningPanel to BorderLayout.NORTH
contentPane.add(warningPanel, BorderLayout.NORTH)
And also remove the call to pack() in that code snippet, since you call it later on in your code.
Hope this helps.
PS
As for GUI building in Eclipse, this previous question on Eclipse GUI Builder plugins maybe of use. I can't speak for tutorials on using Swing in Eclipse, but a quick google search digs up this tutorial using the Eclipse Visual Editor project
When you say you want a good swing tutorial for eclipse, do you want a GUI builder or a tutorial on Swing? Swing is all about layouts. Once you get that down, it's a piece of cake. Just start with flow layouts.
If you want a GUI builder, use Netbeans. It's incredible!
As for the overlap, it's not an overlap, but an overwrite. You can only have one panel in BorderLayout.CENTER, the default location.
If you want them side by side, do getContentPane().setLayout(new FlowLayout());
Or just add(panel, BorderLayout.SOUTH)
Yes I see the warning label is over lapping the first row
That is because You didnt specify the layout for your contentpane.
Heres how you fix it
contentPane.setLayout(new GridLayout(2,1));
add this line under
Container contentPane = getContentPane();
and I think you should change your warning panel to gridlayout 1 and 1 not 0 and 1 because you still have 1 row and 1 column

Panel with line wrapping and line breaking in Java Swing

How to implement a panel that would support line wrapping and line breaking?
I would only add textual labels and line breaks to this panel.
The labels should flow from left to right, wrapping to the next "line" if
needed. The line breaks would cause a jump to the next line.
I would also like to make the panel vertically scrollable.
The solution should work in Java 5. SwingX can be used.
Clarification: The textual labels are actually JXHyperlinks (from SwingX),
i.e. the panel contains clickable labels. This is the reason I cannot just use
JTextArea.
UPDATE: I missed the request for hyperlink support. Don't know how to do that w/o using the EditorPane.
JTextArea does exactly what you've described.
JTextArea textArea = new JTextArea();
JScrollPanel sPane = new JScrollPane(textArea);
alt text http://img187.imageshack.us/img187/3238/wraprn0.png
This sample is not from a panel, that is a container, but from a JLabel, that is intended to show content.
You could use HTML in your content and use a <br> on each break. You should programmatically calculate the breaks according with your rules on component resize.
Here's the code:
import javax.swing.*;
import java.awt.*;
public class Wrap {
public static void main( String [] args ) {
JFrame frame = new JFrame("Wrap test");
String text = "<html>This<br>is<br>a<br>multiline<br>label</html>";
frame.add( new JLabel( text ) );
frame.pack();
frame.setVisible( true );
}
}
I found JTextPane, which I had overlooked before for some reason. This class does what I need.
Thanks for your help though. :)
Although it may not be a solution you're in search of, but from the requirements you have, it seems like a custom LayoutManager may be able to achieve what you are after. By designing and assigning a custom Layout Manager which allows line breaks to a Container (such as Panel), it should be possible to have a Panel which allows line breaks.
The Laying Out Components Within a Container article from The Java Tutorials will provide general information on how Layout Managers work in Java, and in particular, the Creating a Custom Layout Manager will provide information on how to make a custom Layout Manager to apply to an Container.
The behavior of the FlowLayout (the default Layout Manager for Panel) seems fairly close to the behavior you may be after. Adding functionality to line break seems like the missing piece.
Suggestion: Perhaps the custom Layout Manager can have the ability to add a line break by having a Component that represents a line break, which can be added to a Container by using the add() method.
For example, have a class constant Component in the custom Layout Manager, such as (a hypothetical) LineBreakLayout.LINE_BREAK, and adding that to the Container can tell the custom layout manager to move to the next line. Perhaps an implementation can be like:
Panel p = new Panel(new LineBreakLayout());
p.add(new Label("First Line"));
p.add(LineBreakLayout.LINE_BREAK);
p.add(new Label("Second Line"));
The above hypothetical LineBreakLayout will then render the first Label in one line and the second Label in the second line.

Categories

Resources