Hi. When I add a JComponent to my JFrame, it isn't the size of the remaining space in the frame. I want it to work in a way so that when i add a JComponent, it (and it's contents) stretches to fill the remaining space in the screen... Is there anyway to do that?

Hi. When I add a JComponent to my JFrame, it isn't the size of the remaining space in the frame. I want it to work in a way so that when i add a JComponent, it (and it's contents) stretches to fill the remaining space in the screen... Is there anyway to do that?

This will depend on your LayoutManager. What LayoutManager are you using?

I'm not... But I found a way that works. I have a thread that has static copies of the JFrame and the JComponent, and is always watching to see if the height or width is changing, then resizes the JComponent. I don't know it that is the way it's supposed to be done (probably not), but it is working. I only have 2 Components in the frame, so i think this way should be fine....

VernonDozier>What LayoutManager are you using?
llemes4011>I'm not... But I found a way that works.
Can I see the way you have used?
Do you know BorderLayout, GridBagLayout & GridBagConstraints?

VernonDozier>What LayoutManager are you using?
llemes4011>I'm not... But I found a way that works.
Can I see the way you have used?
Do you know BorderLayout, GridBagLayout & GridBagConstraints?

Yeah here's the code:

package JEmail.util;

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

public class FrameResizeHandler extends Thread
{
    //consists of 2 JSplitPanes (one nested)
    private static ContentViewer cv;
    //components to resize
    private static ArrayList<JComponent> components;
    // JFrame to resize to
    private static JFrame watching; 
    
    private static int width;  // JFrame width to check against
    private static int height; // JFrame height to check against
    
    /**
     * I used an ArrayList and JComponents because I want to 
     * get it so that it can deal with multiple resizings at once
     */
    public FrameResizeHandler(JFrame jf, JComponent jc){
        watching = jf;
        components = new ArrayList<JComponent>();
        components.add(jc);
        width = watching.getWidth();
        height = watching.getHeight();
    }
    
    public void run(){
        while(true){
            if(width!=watching.getWidth() || height!=watching.getHeight()){
                if(components.get(0) instanceof ContentViewer){
			  // I know what the first element is so that's why it looks like this
                    cv = (ContentViewer)components.get(0); 
                    // adjust the JComponents' size
                    cv.setNewSize(watching.getWidth()-watching.getInsets().left-watching.getInsets().right, 
                                  watching.getHeight()-watching.getInsets().top-watching.getInsets().bottom-57);
                }
                width = watching.getWidth();  // re-adjust the dimensions to check against
                height = watching.getHeight();
            }
        }
    }
}

Note that this is still VERY sloppy. It doesn't completely work (It doesn't fix the size when the MAXIMIZE button is pressed), and is slow. I need to put in checks to determine when the thread actually goes through its loop so it doesn't eat up processor space (WHICH IT DOES! >=[ )

But Yes. I do know about BorderLayout, and I hate GridBagLayout. It's a total pain in the butt. But it does have its perks. I used it in another section of my program and it worked well there. I couldn't get it to work here.

Use BorderLayout and add a component with CENTER position. Do not forget to issue validate() method of container after component has been added to that container.

I tried that and it didn't resize it. ( I tried it as a JPanel too...)

Please post complete code with bb code tags.

Okay, Here's what I have:

package JEmail.util;

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

public class ContentViewer extends JPanel
{
    public ContentViewer(){
        createAndShowGUI();
    }
    
    public void createAndShowGUI(){
        
        WindowComponents.vertSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        WindowComponents.horiSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        WindowComponents.files = new JTree();
        WindowComponents.tabs = new JTabbedPane();
        WindowComponents.display = new JPanel();
        WindowComponents.tabs.add(new InboxTab(), "Inbox");
        WindowComponents.tabs.add(new ContactsTab(), "Contacts");
        WindowComponents.tabs.add(new NotesTab(), "Notes");
        
        WindowComponents.vertSplitPane.setTopComponent(WindowComponents.tabs);
        WindowComponents.vertSplitPane.setBottomComponent(WindowComponents.display);
        WindowComponents.vertSplitPane.setDividerLocation(.5);
        WindowComponents.horiSplitPane.setLeftComponent(WindowComponents.files);
        WindowComponents.horiSplitPane.setRightComponent(WindowComponents.vertSplitPane);
                
        this.add(WindowComponents.horiSplitPane);
        
    }
}
package JEmail;
import JEmail.util.*;
import javax.swing.*;
import java.awt.*;
/**
 * Write a description of class JMail_Runner here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class JMail_Runner{

    private static final int PWIDTH = 600;
    private static final int PHEIGHT = 500;

    JFrame jMailWindow;
    JEmailMenuBar menuBar;
    ContentViewer content;
    FrameResizeHandler frh;
    
    Insets windowInsets;
    
    public JMail_Runner(){
        createAndShowGUI();
    }
    
    public void createAndShowGUI(){
        try{ // Set the L&F to match the System - will have option to change later.
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }catch(Exception e){
            e.printStackTrace();
        }

        jMailWindow = new JFrame("JEmail");
        jMailWindow.setPreferredSize(new Dimension(PWIDTH, PHEIGHT));
        jMailWindow.setLocation(150,100);
        jMailWindow.setSize(PWIDTH, PHEIGHT);
        
        Container pane = jMailWindow.getContentPane();
        pane.setLayout(new BorderLayout());
        
        menuBar = new JEmailMenuBar();
        content = new ContentViewer();
        
        // The MenuBar stretches fine... just not the content object
        pane.add(menuBar, BorderLayout.NORTH);
        pane.validate();
        pane.add(content, BorderLayout.CENTER);
        pane.validate();
        
        jMailWindow.setVisible(true);
    }
    
    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new JMail_Runner();
            }
        });
    }
}

As per your post I did't find any ambiguity in your code. I have added JMenuBar and ContentView and some components into ContentView - There is no problem.

I am not sure about JEmailMenuBar class. If it is JMenuBar then use setJMenuBar method of JFrame.

huh. I tried it without the JEmailMenuBar (It has a JMenuBar and a JToolbar in it arranged with a GridBagLayout) and it still doesn't work for me....

Are there any methods that I'm forgetting to call, or something like that?

Use setJMenuBar method: It has a seperate layer.

setJMenuBar(menuBar);

Ok, I changed my JEmailMenuBar to extend the JMenuBar class, ad added it through the setJMenuBar() method, but it didn't help the contentViever's sizing problems....

What is the current size of contentViever? Size of contentViever will be grows or shrink with frame.

contentViewer's size is the size of the Frame. however. the JSplitFrame has a size of (0,0)

If you want to change the size of any control use setPreferredSize() method.

commented: Thanks for helping me out! That had been driving me crazy!!! +1

YAY! I got it, Thanks so much for your help =D It ended up that It was the placement of some static variables. I was trying it access them from another package and I think some how it was throwing it off somehow... the whole static thing is still kind of new to me O.o but very useful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.