JFrame flicker on programmatic resize.

Here is the code:
package ui.components;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
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.SwingUtilities;
public class Test {
    public Test()
        final JFrame frame = new JFrame();
        frame.setBounds(100, 100, 300, 300);
        frame.setBackground(Color.blue);
        JPanel panel = new JPanel(new BorderLayout());
        JPanel up = new JPanel(new BorderLayout());
        up.setPreferredSize(new Dimension(100, 100));
        up.setBackground(Color.blue);
        JButton button = new JButton("add size/shrink size");
        button.addActionListener(
            new ActionListener() {
                boolean addf = true;
                public void actionPerformed(ActionEvent e)
                    if (addf) {
                        frame.setSize(
                            frame.getWidth(),
                            frame.getHeight() + 50
                    } else {
                        frame.setSize(
                            frame.getWidth(),
                            frame.getHeight() - 50
                    addf = !addf;
        up.add(button, BorderLayout.CENTER);
        JPanel bp = new JPanel();
        bp.setBackground(Color.red);
        bp.setPreferredSize(new Dimension(100, 200));
        panel.add(up, BorderLayout.CENTER);
        panel.add(bp, BorderLayout.SOUTH);
        frame.setContentPane(panel);
        SwingUtilities.invokeLater(
            new Runnable() {
                public void run()
                    frame.setVisible(true);
    public static void main(String[] args)
        new Test();
}Why the frame flickers when the button is pressed ?
I see that JFrame is filled with it's background color.
I know that "sun.awt.noerasebackground" property can be changed to fix that, but I don't want to change any global settings as the effect applies only to one frame.
Thank you.

Research "Double Buffering". This will not be a one liner but it will solve your problem.

Similar Messages

  • Programmatically Resizing 'Decimate Array'

    I am using Labview 6i. Is it possible to programmatically resize the
    'Decimate Array' function? I've got a bunch of data to display in a graph
    and want no more than say 1000 points displayed. I want to only look at
    every other point if I have more than 1000 points and every third point if I
    have more than 2000 points, etc... Is there an easy way to accomplish this?
    Thanks,
    Peter

    You an find a vi in the palette Analyze/Signal processing/timedomain.
    This one does what you want and is called ! decimate array.vi! The function you seem to recall is called decimate 1D array.
    An easier way to find this particular one is to use FIND in the palette. Click on the magnifier glass and type dec and you find both the function and the vi.
    greetings from the Netherlands

  • Disabling Maximize option from JFrame without disabling the resizing option

    Hi all,
    I want to know how we can disable the maximize option of a JFrame window without disabling the resizing option. I have tried JFrame.resizable(false). It disables both maximizing and resizable option. But I need the resizable option to be working.
    Anyone can help me?
    Thanks in Advance

    this works OK on 1.5.0_05 (unsure about other versions)
    and you'll lose the minimize as well
    import javax.swing.*;
    import java.awt.*;
    class Testing
      public void buildGUI()
        JFrame f = new JFrame("JFrame");
        f.setUndecorated(true);
        f.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
        f.setSize(400,300);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
      public static void main(String[] args)
        SwingUtilities.invokeLater(new Runnable(){
          public void run(){
            new Testing().buildGUI();
    }

  • Is it possible to stop decorated JFrame/JDialog flickering while resizing

    Hi,
    In my application I use decorated JFrames and JDialogs. While resizing awkward flickering occurs, especially under Windows (under Linux is a little bit better).
    I've tried what I found:
    System.setProperty("sun.awt.noerasebackground", "true");
    Toolkit.getDefaultToolkit().setDynamicLayout(true);
    but nothing goes better.
    Any help is appriciated.
    Thanks in advance

    dont think so that is why i dont use it i was runing java 1.5 on 3.00gh with a very good graphics card and 512mb ram and still so i dont think there is a way.... but if there is some one will find it but it sure wont be me.

  • JFrame Not Painting Until Resize

    I have a couple of components in my JFrame. At first, it looks like the JFrame's not painting. I tried calling repaint() before and after I made the JFrame visible. I also moved the JFrame, which should repaint it. However, if I try to resize the JFrame or just click while the cursor is in "resize mode," everything draws and acts fine. Unfortunately, I want to make the JFrame non-resizable. What's the problem?

    try calling the validate method before repainting

  • How to programmatically resize vi for different screen resolutions ?

    Hi,
         I have been struggling with this,How can i built a vi that should run for different screen resolutions (on different pc's).
    There are several threads on forum , But i dont find the exact solution for it.
    I have already tried with VI properties in the VI but i dont think its working.
    Can someone please help me ?
    Thanks and regards,
    Shivkant Paswan

    Yup it is a pain to use panes, but that is the easiest method I've found which meets my requirements.  Learn to use panes and splitters effectively, and then learn their limitations and workaround when you find those limitations.  In general LabVIEW should handle multi-resolutions, and resizing better but these are the tools we have today.
    Unofficial Forum Rules and Guidelines - Hooovahh - LabVIEW Overlord
    If 10 out of 10 experts in any field say something is bad, you should probably take their opinion seriously.

  • Programmatically resizing Front Panel does not repaint

    I am trying to extend the width of a front panel at run time when a user hits a button. The problem is
    that the contents of the newly exposed front panel section are not redrawn. I have to either drag
    another window over that section or minimize-maximize it to get it to repaint.
    My issue is how can I get the front panel to refresh/repaint, or if that is not possible what other
    ways are there to do resizing. I have tried changing the "Window Bounds" and "Panel Bounds" properties of
    the VI (not at the same time) as well as using LVWUtil32 (Windows API Function Utilities (32-bit) for
    LabVIEW). Each of these methods results in the same problem.
    Thanks for the help!
    Naveen

    HI Naveen,
    It seems to me that you are having Problem with your Graphics Hardware, rather than labview. Generally the window should be repainted by system.
    If you go to Display Properties -> Settings -> Advanced -> Troubleshooting or Graphic Acceleration - You may see the slider to full Acceleration. Under this setting the OS basically relegates most graphic duties to Graphic Hardware.
    What you may want to do is to Move the slider a couple of notches back. And then try your VI's. Infact try all the settings on your Slider. See if it works at No Acceleration.
    The Problem for me is I do not Have this Problem and cannot even simulate it to try a solution.
    Another Solution is this -
    There is a vi Property Class called "Front Panel" Which can be access
    ed from Vi Server. Under this Class you will see a Property "Defer Front Panel Update" Generally this Property is used to Prevent a redraw. But if this Property is set to False, Labview immediately redraws the Panel.
    What you may want to do is after resizing to let you program go through this Property Node set to False and See if this Helps.
    By The way, the way you have to wire this Property is to Open a Vi Ref, Wire it to Property Node, Select Property as "Front Panel". Right Click on "Front Panel" and Click on Create-> Property -> Defer Front Panel Update. Wire the Referneces of the these Objects, with the output of "Front Panel" going to "Defer FP Update".
    I hope your ver of Labview has these Properties. I am Using 6.1
    I hope you can take it from there!!
    Please advise How you solved the Problem. Good Luck!!
    Mache
    Another Solution which you can try is this
    Good Luck!
    Mache

  • Programmatically resize desktop

    I have an anoying problem. My graphincs card is $#!£. Everythime I restart my work laptop I have to reset the screen resolution settings.
    Does anyone know:
    1) How to Set the display resolution of Windows  thriugh LabVIEW?
    2) How to select the output device (Monitor, laptop, Laptop & monitor)?
    3) How to detect the output device?
    I need a better machine, I can't have one, so I need to create a VI to make my life easier.
    Cheers
    James

    Hi James W,
    Thank you for contacting National Instruments. Regarding your query, I think you're going to need to look at the Windows API for most of the details on how to do what you want to do. So far, I have found the following details:
    1. There is a registry Key that stores the screen resolution, it is located here
    HKEY_CURRENT_CONFIG>>System>>CurrentControlSet>>Control>>Video and then drill down from here (I think the large hex value changes between machines).
    I can't recommend changing your registry without first backing everything up. You can use the registry VIs within LabVIEW to make this change. 
    2. I am not sure, I think you will probably need to call some part of the Windows API. I would recommend having a look at MSDN to track this one down. 
    3. Again, I am not sure. Once you have found the corresponding DLL/Registry Key, you will be able to call them and access them through LabVIEW.
    I hope this helps,
    Many thanks,
    Message Edited by Andrew McL on 04-19-2010 05:06 AM
    Andrew McLennan
    Applications Engineer
    National Instruments

  • JFrame resize flicker using look&feel

    I just discovered JFrame's setDefaultLookAndFeelDecorated. Of course, it works great (refreshing to see it on vanilla windows!), but the frame flickers during mouse resizing. Every pixel the mouse moves, the frame is drawn gray then the previous frame's image is drawn over. Is there any way to stop this?
    And is there any way to do dynamic resizing? i.e. The window continuously revalidates&redraws itself while the user is resizing, and not just when the user stops resizing. At the least, is there a way to dynamically resize the border? Otherwise, a gray box (and the flicker) are the only things that shows until the user stops resizing.
    Specs: WinMe/JDK1.4.

    Run the following complete program and you should get the flicker problem when resizing it. Them dukes be still available! Tell me if you have any idea how to correct it, or how to dynamically resize the "guts" of the window. Thanks.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class JTestFrame extends JFrame {
         public static void main(String args[]) {
              new JTestFrame().show();
         private JMenuBar menuBar = new JMenuBar();
         public JTestFrame() {
              super("Java is cool");
              // Get the pane to add components to
              Container content = getContentPane();
              // Resize the frame
              Toolkit kit = getToolkit();
              Dimension wndSz = kit.getScreenSize();
              setBounds(wndSz.width/4, wndSz.height/4, wndSz.width/2, wndSz.height/2);
              // Make the frame use its look&feel for its border
              setUndecorated(true);
              getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
              // Make a menu for no other purpose than making a menu
              JMenu fileMenu = new JMenu("File");
              fileMenu.setMnemonic(KeyEvent.VK_F);
              JMenuItem fileExitMenuItem = new JMenuItem("Exit");
              fileExitMenuItem.setMnemonic(KeyEvent.VK_X);
              fileExitMenuItem.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e) {
                        System.exit(0);
              fileMenu.add(fileExitMenuItem);
              menuBar.add(fileMenu);
              JTextArea text = new JTextArea();
              getContentPane().add(new JScrollPane(text), BorderLayout.CENTER);
              setJMenuBar(menuBar);
              // Set the default close operation to exit the application
              setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

  • Any way to get at the title bar in JFrame?

    I'd like to either add a button to the title bar (title pane?) or override the behavior of the resize button. As far as I can tell, the LookNFeel classes don't allow you to add anything, just change colors, icons, etc. The WindowListener classes don't let you get at the resize button. I don't want to override Component.setSize() (that causes other problems), I just want to have the resize button do something different (or add another button).
    I'd appreciate any ideas!

    1) You could create your own frame that extends JWindow. This would be quite involved, since you'd need to make it respond to mouse drags, mouse resizing etc explicitly (if you wanted that functionality), but you would be able to add your own custom border, which you could make to look like a system border, or you could make comletely different.
    2) I don't know this could be persuaded to do what you want, but you could try adding a ComponentListener to the JFrame to check for resizes, and take action accordingly

  • Setting the image size within a JFrame!

    This is the code i have implemented
    import javax.swing.*;
    import java.awt.*;
    class SampleJFrame extends JFrame
         JLabel l1;
         ImageIcon image;
         SampleJFrame()
              image=new ImageIcon("G:/java files1/images/aa.jpg");
              l1=new JLabel("DEDICATION",image,JLabel.RIGHT);
              l1.setSize(100,100);
              //this.setLayout(new FlowLayout(FlowLayout.RIGHT));
              this.add(l1);
    public class JFrame2
         public static void main(String args[])
              SampleJFrame samplejframe=new SampleJFrame();
              samplejframe.setTitle("FRAMES WITH JLABELS WITH IMAGEICON");
              samplejframe.setSize(400,400);
              samplejframe.setVisible(true);
    }When i run this the image occupies the entire JFrame.I want to resize it to a width of around 100,80 pixels.How can i do it?
    I know when i draw a image without linking to a label i can use drawImage of Graphics class to set the size of the image.But in this case i am linking the image to the JLabel and it's occupying the entire screen and the text with the JLabel is thrown somewhere out.

    Read it.Got a lot of info from that.But the trouble with mine is that i am linking the image to the JLabel and thats why i am unable to set its size because the image will be added to the JPanel when i add the JLabel to it?If i was directly printing the image i would have rather used Graphics.drawImage( ) where i can manually specify the width.

  • Strange JFRAME behaviour under Jdk 1.5

    I have the following extracted code :
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.geom.*;
    public class DrawPie extends JFrame{
         private HashMap ColourMap=new HashMap();;
         private HashMap DataMap=new HashMap();
         private double Total_Val=0;
         private int startAngle=0;
         private int arcAngle=0;
         public DrawPie(HashMap DataMap){
         super ("Pie Chart Analysis");
              this.DataMap=DataMap;
         //     System.out.println("DATAMap-->"+DataMap);
         // getContentPane().setBackground(Color.white);
              setSize(500,500);
              setVisible(true);
         public void paint(Graphics g){
              super.paint (g);
              Graphics2D g2 = (Graphics2D) g;
              DrawPieChart(g);
    This programme work fine under jdk1.4.2 or below at the time when the Jframe or windows is resized or icon minimised or maximised.
    But when it was compiled using latest jdk1.5 , unexpected strange result happen , the pie chart draw using java 2d
    g2.fill(new Arc2D.Double(30, 30, 200,200,startAngle,arcAngle
    , Arc2D.PIE));
    wil behaved strangely, the moment the JFrame or windows is resize or dragged.
    The pie chart will disappear if the windows is resized , However if the window is minimised and restore back to normal size , pie chart will reappear and subsequently lost completely if the windows is dragged resulted in the size changes.
    I was puzzled by this strange swing behaviour , any resized or windows minimise or maximise would not result in the lost of pie chart as long as the jdk is not 1.5 ! Was it due to swing fundamental changes incorporated in the latest release?
    Any suggestion?
    Thank

    ok I see what you have done,
    now a few tips in drawing something on your frame:
    never override the paint method of your main Frame (like you did)
    to draw something on it you simply override the contentPane's paint method
    and to make your own contentPane you simply make one by making a new Class
    that extends say JPanel and assign it as a contentPane of your Frame(like I did)
    And in this Paint method you can draw whatever you like it will be properly uptated!!!
    All the best keep up the good work!
    ps. try the code bellow.
    import javax.swing.*;
    public class DrawPie1 extends JFrame {
    private double Total_Val=0;
    private int startAngle=0;
    private int arcAngle=0;
    private MyMainPanel mainPan;
    public DrawPie1()
    super ("Pie Chart Analysis");
    mainPan = new MyMainPanel();
    setContentPane(mainPan);
    //public void paint(Graphics g) //Do not override the paint method of your main frame!!
    // super.paint (g);
    // Graphics2D g2 = (Graphics2D) g;
    // g2.setPaint(Color.red);
    // g2.fill(new Arc2D.Double(30, 30, 200,200,0,78
    // , Arc2D.PIE));
    public static void main(String args[])
    DrawPie1 pie=new DrawPie1();
    pie.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pie.setSize(400,300);
    pie.setVisible(true);
    //**** Second Class
    import javax.swing.*;
    import java.awt.*;
    import java.awt.geom.Arc2D;
    public class MyMainPanel extends JPanel
    public MyMainPanel()
    public void paint(Graphics g)
    super.paint (g);
    Graphics2D g2 = (Graphics2D) g;
    g2.setPaint(Color.red);
    g2.fill(new Arc2D.Double(30, 30, 200,200,0,78
    , Arc2D.PIE));
    }

  • Resizing RichMedia Annotation

    We embedded a flash application in a PDF document using the new flash embedding tool available in Acrobat 9. We also have some JavaScript in the document to programmatically resize the RichMedia annotation when reader opens the document. But it seems the AnnotRichMedia.rect property is read only although it says R/W in the Spec. The following code is what we tried to resize the annotation:
    annot = getAnnotRichMedia(0, "RMTest");
    annot.rect[0] += 50;

    How do I quit getting emails from this forum. My issue is resolved
    Date: Tue, 30 Mar 2010 10:36:04 -0600
    From: [email protected]
    To: [email protected]
    Subject: Re: Flex4 compiled Flash content doesn't run in Adobe Reader
    Alexander,
    Linux AR9.3.1 uses flashplayer 9 and so is unable to cope with SWFs
    produced by FLEX4 which is aimed at flashplayer 10. So what you're
    seeing is a simple incompatibility and for the time being you'll have
    to stick with FLEX3.
    James
    >

  • Resize a control (i.e. String Control)

    LV5.0
    NT 4.0
    Hello,
    I would just like to know if there is a way to:
    1) programmatically resize a control inside my VI program.
    2) get the width & height of my VI panel.
    Right now, im making my panel run into full screen size (size of desktop)
    whenever it starts up, which in turns I want my one control to adjust also
    it's position and size.
    You don't have to answer both question so if you only know one, I would
    greatly appreciate it.
    TIA
    John

    Click here for Free Video!!
    http://www.gohip.com/freevideo/
    John Cruz wrote in message
    news:[email protected]..
    > LV5.0
    > NT 4.0
    >
    > Hello,
    >
    > I would just like to know if there is a way to:
    >
    > 1) programmatically resize a control inside my VI program.
    You have the answered youreself
    >
    > 2) get the width & height of my VI panel.
    Use vi refference and and Property node, select Fron Panel Window>Panel
    Bounds
    >
    > Right now, im making my panel run into full screen size (size of desktop)
    > whenever it starts up, which in turns I want my one control to adjust also
    > it's position and size.
    >
    > You don't have to answer both question so if you only know one, I would
    > greatly appreciate i
    t.
    >
    > TIA
    > John
    >
    >
    >
    Esben

  • Panel Resize - how to tell when mouse released?

    I need to programmatically resize all my controls when the panel is resized.  I can't use the automatic option, as I have some specific things to do, e.g. some controls stay at fixed size, others resize.
    I've used an event structure and am doing the resizing on the "Panel Resize" event.  The problem is that this takes a little time, mainly as one of the controls is an Adobe Reader ActiveX control, which takes a little time to rescale and redraw.
    The result is that the VI seems to queue up the events as the panel is resized - if you grab the corner of the window and move it around, the VI then sits and ploughs through all the events that were created as it was resized.
    What I'm really after is an event that fires after the panel has been resized and the mouse has been released.  Is there a way of achieving this?
    Thanks

    There's no event for this, but you can filter your events, for example, like this:
    There are other variations on this (setting a flag, using a case structure in the original event case, etc.).
    Try to take over the world!
    Attachments:
    Example_BD.png ‏2 KB

Maybe you are looking for

  • Features wish list

    I can't find any other place on Apple's web site to provide feedback, so I'm creating this discussion topic. There are 2 features I'd really like to see Apple add to the next iteration of the iPod software: a way to put a bookmark in a playlist, and

  • Failure of some component

    Error message: Error on switching in renew: NS ERROR UNEXPECTED. Component returned failure code: 0x8000ffff NSERROR-UNEXPECTED [ns]PrefBranch.getCharPref] Id: none Everytime I try to open Firefox this message appears and is removed only upon a reboo

  • Moving Public from G4 XServe to new Intel XServe and preserve permissions

    We have purchased a new Intel XServe (10.4.8) and need to move the Public folder and Users folder from our old XServe G4 (10.3.9) to the new one, and would like to preserve the permissions. Both of these are OD replicas; there is an XServe G5 (10.3.9

  • Human tasks on working days

    How can I do to make Human task's expiration days count only working days?

  • Possibility for HR-advisor to overrule an approval in worklist

    Current Enivorment is PeopleSoft HRMS 8.8 Application Release : HRMS 8.80.01.000 Peopletools release : 8.44.06 Description of the problem Business Process : Adhoc Salary Change Navigation -> Workforce Administration --> Self service transaction --> A