Results 1 to 5 of 5
- 11-29-2010, 09:05 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 4
- Rep Power
- 0
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
-
Show your code, preferably in a small compilable program, an SSCCE, and we'll be better able to help you.
- 12-02-2010, 01:14 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 4
- Rep Power
- 0
Sscce
Hi Fubarable,
Thank you for getting back to me.
Here is my sample case:
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.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); } }
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?
-
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(); } }); } }
- 12-06-2010, 08:54 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 4
- Rep Power
- 0
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
-
Objects repositioning automatically
By ashish9590 in forum NetBeansReplies: 5Last Post: 06-30-2010, 12:10 PM -
repositioning
By ashish9590 in forum NetBeansReplies: 2Last Post: 02-14-2010, 06:42 AM -
Repositioning An unwanted JInternalFrame
By marco.c84 in forum AWT / SwingReplies: 8Last Post: 03-18-2009, 09:42 PM -
jscrollpane
By kaemonsaionji in forum New To JavaReplies: 3Last Post: 02-25-2009, 08:39 AM -
help with JScrollPane
By tommy in forum AWT / SwingReplies: 1Last Post: 08-06-2007, 07:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks