Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #2404 > unrolled thread

JScrollPane resize / new

Started by"lemmi" <lemmi@THRWHITE.remove-dii-this>
First post2011-04-27 15:38 +0000
Last post2011-04-27 15:40 +0000
Articles 7 — 3 participants

Back to article view | Back to comp.lang.java.gui


Contents

  JScrollPane resize / new "lemmi" <lemmi@THRWHITE.remove-dii-this> - 2011-04-27 15:38 +0000
    Re: JScrollPane resize / "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> - 2011-04-27 15:38 +0000
      Re: JScrollPane resize / "lemmi" <lemmi@THRWHITE.remove-dii-this> - 2011-04-27 15:39 +0000
        Re: JScrollPane resize / "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:39 +0000
          Re: JScrollPane resize / "lemmi" <lemmi@THRWHITE.remove-dii-this> - 2011-04-27 15:39 +0000
            Re: JScrollPane resize / "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:39 +0000
              Re: JScrollPane resize / "lemmi" <lemmi@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000

#2404 — JScrollPane resize / new

From"lemmi" <lemmi@THRWHITE.remove-dii-this>
Date2011-04-27 15:38 +0000
SubjectJScrollPane resize / new
Message-ID<1189422454.499481.64600@19g2000hsx.googlegroups.com>
  To: comp.lang.java.gui
Hi there,

I have an application that uses a JScrollPane to display a component
that will be resized every once in a while. At the same time of the
resize I also want to set a new position for the resized component.
E.g. I am doubling the size of the  component and want to change its
location within the viewport so that the component stays centered.
What is the best way to accomplish this without seeing a flickering
behaviour?

Dirk

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#2406 — Re: JScrollPane resize /

From"Roedy Green" <roedy.green@THRWHITE.remove-dii-this>
Date2011-04-27 15:38 +0000
SubjectRe: JScrollPane resize /
Message-ID<nvcae3tt1obf5o6sdltu5mtm166nff8tn3@4ax.com>
In reply to#2404
  To: comp.lang.java.gui
On Mon, 10 Sep 2007 11:07:34 -0000, lemmi <dlemmermann@gmail.com>
wrote, quoted or indirectly quoted someone who said :

>What is the best way to accomplish this without seeing a flickering
>behaviour?

You can't avoid a flicker. Swing is going to have to totally repaint
the window.

to avoid seeing intermediates, you might experiment with setVisible(
false ) and setVisible( true );
-- 
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2424 — Re: JScrollPane resize /

From"lemmi" <lemmi@THRWHITE.remove-dii-this>
Date2011-04-27 15:39 +0000
SubjectRe: JScrollPane resize /
Message-ID<1189591512.545327.3910@g4g2000hsf.googlegroups.com>
In reply to#2406
  To: comp.lang.java.gui
On Sep 10, 2:10 pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
>
> You can't avoid a flicker. Swing is going to have to totally repaint
> the window.
>

It is not really a flickering because of Swing repaints. The
flickering
is caused by the fact that the wrong region of the scrolled component
will
be shown first before the new region will be shown. Imagine having an
image
label inside a scrollpane. The user wants to zoom into the center of
the
image. Then the size of the label needs to be doubled (if that is the
scaling factor) and the location of the label needs to be adjusted so
that
the center of the image is shown in the center of the scrollpane.

Dirk

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2425 — Re: JScrollPane resize /

From"Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
Date2011-04-27 15:39 +0000
SubjectRe: JScrollPane resize /
Message-ID<781a99469ef62@uwe>
In reply to#2424
  To: comp.lang.java.gui
lemmi wrote:
>On Sep 10, 2:10 pm, Roedy Green <see_webs...@mindprod.com.invalid>
>wrote:
>
>> You can't avoid a flicker. Swing is going to have to totally repaint
>> the window.
>
>It is not really a flickering because of Swing repaints. The
>flickering
>is caused by the fact that the wrong region of the scrolled component
>will
>be shown first before the new region will be shown. Imagine ..

.posting an SSCCE, that allows others to see
an actual *example* of the behaviour on-screen.
<http://www.physci.org/codes/sscce.html>

I might have helped when I originally saw your post,
but without code, I could not be bothered speculating.

-- 
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-gui/200709/1

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2427 — Re: JScrollPane resize /

From"lemmi" <lemmi@THRWHITE.remove-dii-this>
Date2011-04-27 15:39 +0000
SubjectRe: JScrollPane resize /
Message-ID<1189681615.137786.279480@k79g2000hse.googlegroups.com>
In reply to#2425
  To: comp.lang.java.gui
>
> .posting an SSCCE, that allows others to see
> an actual *example* of the behaviour on-screen.
> <http://www.physci.org/codes/sscce.html>
>

Yes, yes, yes, I know an SSCCE helps in many situations but this
seemed to be "answerable" without one. Especially since it appears
to be someting that many UI developers have run into before. Anyway,
I will try to put one together.

Dirk

P.S.: Andrew, your homepage URL doesn't seem to work.


>
> --
> Andrew Thompsonhttp://www.athompson.info/andrew/
>
> Message posted via JavaKB.comhttp://www.javakb.com/Uwe/Forums.aspx/java-gui/200709/1

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2428 — Re: JScrollPane resize /

From"Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
Date2011-04-27 15:39 +0000
SubjectRe: JScrollPane resize /
Message-ID<78272a08d48ab@uwe>
In reply to#2427
  To: comp.lang.java.gui
lemmi wrote:
..
>P.S.: Andrew, your homepage URL doesn't seem to work.

Yeah.. ongoing problems with the server.  (shrugs) Sh*t happens.

-- 
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via http://www.javakb.com

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2670 — Re: JScrollPane resize /

From"lemmi" <lemmi@THRWHITE.remove-dii-this>
Date2011-04-27 15:40 +0000
SubjectRe: JScrollPane resize /
Message-ID<1192717678.250406.104230@t8g2000prg.googlegroups.com>
In reply to#2428
  To: comp.lang.java.gui
So, I finally managed to write an SSCCE for this problem. Actually two
of them, which are almost identical. The first one creates a frame
with a scrollpane in its center and two zoom buttons at the bottom.
The user can zoom in and out, which causes the panel that is the
content of the scrollpane to be resized (double size or half size).
Both SSCCEs try to center the content after the user performed a zoom
in or a zoom out. The first SSCCE completely fails, because the call
to the center() method comes to early. The revalidation of the resized
panel happens asynchronously, so the center() method works on old
values. The second SSCCE uses a component listener so that the
centering can happen AFTER the panel has actually been resized. What
you will notice is that the revalidation causes a repaint and
scrollbar adjustment and then the centering causes another repaint and
scrollbar adjustment. This results in flickering behaviour. I was
wondering whether there is any better way, so that the resizing and
the repositioning can happen in one step. Any help is much
appreciated.

Dirk

1. SSCCE ------------------------------------------

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;

public class ScrollPaneTest {

	/**
	 * Constructs the frame with the scrollpane in its center and a
button
	 * panel, which contains the zoom buttons, in its bottom location.
	 */
	public static void main(String[] args) {
		JFrame frame = new JFrame("Test");
		final TestPanel testPanel = new TestPanel();
		JScrollPane pane = new JScrollPane(testPanel);
		frame.add(pane);

		JPanel buttonPanel = new JPanel();
		JButton zoomIn = new JButton("Zoom In");
		JButton zoomOut = new JButton("Zoom Out");
		buttonPanel.add(zoomIn);
		buttonPanel.add(zoomOut);
		zoomIn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				testPanel.zoomIn();
			}
		});
		zoomOut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				testPanel.zoomOut();
			}
		});

		buttonPanel.add(zoomIn);
		buttonPanel.add(zoomOut);
		frame.add(BorderLayout.SOUTH, buttonPanel);

		frame.pack();
		frame.setVisible(true);
	}
}

/**
 * This panel is the content of the scrollpane.
 */
class TestPanel extends JPanel {
	private Dimension ps = new Dimension(300, 300);

	@Override
	protected void paintComponent(Graphics g) {
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		super.paintComponent(g);
		g.drawLine(0, 0, getWidth() - 1, getHeight() - 1);
		g.drawLine(0, getHeight() - 1, getWidth() - 1, 0);
	}

	@Override
	public Dimension getPreferredSize() {
		return ps;
	}

	public void zoomIn() {
		ps.setSize(ps.getWidth() * 2, ps.getHeight() * 2);
		revalidate();
		center();
	}

	public void zoomOut() {
		ps.setSize(ps.getWidth() / 2, ps.getHeight() / 2);
		revalidate();
		center();
	}

	private void center() {
		JViewport viewPort = (JViewport) getParent();
		viewPort.setViewPosition(new Point(ps.width -
viewPort.getSize().width, ps.height - viewPort.getSize().height));
	}
}





2. SSCCE ------------------------------------------

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;

public class ScrollPaneTestWithListener {

	/**
	 * Constructs the frame with the scrollpane in its center and a
button
	 * panel, which contains the zoom buttons, in its bottom location.
	 */
	public static void main(String[] args) {
		JFrame frame = new JFrame("Test");
		final TestPanelWithListener testPanel = new TestPanelWithListener();
		JScrollPane pane = new JScrollPane(testPanel);
		frame.add(pane);

		JPanel buttonPanel = new JPanel();
		JButton zoomIn = new JButton("Zoom In");
		JButton zoomOut = new JButton("Zoom Out");
		buttonPanel.add(zoomIn);
		buttonPanel.add(zoomOut);
		zoomIn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				testPanel.zoomIn();
			}
		});
		zoomOut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				testPanel.zoomOut();
			}
		});

		buttonPanel.add(zoomIn);
		buttonPanel.add(zoomOut);
		frame.add(BorderLayout.SOUTH, buttonPanel);

		frame.pack();
		frame.setVisible(true);
	}
}

/**
 * This panel is the content of the scrollpane. It implements a
component
 * listener. It will cause a change of the viewport position when it
receives an
 * event letting it know that its own size has changed.
 */
class TestPanelWithListener extends JPanel implements
ComponentListener {
	private Dimension ps = new Dimension(300, 300);

	public TestPanelWithListener() {
		// listens for changes to itself
		addComponentListener(this);
	}

	@Override
	protected void paintComponent(Graphics g) {
		try {
			// Artifically decrease repaint performance to simulate
			// real world application behaviour.
			Thread.sleep(100);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		super.paintComponent(g);
		g.drawLine(0, 0, getWidth() - 1, getHeight() - 1);
		g.drawLine(0, getHeight() - 1, getWidth() - 1, 0);
	}

	@Override
	public Dimension getPreferredSize() {
		return ps;
	}

	public void zoomIn() {
		ps.setSize(ps.getWidth() * 2, ps.getHeight() * 2);
		revalidate();
	}

	public void zoomOut() {
		ps.setSize(ps.getWidth() / 2, ps.getHeight() / 2);
		revalidate();
	}

	private void center() {
		JViewport viewPort = (JViewport) getParent();
		viewPort.setViewPosition(new Point(
				(ps.width - viewPort.getSize().width) / 2,
				(ps.height - viewPort.getSize().height) / 2));
	}

	public void componentHidden(ComponentEvent e) {
	}

	public void componentMoved(ComponentEvent e) {
	}

	public void componentResized(ComponentEvent e) {
		center();
	}

	public void componentShown(ComponentEvent e) {
	}
}

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web