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.

Similar Messages

  • How to change the components in a visible JPanel?

    How to change the components in a visible JPanel?
    Is there any way to change the components in a JPanel while it is already visible in a JFrame? I tried the na�ve approach of removing the components and adding new ones, but the effect is not the expected.
    For now I'm opening new frames with newly created panels.

    After adding/removing components to/from a panel try:
    panel.revalidate();
    panel.repaint() // sometimes this is also needed, I'm
    not sure whyI'm not certain, but I think that panel.revalidate() implicitly calls repaint() only if something actually changed in the layout. If nothing changed, it sees no reason to repaint; hence, if something changed in appearance but not in layout, you have to repaint it yourself.
    Or something like that. I'm no expert. ;)

  • How to track the Back and Forword functiona on a JFrame

    We are Developing an Application with Java swings.In this we 've used some of the components and are JFrame,JPanel,SplitPane,JButtons and etc.
    In this we've used JButton for the Back and forword function as we have with the I.E.
    So the Problem is that
    when I click Back Button I need to get the Previous JFrame Contents and for Forword I need to get next JFrame Contents the Function are same as back and Forword Functions of Internet Explorer.
    I'tried with Undo() Package but I did not get the Required Output
    Thanks in Adv,

    How about the following:
    1. Creat a top level container (ie window) with menus,
    back/forward buttons etc. using whichever layout
    manager is appropriate.
    2. Create a panel, and add it to this window. Set the
    panel to use card layout manager.
    3. Display all output from your app in the panel, and
    let the window's back/forward buttons call the
    appropriate methods on the card layout manager.
    Thanks for reply,
    But i had done the same thing i.e icreated a top level(using JFrame) for the buttons menus and etc.
    then using the layouts manager.
    created a panel andused cardlayout manager but my problem is taht when i add those conetents(called panel contents of the frame) as component and add it this panel then i am geeting thaierror stating
    java.lang.IllegalArgumentException: adding a window to a container
    so that is the problem i.t it is not get added to the newpanel.

  • How to set the cell size in a JFrame

    Hello. Is there any way of setting the cell size in a JFrame. The reason why I want to set the cell size is that I want to be sure of their location that when I am placing components on the JFrame. So is there any way of setting the cell size beforehand. Also for the following code.
    JFrame frame = new JFrame("Window");
    frame.setSize(100,100);is the size 100 by 100 is he size of the cell?

    smithbrian wrote:
    When I am placing a component on the grid I want to be sure the component is exactly where I place it. In order to do that I would need to know what is the size of the gird and how to change the size of the gird in the JFrame.What grid?? You're assuming that we know much more about your program than we actually do. We actually know zip. Please read this help site which will help you to avoid similar errors in posting questions here. It has helped me in the past: [smart questions|http://www.catb.org/~esr/faqs/smart-questions.html]

  • How to change the JFrame's titlebar's icon's context menu's font?

    Hi everyone.
    I want to know how to change the font of the text that appears in the context menu originated from right clicking at the icon that's all the way to the left in the titlebar of JFrames that use the default look and feel for decoration (JFrame.setDefaultLookAndFeelDecorated(true); // which uses the Metal L&F, Ocean theme).
    I searched around and found nothing. I thought that I could use what I learned at https://forums.oracle.com/message/9221826, but I couldn't come up with something that worked.
    Thanks in advance.t

    After some more messing around, I finally made it! I like Java so much that it makes me a bit sad how hard it is to do stuff like this. Anyway, I found a method here to recursively change the font of all components from a JFileChooser, but it doesn't work on the JPopupMenu (now I know the name) that pops up from the titlebar's icon. So I messed around with that method, used some casting, and was able to change the JMenuItems' font:
    public static void setSubComponentFont (Component comp[], Font font) {
        for (int x = 0; x < comp.length; x++) {
            if (comp[x] instanceof Container) {
                setSubComponentFont(((Container)comp[x]).getComponents(), font);
            try {
                //comp[x].setFont(font);
                if (comp[x].toString().contains("JMenu")) {
                    for (Component y : ((JMenu)comp[x]).getPopupMenu().getComponents()) {
                        if (y.toString().contains("JMenu")) {
                            y.setFont(font);
            } catch (Exception ex) {}
    I was inspired to use .toString().contains() by this thread.
    I also did this with nested loops, so the path to the menu items can be seen:
    for (Component a : frame.getLayeredPane().getComponents()) {
        System.out.println(a.toString());
        if (a.toString().contains("MetalTitlePane")) {
            for (Component b : ((Container)a).getComponents()) {
                System.out.println(b.toString());
                if (b.toString().contains("SystemMenuBar")) {
                    for (Component c : ((Container)b).getComponents()) {
                        System.out.println(c.toString());
                        for (Component d : ((JMenu)c).getPopupMenu().getComponents()) {
                            System.out.println(d.toString());
                            if (d.toString().contains("JMenu")) {
                                d.setFont(font);
    Every System.out.println() gives a hint to what should go on the following if condition, so they should be used one at a time. This doesn't work for the JFileChooser's title font though. When I have time I'll either look further into it or ask another question.
    So, if someone else needs it like I did, here it is. As a tip, System.out.println() and .toString() are your friends! That's how I learned what was contained in each object, and what path I needed to take to get to the objects of interest.
    Thanks anyway!

  • How to set the fix size jframe window

    when I run the jframe ,the jframe window size is very small
    1)how to set the fix size jframe window??
    2)how to set the jframe cannot change the window size??
    thx~~

    1. You can set the size by calling JFrame's method setSize. There are two versions of this method: (1) setSize(int width, int height) and (2) setSize(Dimension d). Note, that when you use this method to set the frame size, you don't have to call JFrame's pack method.
    2. JFrame has a method called setResizable(boolean resizable) which you can call with the argument false.
    Without any ill intent, these questions are regarding something that is very easily answered by looking at the documentation for the JFrame class. I suggest to anyone working with swing to at least look briefly through the base classes they're extending before giving up. In my experience it is always more gratifying to figure things out on my own than asking someone. But please don't take this the wrong way, I respect people who seek out help in order to further their knowledge, instead of just giving up.

  • How to set the JFrame to be Full screen

    yup.. how to set the JFrame to be Full screen mode??

    Hi,
    You can get the current size of desktop and resize your application to match it.
    //Get desktop screen size
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize()
    //Set bounds for main window
    setBounds(0, 0, screenSize.width, screenSize.height);     Br,
    J

  • How to access the jpanel and other components on click of a button

    hi ,
    need help,how to access the components of a frame onclick of a button ,if the actionPerformed() is written in a seperate class.
    in my code
    button.addActionListener(new Xyz());
    Xyz is implementing the ActionListener.
    in actionPerformed() i want to access the components in the frame.
    thanks.

    public class Xyz implements ActionListener{
      JFrame frame;
      Component[] comps;
      public Xyz(JFrame f){
        frame = f;
        comps = frame.getContentPane().getComponents();
      public void actionPerformed(ActionEvent e){
        // access elements in comps array
    button.addActionListener(new Xyz(this)); // if 'this' is a JFrame in question

  • How to fix the size of JFrame

    Hi Friends,
    How to fix the size of JFrame so that it can not br able to resize beyond a
    certain limit .
    i want this because i my screen if it is resized as small then the component of screen changes there location and position.
    plz help.

    You can... sort of
    The setSize() method is nasty because you don't get the event until you release the button, so the frame resizes and then snaps back. However, if you put the ComponentListener on the frame's JRootPane, you get a stream of resize events (because the window typically needs to be able to dynamically redraw itself). You can still use the setSize() method, but it is potentially confusing to the user, because the window border gets disconnected from the cursor, and as the mouse is moved further, the frame flickers between its instantaneous new size and max size.
    For a better user experience, use Robot to programmatically reposition the mouse back to the screen location equivalent to the maximum dimension.

  • How to resize painted components

    Hi All,
    I am working on a simple project called "WhiteBoard" which is veryu similar to MS-Paint. Could anyone please tell me how to resize the components which I have drawn on the panel. Thanks in advance.
    Regards,
    Shankar.

    Cast the Graphics instance passed to the paintComponent() method of your board to a Graphics2D.
    Then use the scale(double sx, double sy) method of the Graphics2D instance to zoom
    your board in or out.
    IMPORTANT NOTE: If you use a mouse listener on the board, you'll have to convert the mouse
    coordinates according to the zoom factor.

  • How to set the background for all components?

    hi
    Does anybody know how to set the DEFAULT background color in an application.. I need it because in jdk 1.3 the default bgcolor is grey and in 1.5 it is white.. and I wish white as well.. so to make it also white in jdk 1.3 I need to use the (a bit annoying) anyContainer.setBackground( Color.white ); and these are lots in my app.. So my question is: is there such an overall class with a function (say UIManager.setComponentsBackground( Color color ) ) for such a purpose?
    any tip or link would be greatly appreciated

    Does anybody know how to set the DEFAULT background color in an applicationthis might get you close
    import java.awt.*;
    import javax.swing.*;
    import java.util.Enumeration;
    class ApplicationColor extends JFrame
      public ApplicationColor()
        setApplicationColor(Color.RED);
        setLocation(400,300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JPanel jp = new JPanel(new BorderLayout());
        jp.add(new JTextField("I'm a textfield"),BorderLayout.NORTH);
        jp.add(new JComboBox(new String[]{"abc","123"}),BorderLayout.CENTER);
        jp.add(new JButton("I'm a button"),BorderLayout.SOUTH);
        getContentPane().add(jp);
        pack();
      public void setApplicationColor(Color color)
        Enumeration enum = UIManager.getDefaults().keys();
        while(enum.hasMoreElements())
          Object key = enum.nextElement();
          Object value = UIManager.get(key);
          if (value instanceof Color)
            if(((String)key).indexOf("background") > -1)
              UIManager.put(key, color);
      public static void main(String[] args){new ApplicationColor().setVisible(true);}
    }

  • How to print the whole JFrame

    Hi,
    I got this code somewhere from the net.
    import java.awt.*;
    import javax.swing.*;
    import java.awt.print.*;
    /** A simple utility class that lets you very simply print
    *  an arbitrary component. Just pass the component to the
    *  PrintUtilities.printComponent. The component you want to
    *  print doesn't need a print method and doesn't have to
    *  implement any interface or do anything special at all.
    *  <P>
    *  If you are going to be printing many times, it is marginally more
    *  efficient to first do the following:
    *  <PRE>
    *    PrintUtilities printHelper = new PrintUtilities(theComponent);
    *  </PRE>
    *  then later do printHelper.print(). But this is a very tiny
    *  difference, so in most cases just do the simpler
    *  PrintUtilities.printComponent(componentToBePrinted).
    *  7/99 Marty Hall, http://www.apl.jhu.edu/~hall/java/
    *  May be freely used or adapted.
    public class PrintUtilities implements Printable {
      private JFrame componentToBePrinted;
      public static void printComponent(JFrame c) {
        new PrintUtilities(c).print();
      public PrintUtilities(JFrame componentToBePrinted) {
        this.componentToBePrinted = componentToBePrinted;
      public void print() {
        PrinterJob printJob = PrinterJob.getPrinterJob();
        printJob.setPrintable(this);
        if (printJob.printDialog())
          try {
            printJob.print();
          } catch(PrinterException pe) {
            System.out.println("Error printing: " + pe);
      public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
        if (pageIndex > 0) {
          return(NO_SUCH_PAGE);
        } else {
          Graphics2D g2d = (Graphics2D)g;
          g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
          disableDoubleBuffering(componentToBePrinted);
          componentToBePrinted.paint(g2d);
          enableDoubleBuffering(componentToBePrinted);
          return(PAGE_EXISTS);
      /** The speed and quality of printing suffers dramatically if
       *  any of the containers have double buffering turned on.
       *  So this turns if off globally.
       *  @see enableDoubleBuffering
      public static void disableDoubleBuffering(Component c) {
        RepaintManager currentManager = RepaintManager.currentManager(c);
        currentManager.setDoubleBufferingEnabled(false);
      /** Re-enables double buffering globally. */
      public static void enableDoubleBuffering(Component c) {
        RepaintManager currentManager = RepaintManager.currentManager(c);
        currentManager.setDoubleBufferingEnabled(true);
    }From my main class (where the JFrame resides), the print() method is invoked to print the JFrame. However, when it is printed, it only prints the top left part of the JFrame, not the whole JFrame. Can anyone help please? Thank you very much.

    public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
        if (pageIndex > 0) {
          return(NO_SUCH_PAGE);
        else {
          Graphics2D g2d = (Graphics2D)g;     
          g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
           double xScale = 0.63;
           double yScale = 0.56;
          g2d.scale(xScale, yScale);
          disableDoubleBuffering(componentToBePrinted);     
          componentToBePrinted.paint(g2d);
          enableDoubleBuffering(componentToBePrinted);
          return(PAGE_EXISTS);
      }Hi all,
    Alright I managed to scale it to print the whole JFrame. But another problem surfaces. When I select draft quality printing, it doesn't print the whole JFrame. Other printing qualities (standard, high) do print the whole JFrame. I am really puzzled. Anyone has any clue?
    And I have an unrelated question to this topic. How do I set a JTextField so that when I click on it the first time, it highlights its content? Have to work around with the mouselistener?
    Thank you.

  • How to print the components in scrollpane

    hi there,
    can anyone tell me how to print the scrollpane components, i mean the headers & the main view of the component. i've a scrollpane with the following code and i want to print the components:
    JScrollPane pane = new JScrollPane(aTextPane);
    pane.setRowHeaderView(aLineNumberComponent);my requirement is i've a editor & lineno component as textpanes and added to scrollpane & when asked to print it should print the lineno component which resides in scrollpane's row header & the editor textpane which is the main view in the scrollpane. how can i achieve this?
    i'd would appreciate any suggestions & codes given.
    thanx in advance.

    After adding/removing components to/from a panel try:
    panel.revalidate();
    panel.repaint() // sometimes this is also needed, I'm
    not sure whyI'm not certain, but I think that panel.revalidate() implicitly calls repaint() only if something actually changed in the layout. If nothing changed, it sees no reason to repaint; hence, if something changed in appearance but not in layout, you have to repaint it yourself.
    Or something like that. I'm no expert. ;)

  • Where to  find the pcui_gp  components ,How to get the source code of those

    Hi All,
    Can anybody tell the exact location wher the pcui_gp components will be stored if they are  not appearing.
    And another question is how to get the source code of the pcui_gp for customization.
    anybody working on these compoents please help me.
    answers will be rewarded.
    thanks and regards,
    anand

    Hi Arun,
    I am unable to see the pcui_gp components in the DTR ,I require this in order to get the source code of one of its component.
    Can you please tell me the step by step procedure getting those pcui_gp components from J2ee engine to the  dtr or  NWDI.
    If there are any documents on pcui_gp components exclusively please do forward to my mail id [email protected]
    Thansk and Regards,
    Anand.

  • How to place the JFrame in center of the screen

    Hi.. please any one tell me how to place the jframe in the center of the screen.. i am working on windows.
    setLocationByPlotform is not working...

    //GETS SCREEN SIZE
            Dimension screen_Dimension =Toolkit.getDefaultToolkit ().getScreenSize ();
            //GETS WINDOWS SIZE
            Dimension frame_Dimension = window.getSize ();
            //PUTS FRAME IN THE "MIDDLE" OF THE SCREEN
            window.setLocation ( new Point (
                    (screen_Dimension.width / 2-frame_Dimension.width / 2),
                    (screen_Dimension.height / 2-frame_Dimension.height/ 2)));Not sure if you want the raw version of it.......

Maybe you are looking for

  • How to reformat iPod classic

    My iPod Classic told me to reformat immediately. I have tried to re-set. How do I re-format? I also get a message saying my iPod does not have permission to sync with my Mac. I've had both for 2+ years and sync'd them many times. Is this associated w

  • Help: all my capturing clips need to be rendered. why?

    I have successfully captured all my video clips from mini dv tapes in a Canon XM1 into final cut pro 4. However if i place any of these clips onto the timeline they need rendering before they will play. This has never happened before, usually only tr

  • Useable Premiere Elements 11 Serial Number

    I bought Elements 11 and Premiere Elements 11 together last year from Amazon.com.  When I tried to start Premiere on my old computer, it was taking forever, so I started and registered Elements 11.  Because I bought them together in one box, I didn't

  • Syncing email via USB

    Is there any way to sync email between Outlook and the iPhone via the USB connection - from what I've read on this forum is seems like it can't be done. What is the reason for not allowing email to be sync'd via USB?

  • Changes to an existing workflow

    Hi, I have a rather major change that needs to be done on an existing and active (production) workflow. I am wondering what should be the best approach. 1) Back up a copy of the workflow in DEV and make the necessary changes to the <b>old workflow</b