I have 2 tables each are added for a scrollpane however the scrollbar only apperas in one of them
JScrollPane pane = new JScrollPane(table);
JScrollPane pane_2 = new JScrollPane(table_1);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table_1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
panel.add(pane_2);
panel.add(pane);
the scrollbar only appears in pane_2 and not in pane
Any ideas how to fix this ?
Possible reason of such behavior could be different size of the tables. According to my experience JScrollPane shows the scrollbar only in case that its content is bigger and does not fit within the pane.
You can try to experiment with following JScrollPane properties:
horizontalScrollBarPolicy
verticalScrollBarPolicy
They can contain following values (e.g. in vertical case):
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED
ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS
They should control the behavior you ask about. The names are self explanatory I think.
Related
It has been identified as a possible duplicate of the question:
JPanel Window not scaling when resize the main frame
I do not agree, because at that topic the problem is that the JPanel does not resize. In my case, the JPanel has no problems, but the JScrollPane is not getting resized!
I have a JFrame, there is a JPanel inside, containing a JScrollpane, which contains a JTable.
The problem is, that if the JFrame is resized, the JScrollpane stays the same, doesn't matter if the JFrame is made really small, so that the scroll pane does not even fit in the window or really big.
I want to make the JScrollpane adjust itself according to its JPanel, which gets adjusted by resizing of the window.
If the same thing is done by using a JTextArea inside the JScrollPane, everything works just fine -- resizing the JPanel resizes the scroll pane.
Do you have any idea why the JTable ruins the automatic resizing of the JScrollPane, and how to get around it? It seems, that the LayoutManager somehow is not able to handle it properly... (I also tried to add images showing what's happening, but I still don't have enough of reputation...)
Here is the code:
JFrame tableFrame = new JFrame("Test Table");
editPanel vadEditPane;
vadEditPane = new editPanel(vadData);
vadEditPane.setOpaque(true); //content panes must be opaque
tableFrame.getContentPane().add(vadEditPane, BorderLayout.CENTER);
public class editPanel extends JPanel {
public editPanel(dataConv vadData){
JTable vadTable;
String[] columnNames = {---SOME COLUMN NAMES---};
Object[][] data = {---SOME DATA---};
vadTable = new JTable(data, columnNames);
vadTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
vadTable.setFillsViewportHeight(true);
vadTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane scrollPane = new JScrollPane(vadTable);
scrollPane.setBackground(Color.RED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
add(scrollPane);
setBackground(Color.ORANGE);
}
}
I do not agree, because at that topic the problem is that the JPanel does not resize. In my case, the JPanel has no problems, but the JScrollPane is not getting resized!
If you read the comment in the first answer and take the time to understand the suggestion you will indeed find that the solution is the same.
The answer there states that a JPanel uses a FlowLayout (by default). The FlowLayout respects the preferred size of any component added to it. So the size of the table will never change even as the frame size changes.
So the solution suggests to change the layout manager of the panel so that the child components can be resized as the size of the panel changes. I would suggest using a BorderLayout.
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.
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.
As I am a beginner with java swing (I'm using netbeans), I am having trouble integrating tables in my gui.
I have a tab with a panel with research options in a db, and I want to add beneath this panel the produced table with the research results.
With swing, i have put a JPanel 'researchPanel' within my tab and what I do is the following:
table = my_research_function(options);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
JScrollPane pane = new JScrollPane(table);
pane.setViewportView(table);
//resultsPanel.setLayout(new BorderLayout());
resultsPanel.add(pane);
// I'm just using this to ensure i get a table
// with correct results - works fine
JOptionPane.showMessageDialog(null, new JScrollPane(table));
As I saw in the produced code by swing, resultsPanel already has a layout. Just in case, i did a resultsPanel.setLayout(new BorderLayout()) and resultsPanel.add(pane, BorderLayout.CENTER) and I saw some shades in my panel but still no data.
Furthermore, in the Design tab of netbeans, I've set the panel to Auto-Resize both Vertical and Horizontal as the results table can be quite big.
What am I missing?
Swing component can have only one parent component.
You can't see your JTable in resultsPanel because of the following reasons
At first you create JScrollPane pane = new JScrollPane(table);
which add to resultsPanel,
but then you override parent component of your table here JOptionPane.showMessageDialog(null, new JScrollPane(table));.
So remove last line and your JTable will be visible.
I need to create a panel where I can put some rectangles and it automatically reorder just inserting a scrollbar and growing up vertically. Also this panel can be resizable and again the rectangles must to be reordered to correctly be displayed inside the panel.
If I understand the question you want components to wrap to the next line so that the panel grows vertically while the width remains fixed.
If so then check out the WrapLayout
Note: the FlowLayout already supports the wrapping of components to a new row on the panel. This issue is that the preferred size calculation assumes all components are placed on a single row. The WrapLayout overrides the preferred size calculation to support the wrapping of components on a new row.
Use a JScrollPane. If you never want a horizontal scroll bar you can add the following:
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
(By default the scroll pane will add horizontal and vertical scroll bars when required.)
The scroll pane itself will only be resizeable if you add it to a Container with the appropriate layout manager; e.g.
JFrame frm = new JFrame();
frm.setLayout(new BorderLayout());
JScrollPane sp = new JScrollPane();
frm.add(sp, BorderLayout.CENTER); // Adding a component to the CENTER will cause the component to grow as the frame is resized.