| | |
JComponent Placement
Thread Solved |
•
•
Join Date: Jan 2008
Posts: 3,803
Reputation:
Solved Threads: 501
•
•
•
•
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?
•
•
Join Date: Aug 2008
Posts: 201
Reputation:
Solved Threads: 13
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....
•
•
Join Date: Aug 2008
Posts: 201
Reputation:
Solved Threads: 13
•
•
•
•
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?
Java Syntax (Toggle Plain Text)
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.
Last edited by llemes4011; Jul 3rd, 2009 at 5:09 am. Reason: Added Comments
•
•
Join Date: Aug 2008
Posts: 201
Reputation:
Solved Threads: 13
Okay, Here's what I have:
Java Syntax (Toggle Plain Text)
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); } }
Java Syntax (Toggle Plain Text)
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.
I am not sure about JEmailMenuBar class. If it is JMenuBar then use setJMenuBar method of JFrame.
Failure is not fatal, but failure to change might be. - John Wooden
![]() |
Similar Threads
- placement papers and jobs site: sidjobs.info (Website Reviews)
- Placement new (C++)
- Not able to set the background color of JComponent (Java)
- Radiobutton text placement (Python)
- Advertising placement (Advertising Sales Strategies)
Other Threads in the Java Forum
- Previous Thread: I need a class in Java API...
- Next Thread: open local folder with a jButton
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation bi binary blackberry block bluetooth character class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp functiontesting game gameprogramming givemetehcodez graphics gui html ide image int integer j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux list mac map method methods mobile myregfun netbeans nonstatic notdisplaying number online printf problem program project qt recursion researchinmotion rotatetext rsa scanner screen server set singleton sms sort sql string swing system textfields thread threads time title tree tutorial-sample update variablebinding windows working xor






