LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   JTextPane Won't Resize (https://www.linuxquestions.org/questions/programming-9/jtextpane-wont-resize-150529/)

ElementNine 02-25-2004 06:56 PM

JTextPane Won't Resize
 
Code:

        public static void main(String args[]){
       
                /*********************************************************
                *                  Set up mainFrame                    *
                *********************************************************/
                JFrame mainFrame = new JFrame(getDocName());
                windowAction wA = new windowAction();
                mainFrame.addWindowListener(wA);
                mainFrame.setLocation(400,300);
               
                /*********************************************************
                *                    Create mainPanel                    *
                *********************************************************/
                JPanel mainPanel = new JPanel();
                mainPanel.setPreferredSize(new Dimension(800,600));
               
                JTextPane mainTextPane = new JTextPane();
                mainTextPane.setPreferredSize(new Dimension(800,600));
               
                mainPanel.add(mainTextPane, BorderLayout.CENTER);
               
                /*********************************************************
                *                Initialize mainFrame                  *
                *********************************************************/
                mainFrame.getContentPane().add(mainPanel, BorderLayout.CENTER);
                mainFrame.pack();
                mainFrame.setVisible(true);
        }

Okay theres the main method i was wondering how i get the JTextPane to resize with the window? if i dont set a preferred size its super small till i start typing and if i set the size it sticks at the size. so what do i do to make components resize to fit parts of the window

chewysplace 02-26-2004 12:06 AM

I had to dig for this but here you go:
http://216.239.37.104/search?q=cache...hl=en&ie=UTF-8

Looking_Lost 02-26-2004 02:20 AM

If you leave out mainFrame.pack(), so it doesn't optimize the size of everything and set the mainFrame.setSize(800,600) it should have the effect you want.

ElementNine 02-26-2004 09:59 PM

yeah but my textpane doesnt resize when the window resizes i want it to fill a panel

Looking_Lost 02-27-2004 02:05 AM

use a scroll pane as it's container

Code:

                ...
             
                mainFrame.setLocation(400,300);
                mainFrame.setSize(800,600);
       
                JTextPane mainTextPane = new JTextPane();
                JScrollPane mainPanel = new JScrollPane(mainTextPane);

                mainFrame.getContentPane().add(mainPanel, BorderLayout.CENTER);
               
                mainFrame.setVisible(true);
               
                ...


german 02-28-2004 01:19 PM

or give the frame's contentPane a 1x1 GridLayout if you don't want it to scroll...

Code:

mainFrame.getContentPane().setLayout(new GridLayout(1,1));
Also you don't want to put the Object into a JPanel then the contentPane, cuz that's almost the same as a JPanel inside another JPanel, and it just clutters the component hierarchy, which will slow your UI down considerably if you do it in several places. What you want to do should look like this:

Code:


        public static void main(String args[]){
               
                        /*********************************************************
                        *                  Set up mainFrame                    *
                        *********************************************************/
                        JFrame mainFrame = new JFrame("Test");
                        mainFrame.setLocation(400,300);
                       
                        /*********************************************************
                        *                    Create mainPanel                    *
                        *********************************************************/
                        Container mainPanel = mainFrame.getContentPane();
                       
                        JTextPane mainTextPane = new JTextPane();
                       
/********************************************************
      This (below) could also be something like:

mainPanel.setLayout(new GridLayout(1,1));
mainPanel.add(mainTextPane);

      in which case it would not scroll if the text outgrew the component.
********************************************************/
                        mainPanel.add(new JScrollPane(mainTextPane));
                       
                        /*********************************************************
                        *                Initialize mainFrame                  *
                        *********************************************************/
                        mainFrame.setSize(800,600);
                        mainFrame.setVisible(true);
                }

HTH

B.


All times are GMT -5. The time now is 02:16 PM.