10 Replies - 389 Views - Last Post: 14 February 2013 - 03:19 PM Rate Topic: -----

#1 polska03  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 290
  • Joined: 28-November 09

autoresize JFrame or JApplet

Posted 13 February 2013 - 03:39 PM

hello, I would like to auto re size my java application. Essentially I would like to add more buttons and have it so the application repaints itself and authorizes to fit all the components. My problem so far is my program adds more buttons but does not "refresh" or resize. I tried the pack() function on the frame which I thought would work but it didn't. I even tried after adding a button to a panel to add a piece of code such as this: setSize(getHeight()+100,getWidth()+100); but this did not work either.I do not care if this program is an applet or application, I simply would like to know how to auto re size the frames to fit content after it has been added to a panel ( would be great to know how for both an applet or application as I have searched the internet and could not find any resolution). Thanks


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

public class Test extends JApplet {
    
  JPanel panel1 = new JPanel();
  JPanel panel2 = new JPanel();
  JPanel panel3 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
  JPanel panel4 = new JPanel();
  
  JButton btn1=new JButton("BTN1");
  JButton btn2=new JButton("BTN2");
  JButton btn3=new JButton("BTN3");
  JButton btn4=new JButton("BTN4");
  JButton btn5=new JButton("BTN5");
  JButton btn6=new JButton("BTN6");
  JButton btn7=new JButton("BTN7");
  JButton btn8=new JButton("BTN8");
  JButton btn9=new JButton("BTN9");
  
  
  
  public Test() {
      
    panel1.add(btn1); 
      
    add(panel1, BorderLayout.WEST);
    add(panel2,BorderLayout.EAST);
    add(panel3, BorderLayout.SOUTH);
    add(panel4, BorderLayout.NORTH);
      
      
  btn1.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        
         
          panel2.add(btn2);
          panel2.add(btn3);
          panel2.add(btn4);
          panel2.add(btn5);
          panel2.add(btn6);
          panel2.add(btn7);
          panel2.add(btn8);
          panel2.add(btn9);
          
          repaint();
         
      }
    });
  
  
  }
  
  
  public static void main(String[] args){
        
       JFrame frame= new JFrame();
       Test applet=new Test();
       frame.add(applet,BorderLayout.CENTER);
       frame.setSize(300,300);
       frame.pack();
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setVisible(true);
       applet.init();
      
  }
  

  
}



Is This A Good Question/Topic? 0
  • +

Replies To: autoresize JFrame or JApplet

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2884
  • View blog
  • Posts: 9,567
  • Joined: 12-December 12

Re: autoresize JFrame or JApplet

Posted 13 February 2013 - 04:53 PM

Make frame a static member:

public static JFrame frame;

so that you can reference it in the event:

    panel2.add(btn8);
    panel2.add(btn9);
    frame.pack();

There may be a way to find frame through the object hierarchy, avoiding the need for the static reference, but I couldn't locate it using getParent() or getTopLevelAncestor().

This post has been edited by andrewsw: 13 February 2013 - 04:54 PM

Was This Post Helpful? 0
  • +
  • -

#3 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,423
  • Joined: 29-July 11

Re: autoresize JFrame or JApplet

Posted 13 February 2013 - 04:57 PM

All you need to do is make sure you use a layout manager and pack(). Swing will handle the rest.
Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,423
  • Joined: 29-July 11

Re: autoresize JFrame or JApplet

Posted 13 February 2013 - 05:04 PM

public class MainFrame extends JFrame {
	private Timer timer;
	private int count = 1;
	
	public MainFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new FlowLayout());
		add(new JButton("button " + count));
		pack();
		setLocationRelativeTo(null);
		timer = new Timer(3000, adder);
		timer.start();
	}
	
	private ActionListener adder = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			add(new JButton("Button " + String.valueOf(++count)));
			pack();
		}
	};
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				new MainFrame().setVisible(true);
			}
		});
	}
}//end awesomeness


This post has been edited by farrell2k: 13 February 2013 - 05:05 PM

Was This Post Helpful? 0
  • +
  • -

#5 polska03  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 290
  • Joined: 28-November 09

Re: autoresize JFrame or JApplet

Posted 13 February 2013 - 05:14 PM

Thanks for the replies, but neither of them worked for this code. I tried both of your suggestions (see code below) and wasn't able to get to resize. With the static Jframe object, I got an error saying this wan an illegal start for an expression.

package comp351assign1;

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

public class Test extends JApplet {
    
  JPanel panel1 = new JPanel();
  JPanel panel2 = new JPanel();
  JPanel panel3 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
  JPanel panel4 = new JPanel();
  
  JButton btn1=new JButton("BTN1");
  JButton btn2=new JButton("BTN2");
  JButton btn3=new JButton("BTN3");
  JButton btn4=new JButton("BTN4");
  JButton btn5=new JButton("BTN5");
  JButton btn6=new JButton("BTN6");
  JButton btn7=new JButton("BTN7");
  JButton btn8=new JButton("BTN8");
  JButton btn9=new JButton("BTN9");
  
  
  
  public Test() {
      
    panel1.add(btn1); 
      
    add(panel1, BorderLayout.WEST);
    add(panel2,BorderLayout.EAST);
    add(panel3, BorderLayout.SOUTH);
    add(panel4, BorderLayout.NORTH);

      
  btn1.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        
         
          panel2.add(btn2);
          panel2.add(btn3);
          panel2.add(btn4);
          panel2.add(btn5);
          panel2.add(btn6);
          panel2.add(btn7);
          panel2.add(btn8);
          panel2.add(btn9);
          
          repaint();
         
      }
    });
  
  
  }
  
  
  public static void main(String[] args){
        
       public static JFrame frame= new JFrame();
       frame.setLayout(new BorderLayout());
       Test applet=new Test();
       frame.add(applet,BorderLayout.CENTER);
       frame.setSize(300,300);
       frame.pack();
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setVisible(true);
       applet.init();
      
  }
  

  
}



Thanks for the replies, but neither of them worked for this code. I tried both of your suggestions (see code below) and wasn't able to get to resize. With the static Jframe object, I got an error saying this wan an illegal start for an expression.

package comp351assign1;

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

public class Test extends JApplet {
    
  JPanel panel1 = new JPanel();
  JPanel panel2 = new JPanel();
  JPanel panel3 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
  JPanel panel4 = new JPanel();
  
  JButton btn1=new JButton("BTN1");
  JButton btn2=new JButton("BTN2");
  JButton btn3=new JButton("BTN3");
  JButton btn4=new JButton("BTN4");
  JButton btn5=new JButton("BTN5");
  JButton btn6=new JButton("BTN6");
  JButton btn7=new JButton("BTN7");
  JButton btn8=new JButton("BTN8");
  JButton btn9=new JButton("BTN9");
  
  
  
  public Test() {
      
    panel1.add(btn1); 
      
    add(panel1, BorderLayout.WEST);
    add(panel2,BorderLayout.EAST);
    add(panel3, BorderLayout.SOUTH);
    add(panel4, BorderLayout.NORTH);

      
  btn1.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        
         
          panel2.add(btn2);
          panel2.add(btn3);
          panel2.add(btn4);
          panel2.add(btn5);
          panel2.add(btn6);
          panel2.add(btn7);
          panel2.add(btn8);
          panel2.add(btn9);
          
          repaint();
         
      }
    });
  
  
  }
  
  
  public static void main(String[] args){
        
       public static JFrame frame= new JFrame();
       frame.setLayout(new BorderLayout());
       Test applet=new Test();
       frame.add(applet,BorderLayout.CENTER);
       frame.setSize(300,300);
       frame.pack();
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setVisible(true);
       applet.init();
      
  }
  

  
}


Was This Post Helpful? 0
  • +
  • -

#6 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,423
  • Joined: 29-July 11

Re: autoresize JFrame or JApplet

Posted 13 February 2013 - 05:18 PM

As I told you AND showed you the first time, you have to call pack() to resize the frame, Homie G!

You also need to make your JFrame an instance variable, instead of a local on in main(). No need for static.
Was This Post Helpful? 0
  • +
  • -

#7 polska03  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 290
  • Joined: 28-November 09

Re: autoresize JFrame or JApplet

Posted 13 February 2013 - 05:22 PM

Sorry I didn't specify, but if I want to make this an applet. If it is an applet I can't extend JFrame and JApplet, how would I resize this then?
Was This Post Helpful? 0
  • +
  • -

#8 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,423
  • Joined: 29-July 11

Re: autoresize JFrame or JApplet

Posted 13 February 2013 - 05:31 PM

You can't resize the JApplet window as you specify its size via the html file, but you certainly could add a jpanel and resize it with setSize() up to the size you specified in the html file. It's a big pain in the butt. I think you should come up with something else other than what you are planning.
Was This Post Helpful? 1
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: autoresize JFrame or JApplet

Posted 14 February 2013 - 04:48 AM

An Applet size is determined by the .html size
A JFrame can resize itself but you have to call validate() after adding component(s).

P.S.
not a good idea at all to add JComponent(s) to a container on the fly, this can screw up many layout manager. Better to add all JComponet at construction time, make them setVisible(false) if required and toggle their visibility later on
Was This Post Helpful? 0
  • +
  • -

#10 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,423
  • Joined: 29-July 11

Re: autoresize JFrame or JApplet

Posted 14 February 2013 - 02:01 PM

View Postpbl, on 14 February 2013 - 11:48 AM, said:

An Applet size is determined by the .html size
A JFrame can resize itself but you have to call validate() after adding component(s).


No. You must call pack() or manually resize.
Was This Post Helpful? 1
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: autoresize JFrame or JApplet

Posted 14 February 2013 - 03:19 PM

Sorry, what I meant

An Applet size is defined in its .html file
A JFrame will display new added component if validate() is called
and yes you have to call pack() if it was called before or setSize() if the old size is not large enough to display the new added component
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1