-1

I have a JFrame that lets a user scroll through and view records from a database that looks like:

https://i.stack.imgur.com/d9E5a.png

where the labels never move and the info scrolls under them. My problem is that my user wants the scrollbar to look like the one on the far right (which is inside its own JPanel and added to the JFrame) instead of the one attached to the JScrollPane. Does anyone know of a way to make my ScrollPane to scroll with a separate scrollbar?

camickr
  • 308,339
  • 18
  • 152
  • 272
WickedChester
  • 25
  • 1
  • 5
  • `"Does anyone know of a way to make my ScrollPane to scroll with a separate scrollbar?"` -- doesn't make sense to me. Do you mean that you want to nest the JPanel in its own JScrollPane? If so, then do so. – Hovercraft Full Of Eels Nov 09 '17 at 22:48
  • 1
    Doesn't `JTable` fix the header so the content scrolls under it, but the header stays still? – MadProgrammer Nov 09 '17 at 23:11
  • "Do you mean that you want to nest the JPanel in its own JScrollPane? If so, then do so." @HovercraftFullOfEels I'm already doing that. I have a JPanel where I line up the column names with the data displayed in the JScrollPane which is underneath it. What I'm trying to do is instead of having the scrollbar the JScrollPane somes with, have it be one that I have already displayed on my JFrame on the far right (see picture in original post) – WickedChester Nov 10 '17 at 00:10
  • @MadProgrammer: A JTable looks interesting and could have saved me a bunch of work. I will look into it. But right now that isn't what my question is. I am trying to remove the vertical scrollbar the JScrollPane comes with (the small on the left) and instead make it scroll with a JScrollBar I have in a JPanel (the large one on the right). Is there a way to do this or would it be more work than it is worth? – WickedChester Nov 10 '17 at 00:25
  • [JavaDocs to the rescue](https://docs.oracle.com/javase/8/docs/api/javax/swing/ScrollPaneConstants.html#VERTICAL_SCROLLBAR_NEVER) – MadProgrammer Nov 10 '17 at 00:30
  • @MadProgrammer Thanks but I already know how to do that but what I don't know how to do is make is scroll with a different scrollbar – WickedChester Nov 10 '17 at 01:40
  • Attach a PropertyChangeListener to the the scroll bar, monitor for value changes, use something like scrollToVisbleRect on the container you want scrolled – MadProgrammer Nov 10 '17 at 01:55
  • @MadProgrammer Thanks for the help, I will give it a try – WickedChester Nov 10 '17 at 02:18

1 Answers1

0

Alright, I figured out how to accomplish what I wanted. Instead of reinventing the wheel and make a JScrollBar work with a JScrollPane. Instead, I extracted the vertical scrollbar from my JScrollPane, messed with it, and stuck it in my JPanel like so:

  //Where dataPanel is a JPanel I want to be able to scroll
  JScrollPane roller = new JScrollPane (dataPanel, 
                           JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                           JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

  JPanel scrollerPanel = new JPanel();
  JScrollBar scrollBar = roller.getVerticalScrollBar();
  scrollBar.setPreferredSize(new Dimension (20, 100));

  scrollPanel.add(scrollBar);
WickedChester
  • 25
  • 1
  • 5