tags:

views:

44

answers:

2

Hi,

I am developing a simple swing app in which I have a main window with three buttons. When I click on the first button it opens new window of (200,200) dimension. When I click on the second button, the newly opened window's height should get increased and when I click on third button height should get decreased. Can you help me with code....

thanks in advance.

+2  A: 

you could do the following on the newly opened windows which you want to resize:

JFrame fr=getNewlyOpenendWindowReference(); // get a reference to the JFrame
fr.setSize(fr.getSize().getWidth() + 10,fr.getSize().getHeight() + 10);
fr.repaint();

this should increase the size of the JFrame length and widthwise by 10 pixels per call.

smeg4brains
Or use `setBounds` if you want the growth to not always be on the right and bottom of the frame.
Erick Robertson
A: 

Create a Controller class to handle the action events.

Define a FramePanel extends JPanel and add your buttons to it. Set up constants in the class with action event values and set them on your buttons. Then, you can instantiate this FrameController and add it as the listener to those buttons using JButton.addActionListener(). Or, you can do this in the constructor of the FrameController class.

public class FrameController implements ActionListener {
  private JFrame openedFrame;

  public static final int MINIMUM_HEIGHT = 200;

  public FrameController(FramePanel panel) {
    this.panel.getOpenFrameButton().addActionListener(this);
    this.panel.getIncreaseHeightButton().addActionListener(this);
    this.panel.getDecreaseHeightButton().addActionListener(this);
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    String action = e.getActionCommand();
    if (action.equals(FramePanel.ACTION_OPEN_FRAME)) {
      this.openedFrame = new JFrame();
      // set it up how you want it
    } else if (action.equals(FramePanel.ACTION_INCREASE_HEIGHT)) {
      this.openedFrame.setSize(this.openedFrame.getWidth(), this.openedFrame.getHeight() + 10);
    } else if (action.equals(FramePanel.ACTION_INCREASE_HEIGHT)) {
      int newHeight = this.openedFrame.getHeight() - 10;
      if (newHeight < FrameController.MINIMUM_HEIGHT)
        newHeight = FrameController.MINIMUM_HEIGHT;
      this.openedFrame.setSize(this.openedFrame.getWidth(), newHeight);
    }
  }
}
Erick Robertson
Hi it sounds good but i m getting error."Set up constants in the class with action event values and set them on your buttons".i m not getting this part.plz tell me indetail.
sheetal
Declare `public static final String ACTION_OPEN_FRAME = "OpenFrame";` in the FramePanel class. Then, on the button, call `this.openFrameButton.setActionCommand(FramePanel.ACTION_OPEN_FRAME);` This will ensure that the command is sent to the listener when the button is pressed so the listener knows *which* button was pressed.
Erick Robertson