How to resize window (JFrame) on event

I've got a swing application that uses tabs (JTabbedPane). It's default size is 200,200, and I need to resize the window to different sizes depending on what tab is selected. I can set the default size, but I can't seem to get access to the window to resize it (setBounds/setSize) at runtime.
If anybody can tell me how I can go about this, I would greatly appreciate it.
Matt
Here's the code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class TimeTrack_Example extends JFrame {
     Container cp = getContentPane();
  public static void main(String args[]) {
          TimeTrack_Example TT = new TimeTrack_Example();
          TT.setSize(200,200);
  public TimeTrack_Example() {
          JTabbedPane JTPane = new JTabbedPane();
          JTPane.addChangeListener(cl);
               //Jobs Tab
          JPanel jpJobs = new JPanel();
          JTPane.addTab("Jobs", jpJobs);
               //Timer Tab
          JPanel jpTimer = new JPanel();
          JTPane.addTab("Timers", jpTimer);
               //Report Tab
          JPanel jpReport = new JPanel();
          JTPane.addTab("Report", jpReport);
    cp.add(JTPane);
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {System.exit(0);}
    pack();
    show();
          //Tab ChangeListener
     ChangeListener cl = new ChangeListener() { 
          public void stateChanged(ChangeEvent e) {
               int tabID = ((JTabbedPane)e.getSource()).getSelectedIndex();
               if (tabID == 2) {
                    //???               //here's where I need to change the size
}

This is one way:
create a method, lets call it resizeWindow(). Call this method from inside your ChangeListener.
public void resizeWindow() {
this.setSize(300, 400); //whatever values you want
public void stateChange(ChangeEvent e) {
if (tabID == 2) {
resizeWindow();
}

Similar Messages

  • Resizing windows (JFrame)

    Hi,
    I am developing data entry screens and would like to ensure that the user cannot resize, maximize or minimize, i.e can only close the window (JFrame).
    The reason is that if the user maximizes the screen the data input fields are distributed over the screen and this does not look so great!
    Only the minimize option seems to make sense.
    How do I do this?
    Brgds and happy new year
    John

    I thought you could use this:
    frame.setUndecorated(true);
    frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
    frame.setResizable(false);But it looks bad.
    If you want to get also an entry in the Task Bar create a JFrame with size 0,0 and setUndecorated(true).
    And create your JDialog like this:
                   JFrame frame = new JFrame("test");
                   frame.setSize(0, 0);
                   frame.setUndecorated(true);
                   frame.setVisible(true);
                   JDialog dialog = new JDialog(frame);
                   dialog.setSize(300, 300);
                   dialog.setVisible(true);
                   dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                   dialog.addWindowListener(new WindowAdapter() {
                        public void windowClosed(WindowEvent e) {
                             System.exit(0);
                   });

  • How to resize windows by pulling edges (like in Windows)

    Hi, is there a way to pull windows wider (resize them) by pulling on the window edges rather than the lower-right corner? I tend to have a lot of Terminal windows, as well as Photoshop windows.
      Windows XP Pro  

    Your profile says Windows XP pro, and doesn't say what Mac operating system you are running. Now most Mac operating systems do not let you pull the window edges to resize the windows. The only controls you'll get are in the corners.
    There have been some hacks on certain operating systems to make the edges work, but like I say they are hacks, and may not work through an operating system update. The last time I saw such a hack work was way back in System 7.6 with Church Windows. There may be additional ones on Mac OS X that work that have been mentioned on:
    http://www.resexcellence.com/
    Of course if you run Windows in emulation, or virtualization environment, Windows applications themselves will still have their usual interface behavior:
    http://www.macmaps.com/macosxnative.html#WINTEL
    tells of several such options.
    I'd add, I prefer the controls to remain on the corners, because otherwise you get confused how to move windows using their edges, or you might misdirect your cursor to the scrollbar and instead end up resizing the window when you don't want to.

  • How to resize window when stage style is undecorated ?

    Hi,
    I have one undecorated window and I want to make it resizable by using mouse.
    How can I do it ?
    If I use decorated window then it allows me to resize it.
    Thanks.

    Just make a draggable shape and resize the stage depending on the drag action.
    Here is a simple stage illustrating that and moving the window too.
    Note an old bug already seen in 1.1 at least: when dragging such window, on WinXP, sometime it seems to "jump" to one bound of the screen, then it goes back (in a flicker) to current position. Strange and annoying.
    def resizeHandle: Path = Path
        var origX = bind scene.width
        var origY = bind scene.height
        var startW: Number; var startH: Number;
        elements:
            MoveTo { x: bind origX, y: bind origY }
            LineTo { x: bind origX - 50, y: bind origY - 30 }
            LineTo { x: bind origX - 30, y: bind origY - 50 }
        fill: Color.CORAL
        stroke: null
        blocksMouse: true
        // Resize with the arrow
        onMousePressed: function (evt: MouseEvent): Void
            startW = stage.width;
            startH = stage.height;
        onMouseDragged: function (evt: MouseEvent): Void
            stage.width = startW + evt.dragX;
            stage.height = startH + evt.dragY;
    var keyHandler: Rectangle;
    def titleBar: Stack = Stack
        var title: Text;
        content:
            keyHandler = Rectangle
                width: bind title.layoutBounds.width * 1.2
                height: bind title.layoutBounds.height * 1.3
                fill: Color.web('#33FF55')
                blocksMouse: true
                // We can drag the stage by dragging the title bar
                onMouseDragged: function (evt: MouseEvent): Void
                    stage.x += evt.dragX;
                    stage.y += evt.dragY;
                onKeyReleased: function (evt: KeyEvent): Void
                    if (evt.code == KeyCode.VK_ESCAPE)
                        FX.exit();
                    println(evt);
            title = Text
                content: "Resizable and Draggable Window"
                font: Font { name: "Arial Bold", size: 24 }
                fill: Color.web('#331122')
    var scene: Scene;
    def stage: Stage = Stage
        title: "Undecorated Stage"
        style: StageStyle.UNDECORATED
        scene: scene = Scene
            width: 500
            height: 500
            fill: Color.GOLD
            content:
                Ellipse
                    centerX: bind scene.width / 2
                    centerY: bind scene.height / 2
                    radiusX: bind 0.45 * scene.width
                    radiusY: bind 0.45 * scene.height
                    fill: Color.AQUA
                Circle
                    centerX: bind scene.width - 55
                    centerY: bind scene.height - 55
                    radius: 50
                    fill: Color.PURPLE
                titleBar,
                resizeHandle
    keyHandler.requestFocus();

  • HT4818 how to resize windows partition

    How do you resize the windows partition in boot camp?

    iF you have already created the Windows partition, you can't resize it. To do it, you have to use an app like Paragon Camptune, that will allow you to resize your Windows volume > http://www.paragon-software.com/downloads/camptune.html
    The other option is to delete Windows and create the new partition with the size you want in Windows. Make a backup of your data before doing this

  • How to resize window keeping relative width and hieght

    Ok as usual I do not want this done for me but I could use a pointer. Simply put I wish to set a JFrame so that when it is resized by the user it holds its relative width and height.
    Could somebody point me to where it is at in the API or a generic sample of code that I can rework? I have read for it but am not finding it so far. Thank you for your patience and any help offered. If it matters I'm doing a fairly simple app in NetBeans not an applet or anything.
    Edited by: Donalds on Apr 8, 2010 5:10 PM

    This was the best I could think of off the top of my head.
    You may want to request this thread be moved to the swing forums for better contributions.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ComponentEvent;
    import java.awt.event.ComponentListener;
    public class SampleFrame {
        public Dimension lastSize = new Dimension(400,300);
        public JFrame frame;
        public SampleFrame() {
            frame = new JFrame("Sample Frame");
            frame.setSize(lastSize);
            frame.setLocationRelativeTo(null);
            frame.addComponentListener(new ComponentListener() {
                public void componentResized(ComponentEvent e) {
                    Dimension currentFrameSize = frame.getSize();
                    int widthDelta = Math.abs(lastSize.width - currentFrameSize.width);
                    int heightDelta = Math.abs(lastSize.height - currentFrameSize.height);
                    if(heightDelta > widthDelta) {
                        double scaleRatio = currentFrameSize.getHeight() / lastSize.height;
                        currentFrameSize.width = (int) (lastSize.width * scaleRatio);
                    } else {
                        double scaleRatio = currentFrameSize.getWidth() / lastSize.width;
                        currentFrameSize.height = (int) (lastSize.height * scaleRatio);
                    frame.setSize(currentFrameSize);
                    lastSize = currentFrameSize;
                    frame.setLocationRelativeTo(null);
                public void componentMoved(ComponentEvent e) {
                public void componentShown(ComponentEvent e) {
                public void componentHidden(ComponentEvent e) {
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        public static void main(String[] args) {
            new SampleFrame();
    }

  • HELP: How to get window (JFrame) from JPanel ???

    Hi there,
    anyone knows how I can get a ref. to the window (in the form of JFrame) from my JPanel that is in the actual window??
    I need that window when I display a modal dialog from the panel, as the dialog constructor takes the window as a param.
    I could always pass a window-ref. to the JPanel when I create it, but as I have a lot of panels, I'd rather be able to get it just when I need it from the JPanel!
    Thanks a lot for any help on this issue!
    Best regards,
    AC

    Thanks a lot!
    I'll use windowForComponent in swingUtils, I think getRootPane() would only return the panel that the component is in!
    Regards,
    AC

  • How to resize window?

    I sometimes have a problem of window becoming so large, that it's bottom right corner is off-screen (when I disconnect a large external monitor)...how can I resize this window?

    Here is a third party app that will help out:
    http://www.atomicbird.com/mondomouse/
    Kj

  • How to resize window smoothly?

    I need to screen record a resizing a window but need it to happen in a very smooth manner.  I'd could manually drag the window but it won't be completely smooth or fluid.  I thought to use Automator but I don't see anything under resize. 
    I'd like to set starting and ending points.  If it could pause in the middle that would be great.
    Any ideas how this can be accomplished?

    Try the following:
    repeat 20 times
              set boundBox to bounds of window "Downloads" of application "Finder"
              set tFrame to (item 1 of boundBox)
              set lFrame to (item 2 of boundBox)
              set rFrame to (item 3 of boundBox) - 2
              set bFrame to (item 4 of boundBox)
              set bounds of window "Downloads" of application "Finder" to {tFrame, lFrame, rFrame, bFrame}
              delay 0.05
    end repeat
    In this script, change the "- 2" value to adjust the amount the window will change at each loop. The repetition count of 20 can be changed to also adjust the amount the window will change. Both of these will set the extent of the window change, and then you can adjust the delay value (in seconds) to change the rate once the other two values have been set.
    It will take some experimenting to figure

  • Resizing of JFrame while the font on buttons increases

    Hey,
    I would like to ask you how to resize my JFrame ,when the size of buttons put in this frame increases.
    I mean the font on the buttons increases by 1 point every time I press the button. My goal is to make the two buttons visible all the time. Therefore I need to resize my JFrame. I tried in the following method, but it did not work :(
    (cp is contentPane() of my JFrame - frame)
    public void actionPerformed(ActionEvent e) {
    Component c = (Component) e.getSource();
    Integer nazwa= c.getFont().getSize()+1;
    int width = (int) cp.getPreferredSize().getWidth();
    if(cp.getPreferredSize().getWidth()>cp.getWidth()) {
    cp.setSize(new Dimension(width, cp.getHeight()));
    cp.validate();
    int height = (int) cp.getPreferredSize().getHeight();
    if(cp.getPreferredSize().getHeight()>cp.getHeight()) {
    cp.setSize(new Dimension(cp.getWidth(),height));
    cp.validate();
    c.setFont( new Font(c.getName(), c.getFont().getStyle(), nazwa));
    frame.validate();
    }

    pack() does not help, as I have already used it.
    Pls find below the whole code of the program:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    class GUI implements ActionListener{
    final int BNUM = 2;
    static JFrame frame = new JFrame()
    static Container cp = frame.getContentPane();
    GUI() {
    cp.setLayout(new BoxLayout(cp, BoxLayout.X_AXIS));     
    for (int i = 1; i <= BNUM; i++) {
    JButton b = new JButton("Przycisk " + i);
    b.addActionListener(this);
    cp.add(b);
    frame.pack();
    frame.show();
    public void actionPerformed(ActionEvent e) {
    Component c = (Component) e.getSource();
    Integer nazwa= c.getFont().getSize()+1;
    int width = (int) Integer.parseInt(cp.getPreferredSize().getWidth().toString());
    if(cp.getPreferredSize().getWidth()>cp.getWidth())
    cp.setSize(new Dimension(width, cp.getHeight()));
    if(cp.getPreferredSize().getHeight()>cp.getHeight()) {
    cp.setSize(new Dimension(cp.getWidth(),cp.getPreferredSize().getHeight()));
    cp.validate();
    c.setFont( new Font(c.getName(), c.getFont().getStyle(), nazwa));
    public static void main(String[] a) {
    //cp.setPreferredSize(new Dimension(200,200));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    new GUI(); }
    }

  • Resizing Window

    Hi, i designed one window, and set its modality Transparent for good look. now i want to make it resizable. how can i make it resizable?
    anyone know how to resize window which is transperent. i just want to resize it from x axis only......
    Edited by: 924666 on Apr 26, 2012 5:06 PM

    The following example uses ctr+C and ctr+V to resize the transparent stage
    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.paint.Color;
    import javafx.stage.*;
    import javafx.scene.shape.*;
    import javafx.scene.input.*;
    public class Trans extends Application {
         * @param args the command line arguments
        public static void main(String[] args) {
            Application.launch(Trans.class, args);
        @Override
        public void start( final Stage primaryStage) {
            primaryStage.setResizable(true);
            primaryStage.setTitle("Owner");
            primaryStage.initStyle(StageStyle.TRANSPARENT);
            Group root = new Group();
            Scene scene = new Scene(root, 300, 250, Color.RED);
            primaryStage.sizeToScene();
            primaryStage.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
                public void handle(KeyEvent event) {
                    if (event.isControlDown()) {
                        if (event.getCode() == KeyCode.C ) {
                           primaryStage.setWidth(primaryStage.getWidth()+40);
                           primaryStage.setHeight(primaryStage.getHeight()+40);
            primaryStage.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
                public void handle(KeyEvent event) {
                    if (event.isControlDown()) {
                        if (event.getCode() == KeyCode.V ) {
                           primaryStage.setWidth(primaryStage.getWidth()- 40); 
                            primaryStage.setHeight(primaryStage.getHeight()- 40);
            primaryStage.setScene(scene);
            primaryStage.show();
    }

  • Resize windows and manage files in save/open dialogs?

    1. How to resize windows by dragging any border or corner?
    2. How to manage files via a Save/SaveAs or Open dialog (rename, delete, copy other files, etc...)?
    My search on these two things only comes up with comparisons and debates about Win vs Mac. Not relevant to the questions! I already run Win XP in Fusion when I have to for work. I don't want to run Windows; I just want to eliminate a couple Mac shortcomings since Apple hasn't. Yet. Maybe it has to do with patents...
    Anyway, does anybody know of a utility or other way to enable these functions in Mac OS 10? (I've tried Pathfinder; it doesn't quite do the trick.)

    I believe you will find a couple of third-party utilities that provide some enhancements for windows. As for file dialogs I can only recommend Default Folder X. You will find these at VersionTracker or MacUpdate.

  • How do i drag icons and how do i resize windows

    this is my first laptop having been used to emacs before. I can't figure out how to drag items or resize windows when not using a mouse and can't find such simple instructions in the manual.
    thanks.

    In addition to the method described by Jim, you can set options in the Keyboard & Mouse preference panel (in System Preferences) that allow you to get mouse clicks and click lock using the trackpad. Check out those options, many people find they make the trackpad a lot more useful.
    Randall Schulz
    iMac 20" Core Duo; MacBook Pro   Mac OS X (10.4.6)  

  • How to resize photoshop elements 11 window on mac? no green+

    How to resize photoshop elements 11 window on mac? no green+ button as off screen.  cannot rescale monitor - no option on macpro

    Go to system prefs>displays and set your resolution way down. Let OS X open with the new resolution. Then set it back up to the highest available resolution and the window should position itself correctly.

  • How can I resize a JFrame ,to fit into the screen size

    I have a Jframe which has JPanel and JPanel contains lot of other components.JPanel size is 980,1400. when i use JFrame.show method jpanel goes beyond the screen size in length and I am not able to see the portion below the screen.How can I resize the JFrame so that JFrame and JPanel shrinks to fit into the screen size.I need this because I have a PRINT button at bottom of the JPanel.Thanks.

    Thank you for your reply.I tried with the following code as you have told.But the frame is still going beyond the screen.Can you please look into it and tell me whats wrong ?
    //public class PlayerRegForm extends javax.swing.JFrame implements Printable
    public static void main(String args[]) {
    PlayerRegForm prf = new PlayerRegForm();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = prf.getSize();
    if (frameSize.height > screenSize.height)
    frameSize.height = screenSize.height;
    if (frameSize.width > screenSize.width)
    frameSize.width = screenSize.width;
    System.out.println("Screen Size ----------------- " + screenSize);
    System.out.println(" Frame Size ----------------- " + frameSize);
    prf.setSize(frameSize.width, frameSize.height);
    prf.pack();
    prf.show();
    =============================================================================
    Screen Size ----------------- java.awt.Dimension[width=1024,height=768]
    Frame Size ----------------- java.awt.Dimension[width=112,height=28]

Maybe you are looking for

  • How to install manually the specific drivers o keyboard in windows 7 well?

    After install Windows 7 all work ok, but the some keys os my keyboard do not wor, @, volume + or - the brightness etc etc, onli work the letters. how can i do for install the specific keyboard driver? thanq´s all for anwser

  • Weird issue with cli php..

    I try to start my script off with #!/usr/bin/php -r So that I can not use the <?php and ?> tags, and I keep getting an error. Can anyone get this to work? I am using a custom compiled php, so maybe something is wonky on my end.. I keep getting Parse

  • Document properties

    Hi I am creating a form in Livecycle and then when viewing in Acrobat Pro the document properties options are all greyed out.  This is frustrating as I have not been able to set the document to open in full view and add metadata there.  I am sure tha

  • Regular expressions in oracle 10 g

    how to use regular expressions in oracle 10g forms

  • KANBAN and Operation level confirmations

    We are planning to use the SAP KANBAN. Our company uses SAP 4.7. Currently they have visual KANBANs opearting in the factory. We plan to use KANBAN with Production Order strategy. The problem is that ,  we have multiple opeartions within one KANBAN c