Display Form in ScrollPane - java

I created a form. Actually it is 10 JLabels with each JLabel having a text field next to it.
consider,
JLabel_called_Name JTextField_to_obtain_name
JLabel_called_Phone JTextField_to_obtain_phone_number
and so on..
I usually position this in a JPanel and display it in a frame. But my panel and frame have height smaller than the size required to hold 10 of these Labels and Textfields.
So I wish to add them to a JScrollPane.
But in every question I only obtained information of how to add Jlabels to a scroll pane using a Box,
or adding JLabels to a JList.
However I would like to represent it in the format I showed above. A Jlabel beside a JTextField.
How can one acheive this?

But in every question I only obtained information of how to add Jlabels to a scroll pane using a Box, or adding JLabels to a JList.
You can add any component to a JScrollPane:
JPanel = new JPanel();
panel.add( label1 );
panel.add( textField1 );
JScrollPane scrollPane = new JScrollPane( panel );
The trick is choosing the correct layout manager for you panel. Read the Swing tutorial on Layout Managers to help you decide how to design the panel. You can also nest panels to get your desired layout.

Related

JTextArea forces the parent JScrollPane to scroll down

I have a panel inside a JScrollPane and I dynamically populate the panel with components as the data gets received from the service. The panel uses GridBagLayout (but that should be irrelevant). For each record that comes back from the service, I create several components dynamically and append them to the bottom of the panel. Everything works fine, but the problem is that JTextArea that gets created for each record forces the main JScrollPane to scroll down and show the last added JTextArea, as shown here:
I tried to disable everything I could think of to dumb down the JTextArea, but still doesn't help
JTextArea descriptionArea = new JTextArea(project.getDescription().replace("<br>", "\n"));
descriptionArea.setEditable(false);
descriptionArea.setFont(thumbnailLabel.getFont());
descriptionArea.setLineWrap(true);
descriptionArea.setWrapStyleWord(true);
descriptionArea.setFocusable(false);
descriptionArea.setRequestFocusEnabled(false);
DefaultCaret caret = (DefaultCaret) descriptionArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
How can I prevent it from moving the scrollbar? I tried replacing JTextArea with JLabel and that works, but I can't get the JLabel to word wrap the text as good. Any ideas would be highly appreciated.
You could do something like:
Point p = srcollPane.getViewport().getViewPostition();
// add the components to the panel in the viewport of the scrollpane
scrollpane.getViewport().setViewPosition( p );
Now the scrollpane should be reset to its original position before you added the components.

Resizing JTextArea while using lineWrap

I want to create three JTextArea in my swing application.
Each JTextArea has a different size.
The first JTextArea should have 8 columns
The second one should only have 1 column
And the last one should have 50 columns.
My initial problem is that:
Whenever I type something, the JTextArea will keep on re-sizing its width.
This has been fixed by JScrollPane, setLineWrap(true), and setWrapStyleWord(true).
So here's my problem.
Whenever I add setLineWrap() to a JTextArea, the JTextArea will be resized.
My first and second JTextArea have been resized to 12 columns.
I searched and found some solution but they use MigLayout.
Is there any way to add word and line wrap in JTextArea without resizing it (and ofcourse, without the use of MigLayout)?
What's the easiest way to set the columns of JTextArea with word and line wrap?
What's the easiest way to set the columns of JTextArea with word and line wrap?
You create the JTextArea with code like:
JTextArea textArea = new JTextArea(5, 50);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane( textArea );
JPanel panel = new JPanel();
panel.add(scrollPane);
frame.add(panel, BorderLayout.PAGE_START);
By default a JPanel uses a FlowLayout which respects the size of any component added to it. The BorderLayout.PAGE_START will repect the height of any component added to it.
Scrollbars will appear as required what text is added to the text area. So the key is to use a layout manager (or combination of layout managers) that meet your requirement.

Filling a JPanel

I have a few items in a Jpanel which is then pushed to the top and used as a toolbar for a basic search engine. I'm having an issue where my last combobox isn't displaying as there isn't enough room. However, there's a lot of empty space on the left side and I need everything to move across to fill the JPanel so then this can display. So my question is how would I make these items start from the far left and go to right, thanks.
//Labels for combo boxes
JLabel Bookmarklbl = new JLabel("Bookmarks:");
JLabel Historylbl = new JLabel("History:");
FlowLayout flowLayout = new FlowLayout();
MainBrowser.toolBar.setLayout(flowLayout);
//Adding items to Panel
MainBrowser.toolBar.add(Bookmarklbl);
MainBrowser.toolBar.add(BookmarkList);
MainBrowser.toolBar.add(bookmarkbtn);
MainBrowser.toolBar.add(back);
MainBrowser.toolBar.add(forward);
MainBrowser.toolBar.add(MainBrowser.addressbar);
MainBrowser.toolBar.add(home);
MainBrowser.toolBar.add(reload);
MainBrowser.toolBar.add(Historylbl);
MainBrowser.toolBar.add(historyList);
//Set the things added from left to right
MainBrowser.main.setComponentOrientation(
ComponentOrientation.LEFT_TO_RIGHT);
//Add Panel to main frame
MainBrowser.main.add(MainBrowser.toolBar,BorderLayout.NORTH);
How the bar looks:http://postimg.org/image/l314iw6eh/
Assuming toolbar is JPanel and is using FlowLayout, this code might help you,
JPanel panel = new JPanel(); // your toolbar panel
FlowLayout flowLayout = (FlowLayout) panel.getLayout(); // flowlayout
flowLayout.setAlignment(FlowLayout.LEFT); // alignment to left
contentPane.add(panel, BorderLayout.NORTH); // adding this panel to original frame
Hope this helps
The default for a FlowLayout is CENTER. If there is not enough space to display all the components, then the components are wrapped to the next line. Changing the alignment to LEFT won't fix this problem (just the default alignment of components).
showing the combobox bar is very large is there anyway I can limit the width?
You can limit the width of the combo box by using:
comboBox.setPrototypeDisplayValue( "XXXXXXXXXX" );
This will limit the preferred size of the combo box so it can display on your toolbar.
However, you will still want to see the full text of the items when the popup is displayed. For this you can use the Combo Box Popup.
You can try to use BoxLayout, like:
toolBar.setLayout(new BoxLayout(toolBar,BoxLayout.X_AXIS)).
Maybe this will work.
MainBrowser.toolbar.set(new FlowLayout(FlowLayout.LEFT));
EDIT
Sorry it is MainBrowser.toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));

Multiple Java GUI Components in one cell of GridLayout?

Is it possible to add three JTextFields inside of one cell in a Java GridLayout? If not, how can I have a grid based layout where I can set the preferred height of each cell and add more than one Java GUI component to a cell?
Thanks!
You should add them all to one panel and add this panel to the GridLayout panel.
For example:
JPanel inPanel = new JPanel(); // Create new panel
inPanel.add(new JTextField("TF1"); // Add components to it
inPanel.add(new JTextField("TF2");
inPanel.add(new JTextField("TF3");
myGridPanel.add(inPanel); // Add the panel to a your "GridLayout" panel
Also, maybe GridBagLayout will fit your needs.

How do I add ScrollPane to a panel in Java?

I want to add different buttons, vertically stacked, to a JPanel at run-time and use a JScrollPane so that all buttons will be visible (with some scrolling).
In order to do this, I have added my JPanel to a JScrollPane, after which I add buttons to my JPanel.
However, when I do this the vertical scrollbar does not allow me to see all images. For example when I add 7 buttons I can only scroll to see 5 full images and half of the 6 images.
Why doesn't my scrollbar allow me to display all 7 buttons?
Create the panel and scrollpane like:
JPanel panel = new JPanel();
JScrollPane scrollPane = new JScrollPane( panel );
When you add buttons to the panel at run time the code should be:
panel.add( button );
panel.revalidate();
As long as you are using a layout manager the preferred size will be recalculated and the scrollbar will appear.
Make scroll pane a wrapper over your panel - new JScrollPane (myPanel) and add it instead of naked panel in your panel's container.
You also may want to play with its setPreferredSize() method.

Categories