JFrame resizing

Hi,
I have a JFrame, it displays on the right upper corner the icons minimize, maximize and exit.
Minimize and Exit icons are working fine.
I have the problem with the maximize button.
The application I am writing allows to resize the JFrame. Thats fine!
If I run the application, it has an initial size w=800, h=600
If I click the maximize button, the JFrame fits to the window maximal size.
But if I click the maximize button again, I want to have the initial size. This does not work.
It keeps the maximal size.
How can I switch
- between maximal size and "initial" size
or
- between maximal size and last set size (so the size before Jframe was maximized)
if I click the maximize button.
Thanks
A.

Hi,
I found the issue and fixed it.
The issue is the following code.
If I run the application, I call this function, it works fine.
What it does is, it just horizontal center the window.
  public static void centerWindow(Window window) {
      Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
      int x = (int)((screen.width - window.getSize().width) / 2);
      int y = 0;
      window.setLocation(x, y);
  }If I run this function later on again after resizing the window, it causes the issue.
So if I resize the window, I don't call this function anymore.
Thanks
A.

Similar Messages

  • How to resize the components in a JFrame when the JFrame Resizes?

    Hi all,
    i have some problems with my app. I have set the JFrame resizable and that works fine for the JFrame but not for the components in the JFrame.
    So my question is how can i set the components in a JFrame to resize automatically when the JFrame resizes?
    Here are the important things from my code,...if you need more, just let me know, its my first post in a forum .
    Its a JFrame with a JTabbedPane and on the Tabs are Panels with buttons and Tables.
    Thank you in advance!
    public class MainMonitor extends JFrame {
    public MainMonitor() {
              super();
              initialize();
              setVisible(true);
         private void initialize() {
              this.setSize(1145, 785);
              this.setIconImage(Toolkit.getDefaultToolkit().getImage("Images/c.gif"));
              this.setContentPane(getJContentPane());
              this.setTitle("Company Manager");
              this.setResizable(true);
              this.setLocationRelativeTo(null);
         private JPanel getJContentPane() {
              if (jContentPane == null) {
                   jContentPane = new JPanel();
                   jContentPane.setLayout(null);
                   jContentPane.setOpaque(true);
                   jContentPane.add(getJTabbedPane(), null);
                   jContentPane.add(getJPanel11(), null);
              return jContentPane;
         private JTabbedPane getJTabbedPane() {
              if (jTabbedPane == null) {
                   jTabbedPane = new JTabbedPane();
                   jTabbedPane.setLocation(new Point(6, 69));
                   jTabbedPane.setSize(new Dimension(1120, 684));
                   jTabbedPane.addTab("P", null, getJPanel(), null);
                   jTabbedPane.addTab("K", null, getJPanel1(), null);
                   jTabbedPane.addTab("B", null, getJPanel2(), null);
              return jTabbedPane;
         

    Giasaste wrote:
    Thanks a lot, i didnt knew that the Layout Manager is a must.It's not a must, but it is the way to allow your app to be resized nicely and makes it much easier to maintain and enhance your program. Imagine creating a complex gui then later decide that you want another radiobutton and to remove a jlabel / jtextfield pair. with layout managers a chore becomes easy.

  • Implementing JFrame Resizing Rules?

    Recently I've needed to enforce rules on a JFrame's dimensions (for example: preserving an aspect ratio).
    The obvious solution is to call setSize() from ComponentListener's componentResize() method. This is functional, but looking at the Java source, it's pretty inelegant. It actually starts a setSize() -> componentResized() -> setSize() loop that only terminates when setSize() is called with dimensions matching the current dimensions (see Component.reshape()). Also, its visually ugly because when maximized you can see the window snap back to the "fixed" size.
    It would seem that a more elegant way to add this functionality would be to have a method like "public Dimension checkDimensions(Dimension)" available for overriding.
    Does something like this already exist that I've missed?
    I've tried following the source, but I can't find the place where JFrame resizing is handled.

    The optimal solution would be to use a Look&Feel which doesn't have to use the OS window decorations, so the resize events are only generated in Java (not optimal in the fact that you don't get to have the OS frame decorations of course). Then you should be able to add your own RootPaneUI which simple doesn't allow resizing beyond a maximum size or auto resizing respecting an aspect ratio.
    See JFrame#isDefaultLookAndFeelDecorated and (for example) MetalRootPaneUI#MouseInputHandler. Perhaps there are thrid party look and feel where you can more easily replace a similar handler.

  • JFrame resize dynamically in response to content pane size changes

    Hi -
    I have a JFrame that uses CardLayout. The CardLayout panel content is of varied dimensions. it means the JComponents added to CardPanel will change size dynamically.
    Is it possible to have the JFrame resize dynamically in response to the content of the CardLayout panel changing?

    camickr wrote:
    No, the preferred size of a panel using a CardLayout is the preferred size of the largest component added to it.
    As a user I think it would be terrible to have the frame resize every time a turn a page to go to the next card. I have never seen this any any application.
    If you really need this functionality, then manage the panels yourself. Remove the existing Component before adding the next component and then do a frame.pack().My Application will be a JFrame.
    inside this JFrame i want to have three JPanels. according to some events and logic my GUI should switch to a given JPanel from those three panels. as i said the Jpanels sizes will change size on runtime depending on a automatic swing JPanel generation and i will have to add the new generated JPanel to parent JFrame with its new size
    any Ideas how to achive this ?

  • During the JFrame resize, unable to call mylogic.How to know JFrame rezing?

    {font:Arial}{size:10pt}Hi
    In my project i came across an issue. In that I had a JFrame (with FlowLayout) added with 24 JButtons (Its appearence will be similar as JToolbar). On this JFrame i called pack(). When i try to resize the JFrame, its componentResize() listener will not be called every time, instead it is calling only when its resizing complete.
    I am looking for a solution, that will rearrange JButtons (as specified Column/Row wise) during the JFrame resize. Can any body help me why componentResize() is not calling, during the resize. Tell me all alternate solutions. Pls help me.
    Waiting for your reply
    {size}{font}

    Can any body help me why componentResize() is not calling, during the resizeBecause its not very efficient to layout the frame every time you move the mouse 1 pixel. So by default this behaviour is turned off. I think you can turn in on by adding the following at the start of your program:
    Toolkit.getDefaultToolkit().setDynamicLayout(true);

  • Resizing Image when JFrame resized?

    I have a JFrame with a JPanel added to the contentPane (BorderLayout.CENTER). In the JPanel is a JLabel (BorderLayout.CENTER) with an ImageIcon set to it. The image is a subimage of a larger image whose width is scaled to fit the current JLabel width (which is dictated by the JPanel width and finally dictated by the JFrame width).
    When the user resizes the JFrame, the image of the ImageIcon should be replaced with a new image which has been scaled to fit the new width and that reflects the new portion of the larger source image that is visible.
    The JFrame already has a ComponentListener added for retaining the current JFrame size and screen position, but calling my resizeDisplay() method from componentResized() does not seem to have the updated JLabel size. I have added a ComponentListener to the JLabel and called my resizeDisplay() method from componentResized() but it does not seem to have the updated JLabel size either.
    How can my application properly react to JFrame resizes so that the method is retrieving the new size values?
    Thanks,
    Robert Templeton

    Its always easier to solve a problem when sample code (not a complete program) is posted with the question.
    Maybe I don't understand the question, but when I run the following code the output reflects the size of the label after it has been resized. I'm using JDK1.3 on Window 98.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.text.*;
    public class TestLabel extends JFrame
         public TestLabel()
              JPanel panel = new JPanel();
              setContentPane( panel );
              panel.setPreferredSize( new Dimension( 200, 200 ) );
              panel.setLayout(new BorderLayout() );
              final JLabel myLabel = new JLabel( new ImageIcon("dukeWaveRed.gif"), JLabel.CENTER );
              panel.add( myLabel );
              myLabel.addComponentListener( new ComponentAdapter()
                   public void componentResized(ComponentEvent e)
                        System.out.println( myLabel.getSize() );
         public static void main(String[] args)
              TestLabel frame = new TestLabel();
              frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
              frame.pack();
              frame.setVisible(true);
    }

  • Problem with JFrame resizing at runtime

    Hi All,
    I have a problem with resizing the JFrame at runtime. Even if i resize the JPanel. It is not affection on all the components over the frame. How can i solve that I am using AbsoluteLayout(jre1.6) and here i am going to provide you code snippet. My requirement is to resizt the form. If form is small in size then all the component should also be according tothe size of the form.
    public void initComponents(int size) {
    if(size >= 50) {
    x_co_or = 300;
    y_co_or = 300;
    } else if(size < 50) {
    x_co_or = 200;
    y_co_or = 200;
    jPanel1 = new javax.swing.JPanel();
    jButton1 = new javax.swing.JButton();
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
    jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
    jButton1.setText("jButton1");
    jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 40, 110, 40));
    getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, x_co_or, y_co_or));
    pack();
    Can anybody suggest any idea/solution.
    Any idea/solution are most welcome.
    Regards,
    Pradeep Dubey

    How can i solve that I am using AbsoluteLayout(jre1.6)
    and here i am going to provide you code snippet.no need for the snippet, you're using absolute positioning, which means you're responsible for location and size on every single change.
    start coding now, take about a week, or you could use a layout manager, take maybe, 5 minutes

  • JFrame resizing question

    When i resize a JFrame the components it hold remain unchanged until I stop resizing the frame. Then the components conform to the frame size. Is there a way to have the components resize dynamically?

    Thank for the answer, that's exactly what I nedded!

  • JFrame resizing problem......

    Hi All,
    can anyone guide me how to stop resizing or dragging the JFrame.
    my frame should not be movable or resizable by the user.
    Urgent!!
    Regards
    sd

    Have you tried frameObj.setResizable(false)?
    Phani

  • JFrame resizing setUndecorated(true)

    I have a JFrame that it's title bar has several Images.
    In Order to set these images I used setUndecorated(true)
    and added the images with panels to the north area of the panel.
    After this change my Frame has a fixed size.
    I implementes MouseListener and MouseMotionListener handler to deal with drugging of the JFrame -how can I implement the resize of the frame?
    thanks
    liat

    Thats why a frame has a title bar, so the mouse can click on a component and drag it somewhere. If you remove the title bar then you need to add the dragging logic to some other component. Check out this thread for a simple example:
    http://forum.java.sun.com/thread.jsp?forum=57&thread=406114

  • Netbeans wont allow JFrame resize

    Hi Guys,
    I have just started using Netbeans 6.1 and am new to the netbeans environment for Java. I have been writing a simple GUI and it has been working fine until now. The GUI displays a JFrame which consists of a few panels, etc and some painted components. All was fine until today when I modified one of the panels indivigually when I went back to the main JFrame it had been reduced to a really small size as in a size of 0. Any attempts to rezise the JFrame or run the program results in the Netbeans error:
    java.lang.IllegalArgumentException: Invalid size
         at org.jdesktop.layout.GroupLayout.checkResizeType(GroupLayout.java:223)
         at org.jdesktop.layout.GroupLayout.checkSize(GroupLayout.java:208)
         at org.jdesktop.layout.GroupLayout.access$500(GroupLayout.java:85)
         at org.jdesktop.layout.GroupLayout$GapSpring.<init>(GroupLayout.java:2851)
         at org.jdesktop.layout.GroupLayout$SequentialGroup.add(GroupLayout.java:1588)
         at org.netbeans.modules.form.layoutdesign.support.SwingLayoutBuilder.fillGroup(SwingLayoutBuilder.java:317)
         at org.netbeans.modules.form.layoutdesign.support.SwingLayoutBuilder.composeGroup(SwingLayoutBuilder.java:232)
         at org.netbeans.modules.form.layoutdesign.support.SwingLayoutBuilder.fillGroup(SwingLayoutBuilder.java:253)
         at org.netbeans.modules.form.layoutdesign.support.SwingLayoutBuilder.composeGroup(SwingLayoutBuilder.java:232)
         at org.netbeans.modules.form.layoutdesign.support.SwingLayoutBuilder.fillGroup(SwingLayoutBuilder.java:251)
         at org.netbeans.modules.form.layoutdesign.support.SwingLayoutBuilder.composeGroup(SwingLayoutBuilder.java:232)
         at org.netbeans.modules.form.layoutdesign.support.SwingLayoutBuilder.fillGroup(SwingLayoutBuilder.java:253)
         at org.netbeans.modules.form.layoutdesign.support.SwingLayoutBuilder.composeGroup(SwingLayoutBuilder.java:232)
         at org.netbeans.modules.form.layoutdesign.support.SwingLayoutBuilder.createLayout(SwingLayoutBuilder.java:174)
         at org.netbeans.modules.form.layoutdesign.support.SwingLayoutBuilder.setupContainerLayout(SwingLayoutBuilder.java:107)
         at org.netbeans.modules.form.VisualReplicator.setupContainerLayout(VisualReplicator.java:791)
         at org.netbeans.modules.form.VisualReplicator.updateContainerLayout(VisualReplicator.java:324)
         at org.netbeans.modules.form.FormDesigner$FormListener.run(FormDesigner.java:2241)
         at org.netbeans.modules.form.FormDesigner$FormListener.processEvents(FormDesigner.java:2186)
         at org.netbeans.modules.form.FormDesigner$FormListener.formChanged(FormDesigner.java:2145)
         at org.netbeans.modules.form.FormModel.fireEvents(FormModel.java:1268)
         at org.netbeans.modules.form.FormModel.fireEventBatch(FormModel.java:1241)
         at org.netbeans.modules.form.FormModel.firePendingEvents(FormModel.java:1204)
         at org.netbeans.modules.form.FormModel.access$000(FormModel.java:62)
    [catch] at org.netbeans.modules.form.FormModel$2.run(FormModel.java:1184)
         at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
         at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
         at org.netbeans.core.TimableEventQueue.dispatchEvent(TimableEventQueue.java:104)
         at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
         at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
         at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
         at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
         at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
         at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
    This is probably a simple thing I have done wrong here but none of the properties seem to allow me to resize?
    any assistance would be much appreciated.
    cheers
    Tony

    Thanks mate, I reverterd to an older history when it was working and did the clean build seemed to fix the issue.
    Cheers

  • JFrame + Resize

    hy guys!
    can anybody tell me how to make a JFrame fixed size,
    so that the window appears for example in 300x300 and
    cannot be resized any longer.
    thx
    rkwitt

    Now that's easy.
    i) you read the manual like the rest of us.
    ii) you discover from (i) that JFrame inherits from Frame and Frame has a method call setResizable(boolean).

  • Continuous layout on JFrame resize?

    hi,
    on my machine (win2k) when you resize a JFrame with the mouse, the contents are not resized until you have released the drag. Is this a property of my java/OS settings or a limitation of Swing?
    JSplitPane has a setContinuousLayout method which sounds like it does something similar?
    thanks,
    asjf

    Component and Window listeners are not going to work since the window resize is not happening on the AWT thread.. So, you do need your own thread to monitor the size of the window, and invalidate/validate your window if the size changes. The following should work for you, coded as a method in a utility class so it can be easily reused by any JFrame:
         private static Map continuousLayoutWindows;
         * Sets the specified frame to do a continuous layout, so that if the user resizes the frame,
         * the components within the frame are resized as well.
         * @param frame JFrame
         public static void setContinuousLayout(final JFrame frame) {
              new Timer(100, new ActionListener() {
                   public void actionPerformed(ActionEvent e) {
                        Dimension currentSize = frame.getSize();
                        if (continuousLayoutWindows == null) {
                             continuousLayoutWindows = new HashMap();
                        Dimension windowSize = (Dimension)continuousLayoutWindows.get(frame);
                        if (windowSize == null) {
                             windowSize = new Dimension();
                             continuousLayoutWindows.put(frame, windowSize);
                        if (!currentSize.equals(windowSize)) {
                             windowSize.width = currentSize.width;
                             windowSize.height = currentSize.height;
                             SwingUtilities.invokeLater(new Runnable() {
                                  public void run() {
                                       frame.invalidate();
                                       frame.validate();
                                       frame.repaint();
              }).start();

  • Undecorated jframe resize problem

    i created a custom jframe look by setting a jframe to undecorated and designing my own maximize/minimize/close buttons and other things to improve its appearence
    the problem is that when set to undercorated all the default resizing/move methods are gone
    i tried implementing my own resize methods, they seem to work but it flashes like crazy while resizing
    anyone have any idea how to stop all the flashing or a better way to implement this?
    the following is a small example of the problem i am having, i only implemented the north resize part so the code can be easier to read.
    thnx in advance
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.imageio.*;
    import java.io.*;
    import java.awt.image.BufferedImage;
    public class TestFrame extends JFrame implements MouseMotionListener, MouseListener
         Point sp;
         int     compStartHeight;
         int minHeight = 100;
         JPanel frameContent = new JPanel();
         public TestFrame()
              super("testing frame");
              setSize(600, 600);
              setContentPane(frameContent);
              frameContent.setBackground(Color.black);
              frameContent.setLayout(new BoxLayout(frameContent, BoxLayout.Y_AXIS));     
              setUndecorated(true);
              addMouseMotionListener(this);
              addMouseListener(this);
              JButton testButton = new JButton("TEST");
              JButton testButton2 = new JButton ("TEST2");
              frameContent.add(testButton);
              frameContent.add(Box.createVerticalGlue());
              frameContent.add(testButton2);
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              setVisible(true);
         public void mouseMoved(MouseEvent e)
              Point p = e.getPoint();
              if (p.y > e.getComponent().getSize().height - 5)
                   setCursor( Cursor.getPredefinedCursor( Cursor.N_RESIZE_CURSOR ));
              else
                   setCursor( Cursor.getPredefinedCursor( Cursor.DEFAULT_CURSOR));
         public void mouseDragged(MouseEvent e)
              Point p = e.getPoint();
              int compWidth = getSize().width;
              if (getCursor().getType() == Cursor.N_RESIZE_CURSOR)
                   int nextHeight = compStartHeight+p.y-sp.y;
                   if (nextHeight > minHeight)
                        setSize(compWidth,nextHeight);
                        validate();
              else
                   int x = getX()+p.x-sp.x;     
                   int y = getY()+p.y-sp.y;     
                   setLocation(x,y);
         public void mousePressed(MouseEvent e)
              sp = e.getPoint();
              compStartHeight = getSize().height;
         public void mouseEntered(MouseEvent e)
        public void mouseExited(MouseEvent e)
              if (sp == null)
                   setCursor( Cursor.getPredefinedCursor( Cursor.DEFAULT_CURSOR));
        public void mouseClicked(MouseEvent e)
        public void mouseReleased(MouseEvent e)
              sp = null;
         public static void main(String[] args)
         new TestFrame();
    }

    I doubt there is a faster / easier way to resize.
    Have you tried adding
    public boolean isDoubleBuffered()
      return true;
    }To over-ride Component.isDoubleBuffered. This should sort out your flickering problem.
    Bamkin

  • 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);
    }

Maybe you are looking for

  • Have the Adobe Acrobat XI Standard concurrent licensing (not per-seat license)?

    Question about Adobe Acrobat XI Standard corporate licensing. Have the concurrent license (not per-seat, like Floating licensing)?

  • Apple Remote desktop does not start

    Hello, I just bought Apple Remote desktop (3.7.1) on the App Store. When I launched it on my iMac (17", 3.4Ghz/8Gb/1Tb) under OSX 10.9.1 the menu of Apple Remote desktop appear but nothing else, no window. When I put my cursor in the menu the color w

  • Missing navigation toolbar

    I installed Ff for Android on my S3 tablet and inspite of my best efforts I cannot get the Nav bar to show. In fact none of the toolbars show. Are the toolbars supposed to appear on an Android tablet?

  • Using stylesheets right

    Hi guys, for a GUI i wanted to use a stylesheet to save code but i have some questions in how to use it. 1. Is it possible to make elements position flexible? So if i expand the scene the element will still be at xpos = 10% of Scene size or something

  • Auto-adjust references when adding rows/columns

    If I recall correctly, it was the behavior of Appleworks that if you added, say, column B, making the old column B into column C, all references to B throughout were automatically changed to C. This does not happen in Numbers. Is there any way to mak