DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   JComponent Placement (http://www.daniweb.com/forums/thread201321.html)

llemes4011 Jul 2nd, 2009 5:04 pm
JComponent Placement
 
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?

VernonDozier Jul 2nd, 2009 5:37 pm
Re: JComponent Placement
 
Quote:

Originally Posted by llemes4011 (Post 906810)
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?

llemes4011 Jul 2nd, 2009 6:13 pm
Re: JComponent Placement
 
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....

adatapost Jul 3rd, 2009 1:27 am
Re: JComponent Placement
 
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?

llemes4011 Jul 3rd, 2009 5:04 am
Re: JComponent Placement
 
Quote:

Originally Posted by adatapost (Post 907107)
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.

adatapost Jul 3rd, 2009 7:40 am
Re: JComponent Placement
 
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.

llemes4011 Jul 5th, 2009 12:42 am
Re: JComponent Placement
 
I tried that and it didn't resize it. ( I tried it as a JPanel too...)

adatapost Jul 5th, 2009 12:49 am
Re: JComponent Placement
 
Please post complete code with bb code tags.

llemes4011 Jul 5th, 2009 12:55 am
Re: JComponent Placement
 
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();
            }
        });
    }
}

adatapost Jul 5th, 2009 1:23 am
Re: JComponent Placement
 
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.


All times are GMT -4. The time now is 8:31 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC