Results 1 to 5 of 5
  1. #1
    TrekSoft is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Question Button repositioning in JScrollPane

    Hi,

    I have a problem with adding a button to a JScrollPane I have. What I'd like to do is to position it in a set location within the pane and then be able to scroll around and have the button move appropriately as I scroll. What happens now is that when I scroll, the button repositions (although it has some trouble with the repainting) to keep the same relative position it had before (50 pixels from the upper left corner).

    Does anyone know how to solve this problem?

    Thanks,

    David

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    TrekSoft is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default Sscce

    Hi Fubarable,

    Thank you for getting back to me.

    Here is my sample case:

    Java Code:
    import java.awt.BorderLayout;
    import javax.swing.*;
    
    public class ButtonInScrollPaneTest extends JFrame
    {
    
        public static void main(String[] args)
        {
            ButtonInScrollPaneTest.getMainFrame();
        }
    
        private static ButtonInScrollPaneTest mainFrame;
    
        public static ButtonInScrollPaneTest getMainFrame()
        {
            if (mainFrame == null) {
                mainFrame = new ButtonInScrollPaneTest();
            }
    
            return mainFrame;
        }
    
        private ButtonInScrollPaneTest()
        {
            setTitle("Button in scroll pane test");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100,100,400,300);
    
            JPanel background = new JPanel();
            background.setLayout(new BorderLayout());
            getContentPane().add(background);
    
            Icon image = new ImageIcon( "C:\\pattern.jpg" );
    	JLabel label = new JLabel( image );
    
            JButton button1 = new JButton();
            button1.setBounds(50,50, 30, 30);
    
            JScrollPane scroller = new JScrollPane();
            scroller.getViewport().add(label);
            scroller.add(button1);
    
            background.add(scroller,BorderLayout.CENTER);
            setVisible(true);
        }
    
    }
    To get the background image to work (which is what I used to cause the panel to be scrollable), just save the attached image to your main C: directory.

    The problem is that when you scroll the panel, the button appears to move but when you hover over the area on the screen it used to be, it reappears and you can see it has been repositioned to have the same relative location.

    Is there a way to stop this and make it keep the same absolute position so that you can scroll away from it and then scroll back?
    Attached Thumbnails Attached Thumbnails Button repositioning in JScrollPane-pattern.jpg  

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If you want the JButton "glued" to the image held by the JScrollPane, then it has to be added to that component. I would recommend that the image be a background image of a JPanel, which means it must be drawn in the JPanel's paintComponent method, and that you add your JButton to this same JPanel. For example:

    Java Code:
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class ButtonInScrollPaneTest2 extends JPanel {
       // TODO: **** change path below to match your actual image path *******
       public static final String IMAGE_PATH = "src/yr2010/m12/a/pattern.jpg";
       private Image image;
       private JButton button = new JButton("Button");
       
       public ButtonInScrollPaneTest2() {
          try {
             image = ImageIO.read(new File(IMAGE_PATH));
          } catch (IOException e) {
             System.err.println("Image " + IMAGE_PATH +  " not found");
             e.printStackTrace();
             System.exit(1);
          }
          setPreferredSize(new Dimension(image.getWidth(null), image.getHeight(null)));
          int eb = 50;
          setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb));
          setLayout(new FlowLayout(FlowLayout.LEFT));
          add(button);
       }
       
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          if (image != null) {
             g.drawImage(image, 0, 0, null);
          }
       }
    
       private static void createAndShowUI() {
          JScrollPane scrollPane = new JScrollPane(new ButtonInScrollPaneTest2());
          scrollPane.setPreferredSize(new Dimension(400, 300));
          JFrame frame = new JFrame("ButtonInScrollPaneTest2");
          frame.getContentPane().add(scrollPane);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }

  5. #5
    TrekSoft is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    Hi,

    Thank you for getting back to me - the problem is that in the specific case I'm working with, it doesn't work to add it to the component itself because for some reason, the component (which is a graph object using a 3rd part library by jGraph) deactivates the button essentially so I can't click on it.

    Is there any way you know that I can work around this?

    EDIT: I ended up solving my problem in a different way unrelated to the issue so I personally don't need help with this anymore. If someone knows the solution to work around it, though, maybe someone else would find it helpful.
    Last edited by TrekSoft; 12-07-2010 at 07:44 PM. Reason: Solved problem

Similar Threads

  1. Objects repositioning automatically
    By ashish9590 in forum NetBeans
    Replies: 5
    Last Post: 06-30-2010, 12:10 PM
  2. repositioning
    By ashish9590 in forum NetBeans
    Replies: 2
    Last Post: 02-14-2010, 06:42 AM
  3. Repositioning An unwanted JInternalFrame
    By marco.c84 in forum AWT / Swing
    Replies: 8
    Last Post: 03-18-2009, 09:42 PM
  4. jscrollpane
    By kaemonsaionji in forum New To Java
    Replies: 3
    Last Post: 02-25-2009, 08:39 AM
  5. help with JScrollPane
    By tommy in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 07:58 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •