Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Resize JFrame without flickering

Posted on 2006-05-22
12
Medium Priority
?
870 Views
Last Modified: 2013-11-23
When i try to resize a simple JFrame with the setSize() method the frame starts flickering but if i resize the frame with the the mouse it works.
Can you tell me a method to resize the frame without flickering?


Simple Code example:


import java.awt.*;
import javax.swing.*;

public class TestFrame extends JFrame  {

    JPanel panel = new JPanel();
    JButton button = new JButton("Resize");

    public TestFrame() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(200,200);
        setLayout(new GridLayout());

        panel.setBackground(Color.red);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setSize(getSize().width + 10, getSize().height + 10);
            }
        });

        this.add(button);
        this.add(panel);

    }

    public static void main(String[] args) {
        TestFrame frame = new TestFrame();
        frame.setVisible(true);
    }
}
0
Comment
Question by:gfilia2
10 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 16735018
That's because when you use the mouse, you do it incrementally but in setSize (), you do it directly to the new size.
0
 

Author Comment

by:gfilia2
ID: 16735074
Can you tell me a solution to change the frame size without mouse and flickering?
0
 
LVL 8

Expert Comment

by:colr__
ID: 16735362
Instead of using +10, use +1.

Its because yor moving in blocks of ten that your seeing a flicker.

colr__
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:gfilia2
ID: 16735487
I can see the flickering with +1 too.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16739972
That's because when you do it from code, it is much faster than you doing it from the mouse manually (so in the second case, you don't see the flicker). I can't think of a solution to this other than you moving your hand as fast as the computer processes the instruction ;-) then you will see the flicker in each case actually.
0
 
LVL 1

Expert Comment

by:rongasa
ID: 16740395
I am not sure but i would try to set the frame size from another thread and with small incrementing steps (as mentioned above) if i were you. I had a similar problem with Jlist flickering and it was solved by introducing a thread that was doing the resize/add-remove list element. I know it sounds stupid but it worked for my case. I had only thought of it because flickering appeared only is some PC's of my customer, it never appeared in my computer or in any Solaris machine. I think it has something to do with processing power, event handling etc...
I hope i helped.
0
 

Author Comment

by:gfilia2
ID: 16741055
Thanks for your suggestion, I'll try it.
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 1000 total points
ID: 16748542
>> I think it has something to do with processing power,

That is mainly what it has to do with. The faster the CPU processes the instruction, the more it flickers. The slower it is, the smoother it appears (like you did it from hand).

>> i would try to set the frame size from another thread

Not needed - the GUI event-handling thread (called as the EDT or the Event Dispatch Thread) is anyway a separate thread. So the actionPerformed () method is called in a separate thread.
0
 

Author Comment

by:gfilia2
ID: 16790824
I've tryed with a separate thread but again without success!
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16796067
I told you it won't work. The only way to do it is to make your hand as fast as the CPU (to replicate the problem with your hand) or to make your CPU as slow as your hand (to solve it ;-))
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month12 days, 8 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question