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
Related
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>";
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 have a JFrame which contains a JSplitPane in a JScrollPane (so the user can scroll if the window is to big). The JSplitPane contains a JTabbedPane as the top component and graphics as the bottom component.
Now i want to read a .csv und display it in my JTabbedPane. I can scroll through the list with a second JScrollPane. Here comes the problem, when i import the .csv in my programm, the first JScrollPane seems not to notice that there is a second JScrollPane for scrolling the list and then my window gets a lot of free space to scroll.
JFrame frame = new JFrame();
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.WRAP_TAB_LAYOUT);
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setTopComponent(tabbedPane);
splitPane.setBottomComponent(graphics());
JScrollPane scrollPane = new JScrollPane(splitPane);
frame.add(scrollPane);
frame.setVisible(true);
When i import the .csv I add a new JPanel to the tabbedPane. The JPanel contains a list from the data from the .csv
JPanel panel = new JPanel();
JScrollPane scrollPane = new JScrollPane(panel);
// here comes the code for the list
tabbedPane.add(scrollPane);
I hope you understand my problem, it is hard to explain.
Edit: Pictures from before and after importing the .csv may help you to understand.
Get rid of the first scroll pane. Add the split pane directly to the CENTER of the BorderLayout used by the frame. As the frame resizes all the space will be allocated to the split pane.
You can then use:
splitPane.setResizeWeight(1.0);
Now all the extra space will go to the second component as the frame is resized. Therefore the scrollbar for that component will appear/disappear as required.
This is the code for my GUI:
public TLGUI(){
final int x=500,y=600;
JFrame frame=new JFrame();
frame.setSize(x, y);
frame.setVisible(true);
frame.setResizable(false);
JLabel labelTL=new JLabel("This is a test label");
JScrollPane pane = new JScrollPane(labelTL,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JPanel panel=new JPanel();
panel.add(labelTL);
frame.add(panel);
frame.add(pane);
}
I have a huge problem right now regarding the line marked with the ** **.
This code does indeed add a scrollbar to my window, but the problem is that if I place it in front of the TLFrame.add(panel), I wont see it at all (I guess the panel is covering it in this order), and as soon as I turn it around, I can see a scrollbar but the whole frame other than the scrollbar is grey (I suppose the scrollbar is covering the panels contents here).
However, I want both of them to be visible at once, of course. Because my Label is bigger than the Frame, I want to be able to scroll down at least.
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.