I have a JScrollPane and a JPanel.
JScrollPane scrollPane_yst = new JScrollPane();
scrollPane_yst.setBorder(null);
scrollPane_yst.setViewportBorder(null);
panel.add(scrollPane_yst);
JPanel panel_yst = new JPanel();
panel_yst.setPreferredSize(new Dimension(150, 1000));
panel_yst.setBorder(null);
panel_yst.setBackground(Color.DARK_GRAY);
scrollPane_yst.setViewportView(panel_yst);
panel_yst.setLayout(new BoxLayout(panel_yst, BoxLayout.Y_AXIS));
During runtime, I add other components to panel_yst, but it doesn't get bigger when the components exceed its size.
(Same problem with and without setting preferred size)
ANSWER:
I found the solution. The problem was, that I set the preferred size of the panels that I lateron added onto the panel inside the scrollpane.
Because of that, the size of those panels was messed up. Just dont set the PreferredSize of the components that you add onto the panel.
I found the solution. The problem was, that I set the preferred size of the panels that I lateron added onto the panel inside the scrollpane.
Because of that, the size of those panels was messed up. Just dont set the PreferredSize of the components that you add onto the panel.
Related
I'm trying to add precreated JPanel instances into a bigger JPanel into a JScrollPane, but I can't get it to work.
I'm using NetBeans to create the container, then using the context menu to include it into a scroll panel.
To add the JPanel at runtime I'm doing this:
ActivityPanel actPanel;
for(Reservation r: mainFrame.getKiosko().getReservations()) {
if( r.getUser().equals(user)) {
actPanel = new ActivityPanel(r.getActivity(),mainFrame);
actPanel.setHour(r.getHour());
Dimension actDim = new Dimension(600, 100);
actPanel.setPreferredSize(actDim);
actPanel.setMaximumSize(actDim);
actPanel.setMinimumSize(actDim);
pnlReservations.add(actPanel);
}
}
But it just is not working. Should I create the ScrollPanel at runtime? If so How should I do it?
pnlReservation has a BoxLayout by page axis.
actPanel.setPreferredSize(actDim);
Don't try to manually control the preferred size of a component. The size of the panel should be determined by the components you add to the panel.
Same with your pnlReservation, don't hardcode a preferred size. The layout manager will determine its preferred size based on the preferred size of the components added to it.
Then the scrollbars will appear automatically as the preferred size of the pnlReservation dynamically changes when you add components to it.
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 JFrame window, and I'd like to add a scrollable JTable towards the middle of it. I have a method, called collectionTableScrollPane() that generates the JScrollPane (and I know this is guaranteed to work).
I then proceed to add it to my mainPanel panel. However, I'd like there to be some forced 30px padding on the left and right of the JScrollPane. Logically, I would create a holding JPanel with a centred FlowLayout, and add Box.createHorizontalStrut(30) either side of the JScrollPane.
JPanel tableHolderPanel = new JPanel(new FlowLayout());
mainPanel.add(tableHolderPanel);
tableHolderPanel.add(Box.createHorizontalStrut(30));
tableHolderPanel.add(collectionTableScrollPane());
tableHolderPanel.add(Box.createHorizontalStrut(30));
However, I'm getting a strange result, where the JScrollPane in the middle of the window (denoted by the arrows) sort of becomes ineffectual.
Does anyone know what the problem is?
Note that the JTable contains four rows, of which only two are visible.
I had some issues in the past when i used a JScrollPane inside a panel with a FlowLayout. The behaviour could be tricky, when the content grow, the horizontal scrollbar may appear or the FlowLayout should add a new line.
In your case, i will replace the FlowLayout by a BorderLayout :
JPanel tableHolderPanel = new JPanel(new BorderLayout());
mainPanel.add(tableHolderPanel);
tableHolderPanel.add(Box.createHorizontalStrut(30), BorderLayout.WEST);
tableHolderPanel.add(collectionTableScrollPane(), BorderLayout.CENTER);
tableHolderPanel.add(Box.createHorizontalStrut(30), BorderLayout.EAST);
As far as I'm aware, Box is suppose to be used with the BoxLayout, this may be causing you some issues. Instead, why not use a EmptyBorder on the tableHolderPane
BoxLayout accepting size that came from JComponents, the same issue with default FlowLayout pre_implemented for JPanel
you have to returns PreferredSize by overrode JPanel nested JScrollPane,
use another LayoutManager, e.g. GridBagLayout or todays MigLayout
use NestedLayout, by using BorderLayout where you put two JLabels (e.i. that returns PreferredSize) to the EAST and WEST area
everything depends if you really to want to create the empty area and if shoud be resiziable or not
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.
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.