Note that there are some explanatory texts on larger screens.

plurals
  1. PO"Atomic rendering" in Swing
    primarykey
    data
    text
    <p>I have a swing application that contains an SVG canvas inside a JScrollPane. The application modifies the displayed SVG document, which also leads to a change of the size of the document. This size change needs to be reflected in the application. The SVG canvas is resized and the viewport of the JScrollPane is scrolled so that it displays the correct section of the canvas.</p> <p>However, this leads to something like a "visual jumping", because the user first sees the change of the canvas size and after that sees the scrolling operation.</p> <p>Is there a way to tell java to stop processing rendering events on a given component (and its subcomponents) and only resume after I have finished my modifications to only display the result of all modifications?</p> <p>Here is my idea in pseudo code:</p> <pre><code>myScrollPane.suspendRendering(); svgDocument.changeSize(); svgCanvas.changeSize(); myScrollPane.getViewport().scrollToCorrectPosition; myScrollPane.resumeRendering(); </code></pre> <p>I tried with <code>myScrollPane.setIgnoreRepaint(true)</code>, but it seems to not have any effect here (even if I never set <code>ignoreRepaint</code> to <code>false</code> again.</p> <p>And here is an SSCCE that tries to simulate the effect:</p> <pre><code>import java.awt.BorderLayout; import java.awt.Dimension; 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.JScrollPane; import javax.swing.SwingUtilities; public class Jumping extends JFrame { private JButton innerPanel= new JButton("Some silly, useless text, just for fun. And it goes on even longer. But that's not a problem."); private JScrollPane scrollPane= new JScrollPane(innerPanel); private JButton btnJump= new JButton("Jump"); private int lastWidth= 1024; public Jumping(){ this.setLayout(new BorderLayout()); this.add(btnJump, BorderLayout.NORTH); this.add(scrollPane, BorderLayout.CENTER); this.scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); this.scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); this.innerPanel.setPreferredSize(new Dimension(1024, 768)); this.innerPanel.setSize(1024, 768); this.innerPanel.setMinimumSize(new Dimension(1024, 768)); this.innerPanel.setMaximumSize(new Dimension(1024, 768)); this.setSize(640, 480); this.btnJump.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try{ System.err.println("&gt; actionPerfomed"); //resize the canvas new Thread(){ public void run() { System.err.println("&gt; SwingWorker.doInBackground "+SwingUtilities.isEventDispatchThread()); SwingUtilities.invokeLater(new Runnable(){ public void run(){ System.err.println("&gt; resize canvas "+SwingUtilities.isEventDispatchThread()); final int newWidth= (int) (lastWidth * 1.5); innerPanel.setSize(newWidth, 768); innerPanel.setPreferredSize(new Dimension(newWidth, 768)); innerPanel.setMinimumSize(new Dimension(newWidth, 768)); innerPanel.setMaximumSize(new Dimension(newWidth, 768)); lastWidth= newWidth; System.err.println("&lt; resize canvas "+SwingUtilities.isEventDispatchThread()); } }); //scroll to correct position SwingUtilities.invokeLater(new Runnable(){ public void run(){ System.err.println("&gt; scroll to pos "+SwingUtilities.isEventDispatchThread()); try { System.err.println("&lt; sleep "+SwingUtilities.isEventDispatchThread()); Thread.sleep(500); System.err.println("&gt; sleep "+SwingUtilities.isEventDispatchThread()); } catch (InterruptedException ex) { ex.printStackTrace(); } final Point viewPos= scrollPane.getViewport().getViewPosition(); scrollPane.getViewport().setViewPosition(new Point(viewPos.x + 50, viewPos.y)); System.err.println("&lt; scroll to pos "+SwingUtilities.isEventDispatchThread()); } }); System.err.println("&lt; SwingWorker.doInBackground "+SwingUtilities.isEventDispatchThread()); } }.start(); System.err.println("&lt; actionPerfomed "+SwingUtilities.isEventDispatchThread()); }catch(Exception ex){ ex.printStackTrace(); } } }); } public static void main(String[] args){ final Jumping frame= new Jumping(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload