I have got the following basic setup on a Part of my GUI:
A JScrollPane
On it, a JPanel with a BoxLayout (new BoxLayout(tablePanel, BoxLayout.PAGE_AXIS))
And on this Panel, a Bunch ob Panels.
I am trying to scroll to the Panel that has been highlighted... this works ALMOST.
Currenly, if a Panel is only half-visible on the bottom Part, the ScrollPane scrolls to make it fully visible.. great.
If it is half-visible on the TOP part, it does not... I could live with that.
But if a totally invisible Panel at the very bottom is highlighted, the system does not comment, but neither does it scroll there!
if(selectedPanel!=null){
Rectangle targetRectangle = new Rectangle(selectedPanel.getX(), selectedPanel.getY(), selectedPanel.getWidth(), selectedPanel.getHeight());
Rectangle r = scrollPane.getVisibleRect();
if (!r.contains(targetRectangle)) {
tablePanel.scrollRectToVisible(targetRectangle);
}
}
I am unfortunately not 100% sure how it behaves when the second-to-last panel is selected while not visible, because I cannot make that happen without some code-gymnastics; perhaps someone can help with the information I can give at this point.
you have to compare Rectangle from/returns JViewport(visible rectangle from JScrollPane), not from JScrollPane
use selectedPanel.getBounds instead of (selectedPanel.getX(), selectedPanel.getY(), selectedPanel.getWidth(), selectedPanel.getHeight());
still isn't centerred, have to divide JVievports and selectedPanel with 2
the same result as to use single code line JComponentPlacedIntoJScrollPane.scrollRectToVisible(selectedPanel.getBounds())
for better help sooner post an SSCCE/MCVE, short, runnable, compilable
Related
I just recently started using Swing to create GUIs for programs, and it's been pretty fun to mess around with so far. However, I'm having an issue with a JPanel with the layout set to gridLayout. Right now it looks like this:
The grid on the right is a JPanel set to a GridLayout, with each cell being a bordered JLabel. The options on the left are also inside a JPanel, and the left JPanel and right JPanel are nested in a GridBagLayout set on a JFrame.
Essentially, my problem is that I want to "scale" the grid on the right so that each cell is a certain height and width. The grid itself will have a variable number of rows and columns, which are set when the program first starts up. Eventually, I plan to have the right JPanel in a JScrollPane (if that's how that works...), so I'm not really concerned about whether or not all of the grid shows up onScreen.
I tried setting the fill value for the gridLayout to "BOTH" and it gave me the following result:
This is closer to my intention, but I wanted the actual ImageIcon in the JLabels to fill the entire JLabel. Additionally, I would want the JLabels to be the same height and width. However, I don't know exactly how to do that. I've been messing around with it for a while now, and I'm not sure if I'm just too much of a noob with Swing, or if I'm missing something in the documentation.
In the end, I'd like the grid cells to be a fixed height and width, no matter the number of cells, and no matter whether it goes offscreen or doesn't fill it.
(Also, I just thought, maybe it's not the best idea to code this and then shove it in a JScrollPane later and expect it to perform the same.... I guess I'll just see what happens.)
but I wanted the actual ImageIcon in the JLabels to fill the entire JLabel.
Check out Darryl's Stretch Icon which will allow the icon to resize to file the space available for the JLabel.
I am trying to design a class of endless coordinate board with a grid. I have extended a JViewport and it draws at initial position ok (inside JScrollPane). How to tell scroll pane that there are some space to scroll in any direction?
The following does not help
JCoordinateViewport coordinate = new JCoordinateViewport();
coordinate.setBackground(Color.WHITE);
//coordinate.setPreferredSize(new Dimension(10000, 10000));
JScrollPane scroll = new JScrollPane();
//scroll.setViewportView(coordinate);
scroll.setViewport(coordinate);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.getVerticalScrollBar().setMinimum(-10000);
scroll.getVerticalScrollBar().setMaximum(+10000);
scroll.getHorizontalScrollBar().setMinimum(-10000);
scroll.getHorizontalScrollBar().setMaximum(+10000);
UPDATE
Does anybody knows how JScrollPane determines scroll ranges from it's viewport?
UPDATE2
I found, that scrollbars appear t work if maximums and minimums are set after setVisible called.
But unfortunately, paintConponent does not called upon scroll.
Why?
UPDATE3
Although scroll bars work, they don't change viewport position.
Why?
As shown here, painting on a JViewport appears to "stick" to the viewport, while painting on the underlying scrollable component slides beneath. Sizes are integral multiples of TILE: for demonstration purposes, the preferred size of the viewport is made smaller than the underlying panel; in practice, it's better to override getPreferredSize(). See also ScrollAction, which auto-scrolls as the mouse hovers near any border.
CoordinateViewport coordinate = new JCoordinateViewport(); coordinate.setBackground(Color.WHITE); //coordinate.setPreferredSize(new Dimension(10000, 10000));
JScrollPane scroll = new JScrollPane(); //scroll.setViewportView(coordinate);
scroll.setViewport(coordinate);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.getVerticalScrollBar().setMinimum(-10000);
scroll.getVerticalScrollBar().setMaximum(+10000);
scroll.getHorizontalScrollBar().setMinimum(-10000);
scroll.getHorizontalScrollBar().setMaximum(+10000);
.
The following does not help
then issue should be only in the class with name CoordinateViewport
for better help sooner post an SSCCE, short, runnable, compilable, just about JFrame, JScrollPane and JViewport
for reducing of flickering in the JViewport is required to set
own RepaintManager
and by using/with built_in methods in JViewport
JViewport.setScrollMode(JViewport.BLIT_SCROLL_MODE);
JViewport.setScrollMode(JViewport.BACKINGSTORE_SCROLL_MODE);
JViewport.setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
please see Passing current Date and JTable how to change BackGround Color as potential source for SSCCE
I'm trying to put a vertical JScrollBar on top of a JPanel. I can add the scoll bar easily enough, but when I run the program, my scroll bar doesn't have any effect. I can drag it up and down, but the contents of the JPanel don't move. I've read all what I can find about how to do this, and it seems very straightforward, but I'm obviously missing something.
Please note that my JPanel layout is set to NULL. Here is the relevant code. Thanks!
public JDlgResults(ArrayList<AnsweredProblem> probList) {
initComponents();
pnlResults.setLayout(null); //allows free form placing of JLabels
/* Iterate over the ArrayList and add each problem to the results table */
Iterator iter = probList.iterator();
int row = 1;
while(iter.hasNext()) {
addRow(row, iter.next());
row++;
}
JScrollBar jScrollResults = new javax.swing.JScrollBar();
pnlResults.add(jScrollResults);
jScrollResults.setBounds(590, 0, 17, 196);
}
I assume that your initComponents() method sets up a bunch of components and adds them to your contentPane and establishes your dialog (whether it be dialog or a frame).
Simply adding your JScrollPane to that panel wont do the trick.
Instead, add your pnlResults to the JScrollPane instance and make that your contentPane.
That's by design. It would be kind of bad if the scroll bar magically tried to pick up panels in its vicinity and started scrolling them.
On a low level, you can add listeners to the scroll bar and implement scrolling yourself. You would also have to tell the scroll bar how much there is to scroll.
However, this is not what you want. You want a JScrollPane that you wrap around your panel. In that way, you never instantiate the scroll bars directly and do not have to deal with the low-level mechanics of scrolling (which is quite complicated).
I have a JSplitPane with a vertical split. The top component is a table which is in a JScrollPane, and the bottom component is a detailed description of a row. The bottom component always have the same number of elements, so essentially it stays the same height.
Right now, I am using setDividerLocation to position where the pane is being split. If I put 100 into it, the top portion will be 100px, if 500 then the top will be 500px. But if I re-size the JFrame, then the top component is still 100px and the bottom component is really big.
I was wondering if there is a way to set the divider location based on the bottom component instead of the top component?
Sorry if I didn't explain it well, if what I said is still confusing, please let me know and I can try to explain it better.
------ EDIT ------
Thanks to ControlAltDel for figuring it out. This is what my code looks kinda looks like now:
JSplitPane splitPane = new JSplitPane();
splitPane.setTopComponenet(new JTable());
splitPane.setBottomComponent(new JPanel());
splitPane.setResizeWeight(1); // This gives the top component priority when the window is resized
http://docs.oracle.com/javase/7/docs/api/javax/swing/JSplitPane.html#setResizeWeight(double)
Been developing a game for a while, and currently re working the GUI, or at least trying to. Had the massive problem of not being able to resize the frame (without issues), as I didn't understand layout managers very well. A few projects later, and time to come back and do some more on the game, and I hit a problem...
The basic layout of the main frame is, mainPane, containing one gameScrollPane and one controlPanel. The scroll pane is a scroll pane, and the control panel a normal panel. The scroll pane contains the main game panel.
As I wanted the scroll pane to take up most of the screen, with the control panel taking up a small lower area, much the same as many Sim like games, so chose the Border layout for the mainPane. I added the scroll pane and set the constraints CENTER and the control panel added and constriants SOUTH. This didn't show the scroll pane, so I played around trying different constraints, and it seems that only when I set the scroll pane constraint to North, does it display at all.
To demonstrate this, I have created a quick video...
http://screenjel.ly/q5RjczwZjH8
As you can see, when I change the value of NORTH to CENTER and re run, it's like its not there!
Bonus points for anyone who can see a clear second problem which I may start another question for after this issue is solved!
I thank you for your time to read this.
Thanks in advance for any ideas or thoughts :)
Rel
If you'd posted some code to start with then you might have gotten a really quick answer. Luckily, you posted a link in the comments to the other response.
The setContentPane() stuff is weird, especially after doing some things to it that will then get wiped out. However, that's not your problem.
The issue is that you are adding levelMaker and personMover right to mainPane without any constraints. These will then be blowing away anything you set for CENTER... in this case the previously set gameScrollPane.
That's why you see it for NORTH and not for CENTER.
I can't get the video to show. It's been buffering for ages.
My guess would be that the scrollpane is in fact filling the center; it's just your game panel that's not being shown.
Your game panel needs to return reasonable values for getPreferredSize().
Update
Another thing you may want to do is have your game panel implement the Scrollable interface. You can then override getScrollableTracksViewportWidth and ...height to return true so your panel will be forced to the scrollpane's dimensions.