I have a little issue with my program, so I ask for your help.
I added a JScrollPane in a JFrame and in that JScrollPane I added a JPanel. Scrolling that JPanel with my mouse works normally.
But, in an other script, there's a while(true) running, and it detects when I press the keys: W,S,A,D. And if it detect a key press, it uses:
mainWorldScrollPane.getViewport().setLocation();
//mainWorldScrollPane is my JScrollPane
to scroll the JPanel , but the problem is that it does not scroll the JPanel, it scrolls the ViewPort of the JScrollPane.
And this is what I get:
So, my question is:
Can somebody tell me how do I scroll the JPanel with those keys, not the //JScrollPane's ViewPort?
Thanks for your help!
Related
I have some JLabels which are not in view on the JFrame. My JFrame has the dimensions: 1366x by 750y. I want to put a few JLabels that are below these dimensions, i.e. at position 800y. It will be a JLabel which has the size 100x by 50y.
I want to have the label already declared and customised from the swing class. But I want to only add it to a scroll panel when I press a button. That way, the scroll panel will not scroll until you press the button. This is how it will work:
Panel scroll panel which doesn't scroll
Button is clicked
The label is added to the scroll panel
The scroll panel can now scroll. It will scroll because it is set to ASNEEDED for the verticalscrollbarpolicy.
The problem I have is that I can't see the label when it is under the JFrame in the design view of eclipse. Here is a photo:
How can I do this? Please ask for clarification if required.
EDIT
Now If I clicked on some other component above the currently selected label, the eclipse workbench area would scroll up and I wouldn't be able to scroll down to it. To find this label, I would have to find it in the Component Structures list on the left side. I want to be able to scroll the actual workbench area and I want to be able to see the text in that label which is circled.
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
I've got a vertically scrolling JPanel, on that JPanel are a couple of JScrollPanes, when the user scrolls down through the panel, if the users mouse goes over the JScrollPane they are no longer able to scroll the JPanel, the have to move their mouse off the JScrollPane and back onto the panel. It gets quite annoying after a while.
To this end is there any way to tell java to continue scrolling the parent JPanel unless the user clicks on the child JScrollPane and specifically tries to scroll it?
I believe what you're looking for is JScrollPane#setWheelScrollingEnabled(boolean handleWheel)
Is there any way to color a JButton? I want to change it's color after it's pressed to show that it has been pressed. Is there a way that I can do this?
Also, in a JFrame, is there a way to make it so that the whole contents of the window will stretch to the fill the frame when the window is maximized?
Thanks, I appreciate it.
Here's a link for changing JButton color on every state.
How to change a JButton color on mouse pressed?
For the JFrame, you can set the Layout of the JFrame to BorderLayout and add the JPanel in the center of the JFrame, so it will fill all the JFrame even when resized.
Example:
frame.setLayout(new BorderLayout());
frame.add(panel,BorderLayout.CENTER);
I'm having trouble using CardLayout with a JPanel inside of a JScrollPane. Basically I have a main JFrame and a JPanel inside of a JScrollpane, and then a whole bunch of different JPanels that are acting as 'cards' and pop up on different button clicks. The issue is, that some panels aren't scrollable all the way down to the bottom in the JScrollPane, they only scroll to the point of the initial panel.
Can anybody with some experience with NetBeans tell me how to make sure that each panel is fully scrollable in the scroll pane? If you only know the java code for setting this option I'd appreciate that too.
Thanks.