I want to add the possibility for my users to add a comment on a form. To display them, I created JPanel inside a simple JScrollPane. I set the layout of this JPanel to BoxLayout because I wish to add them all in only one column and it seemed to be the easiest way by calling BoxLayout.Y_AXIS in the constructor. I also tried GridLayout and GridBagLayout but it was not what I was looking for.
My problem is that when a JPanel has the BoxLayout layout, it's width automatically is the same as it's container, but my container is a JScrollPane and the caret hides the right side of my comment!
You can see the JTextField and a JButton on the bottom left, here's the code on the click event :
private void btnAjoutCommentaireActionPerformed(java.awt.event.ActionEvent evt) {
//I take the text from the JTextField and format it to html
String formattedComment = "<html><br><div style='width:280px;'>" +
txtArCommentaire.getText().replaceAll("\n", "<br>") +
"</div><br></html>";
JLabel label = new JLabel(formattedComment);
//I add a blue border
label.setBorder(new TitledBorder(new EtchedBorder(Color.lightGray, Color.blue), ConfigUser.getCu().toString()));
//this below doesn't work
label.setSize(280, 200);
//I tried adding a JPanel in between but it didn't really worked out
//JPanel panel = new JPanel();
//panel.setLayout(new GridLayout(1, 1));
//panel.setSize(297, 200);
//panel.add(label);
///pnlCommentaire is the JPanel inside the JScrollPane
pnlCommentaire.setLayout(new BoxLayout(pnlCommentaire, BoxLayout.Y_AXIS));
pnlCommentaire.add(label);
pnlCommentaire.revalidate();
pnlCommentaire.repaint();
}
As you can see I tried to adust the size in html using style='width:280px'and on the JLabel using label.setSize(280, 200); but none of them worked.
Do you have any idea on how I could resize this Jlabel?
EDIT :
I added a margin-right property to the div so that I can at least fully see the text in the JLabel but the right border is still hidden.
String formattedComment = "<html><br><div style='width:280px;margin-right:50px;'>" +
txtArCommentaire.getText().replaceAll("\n", "<br>") +
"</div><br></html>";
Related
This seems like a simple thing to do, but I can't get it to work.
I have a BorderLayout. I want to use the top part for a title bar. I want to add a JPanel with labels, buttons and other components. However, the PAGE_START part of the border layout won't left align the panel. Here's the situation, with comments in where I've tried to set the alignment.
I noticed that when I don't add a panel to the border layout, and just write the JLabel straight in, it has left alignment by default.
This is not what I want, though, because I am planning on putting a BoxLayout.X_AXIS horizontally through the BorderLayout.PAGE_START title area. Seems to be a reasonable thing to do?
The Container pane argument to the static method is just the single panel on the main JFrame.
public static void addComponentsToPane(Container pane)
{
JLabel jlabel = new JLabel("I want to left align this inside a JPanel");
// Doesn't work: jlabel.setAlignmentX(Component.LEFT_ALIGNMENT);
JPanel jpanel = new JPanel();
//Doesn't work: jlabel.setAlignmentX(Component.LEFT_ALIGNMENT);
jpanel.add(jlabel);
pane.add(jpanel, BorderLayout.PAGE_START);
// Other parts of the BoxLayout (works fine)
JButton button = new JButton("Button 2 (CENTER)");
button.setPreferredSize(new Dimension(200, 100));
pane.add(button, BorderLayout.CENTER);
button = new JButton("Button 3 (LINE_START)");
pane.add(button, BorderLayout.LINE_START);
button = new JButton("Long-Named Button 4 (PAGE_END)");
pane.add(button, BorderLayout.PAGE_END);
button = new JButton("5 (LINE_END)");
pane.add(button, BorderLayout.LINE_END);
}
Even when I tell the panel to left align the label, it doesn't appear left aligned.
Does anyone know what I am doing wrong?
By default a JPanel uses a FlowLayout with "center" alignment.
if you want components "left" aligned, then you need to set the layout on the panel to use a FlowLayout with "left" alignment.
Read the FlowLayout API for the proper constructor to use to set the alignment.
Or you can also read the Swing tutorial on How to Use FlowLayut which gives the constructors and valid values to specify the alignment.
I'm developing Burp extension and add additional tab. I have to return java.awt.component, so i decided javax.swing.JPanel would be nice. It must be a JLabel and JTextField on my Tab, code here:
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel,Y_AXIS));
JLabel label = new JLabel("hostname : ");
panel.add(label);
JTextField tf = new JTextField("text");
panel.add(tfHost);
I wanted little text and textfield on top left, but my TextField stretched on all my screen. What do i have to do to fix it? Maybe i have to change layout manager?
The problem is a BoxLayout will allow components to grow to fill the available space to the panel.
So the easiest solution is to add your panel to another panel that will respect the size of the BoxLayout panel.
Something like:
JPanel wrapper = new JPanel(); // uses FlowLayout by default.
wrapper.add( panel );
frame.add( wrapper );
Now when you add the wrapper panel to the frame, the wrapper panel will grow in size, but it will not affect the components added to the wrapper panel.
I am trying to make a panel to have 4 items on top. these are a JLabel, JTextField, JLabel and JTextField.
In the center I need a JTextArea and to the left of it a JList that is scrollable.
On the bottom I need 3 buttons.
What would be the best layout manager for this and how should I go about it?
Would having just 3 columns be a good idea?
Heres what I have so far:
JPanel panel = new JPanel();
JTextField IDLabel = new JLabel("ID: ");
IDLabel.setBounds(10, 10, 80, 25);
panel.add(IDLabel);
JTextArea IDText = new JTextField(5);
IDText.setBounds(100, 10, 160, 25);
panel.add(IDText);
JLabel TitleLabel = new JLabel("Title: ");
TitleLabel.setBounds(10, 10, 80, 25);
panel.add(TitleLabel);
JTextField TitleText = new JTextField(10);
TitleText.setBounds(100, 10, 160, 25);
panel.add(TitleText);
JList list = new JList(new String[]{"test1", "test22"});
list.setFixedCellWidth(150);
list.setFixedCellHeight(50);
list.setFont(new Font("Serif",Font.BOLD,16));
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
panel.add(list);
JTextArea BodyArea = new JTextArea();
BodyArea.setSize(200, 200);
BodyArea.setText("Test area");
panel.add(BodyArea);
You will mostly likely need to use a combination of layouts (AKA compound layouts), for example
North Panel
Create a JPanel and assign it a FlowLayout or GridBagLayout or GridLayout depending on what you want to achieve.
Add the JLabel, JTextField, JLabel, JTextField to it.
Center Panel
Create a JPanel with BorderLayout. Add the JTextArea to the CENTER position and the JList to the WEST position
South Panel
Craete a JPanel with a FlowLayout or GridBagLayout or GridLayout depending on what you want to achieve.
Add the buttons to it.
Putting it together
Create a JPanel with a BorderLayout, add the "north" panel to the NORTH position, the "center" panel to the CENTER position and the "south" panel to the SOUTH position
You could use a single container and a GridBagLayout, but that's a lot of work.
Take a look at Laying Out Components Within a Container for more details
Maybe start with a BorderLayout for the main layout. Then you can add components to the PAGE_START (NORTH), LINE_START (WEST) and CENTER and PAGE_END (SOUTH). Check out the section from the Swing tutorial on Using Layout Manager, for more information and examples.
Of course you would also use panels when you want to display multiple components in a single area. So your buttons would first be added to panels.
Also, follow standard naming conventions. Variable names should NOT start with an upper case character( ie. BodyArea, TitleText). You got variable like (panel, list) correct so be consistent.
I am currently trying to create a script editor. But the lineNumber JPanel is not top aligned next to the JTextArea. The lineNumber JPanel appears at the center on the right side of the JTextArea.
It looks like this:
This is the class which instantiates both of these components:
private ScriptEditor() {
((FlowLayout) this.getLayout()).setVgap(0);
((FlowLayout) this.getLayout()).setHgap(0);
//This is the lineNumber JPanel which has no LayoutManager set.
lineNumPanel = new LineNumberPanel();
//I tried setAlignmentY but it did not work
lineNumPanel.setAlignmentY(TOP_ALIGNMENT);
//The text area.
scriptArea = new JTextArea(22,15);
scriptArea.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 15));
scriptArea.setMargin(new Insets(3, 10, 3, 10));
//This JPanel contains the two components: lineNumber JPanel and the JTextArea
JPanel temp = new JPanel();
temp.add(lineNumPanel);
temp.add(scriptArea);
//Set the scrollPane
JScrollPane scrollPane = new JScrollPane(temp);
scrollPane.setPreferredSize(new Dimension(width, height));
//Add the scrollPane to this JPanel.
add(scrollPane);
}
JPanel temp = new JPanel();
By default a JPanel uses a FlowLayout. a FlowLayout vertically centers the components added to the panel. If you don't like this behaviour then try a different layout manager like a horizontal BoxLayout, which will allow you to align the component at the top/center/bottom depending on the components vertical alignment.
However, using a JPanel is not the best approach. Instead you should be adding the line number component to the row header of the scroll pane. See Text Component Line Number for an example of this approach.
I have a JFrame with JScrollPane in it. I have JPanel inside a scrollPane. And add multiline labels in it.
Everything is ok with multiline labels. I enclose my text in <HTML>..</HTML> tags.
And labels display its wrapped text.
"..." means long multiline text.
The problem is that useless area is displayed in the bottom.
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(300, 300));
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
label1.setText("<html>" + "..." + "</html>");
panel.add(label1);
label2.setText("<html>" + "..." + "</html>");
panel.add(label2);
JScrollPane scroll = new JScrollPane(panel);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
frame.setContentPane(scroll);
frame.pack();
frame.setVisible(true);
EDIT.
So I have to set preferred size for inner JPanel. After that scrollPane draws its content(shows scrollbars) as its content has this fixed "inner panel preffered size".
If I won't set preferred size for the panel, JLabels wouldn't wrap the text.
After being layed out by the layout manager inner panel's size grows and became larger than previously set preferred size. Panel grows itself, its ok, I see wrapped text of labels in it. But scrollpane behaves incorrectly. It paints scroll as inner panel is still of prefferred size size. So I need correct resizing behaviour for JScrollPane.
use JTextPane or JEditorPane instead of JPanel contains bunch of JLabels
JTextPane or JEditorPane supporting stylled text or Html <= 3.2 for Java6
theoretically you can use JList, instead of Jlabels, but in this case you have to call for setPreferredScrollableViewportSize(new Dimension) same as for JPanel in the JScrollPane
EDIT
then use Highlighter
use built-in reader/writer for JTextComponents