Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Adjusting Size of JPanel to Boundaries of Painting

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,684
    Thanks
    25
    Thanked 333 Times in 309 Posts

    Default Adjusting Size of JPanel to Boundaries of Painting

    Is it possible to adjust the size of a JPanel to ensure all of the Painting done on it is viewable?

    A code snippet I created to try to deal with all the problems I may face prior to implementing it on a full size program is below:
    import javax.swing.*;
    import java.awt.*;
    import java.io.*;
     
    public class DrawHelpExample extends JPanel
    {
    	String text0;
     
        public DrawHelpExample() 
        {
        	super();
        	//super.setPreferredSize(new Dimension(725,1200));
        	try{
     
    	    	BufferedReader in = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("EventTableOverviewText0.txt")));
    			String line = in.readLine();
    			text0 = "";
    			while(line!=null)
    			{
    				text0+=line;
    				line = in.readLine();
    				if(line!=null)
    					text0+="-----";
    			}
    			in.close();
     
        	}catch(Exception ex){; 	}
        }
     
        public void paintComponent(Graphics g)
        {
        	super.paintComponent(g);
        	Graphics2D g2 = (Graphics2D)g;
        	String[] text0Vals = text0.split("-----");
        	for(int i=0;i<text0Vals.length;i++)
        		g2.drawString(text0Vals[i],10,10+(20*i));
        }
     
        public static void main(String[] args)
        {
        	JFrame frame = new JFrame("Test Frame");
        	frame.setSize(750,450);
        	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	JScrollPane scrollPane = new JScrollPane(new DrawHelpExample(),JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        	//scrollPane.setPreferredSize(new Dimension(750,450));
        	frame.setContentPane(scrollPane);
        	frame.setVisible(true);
        }
    }

    I cannot provide the content of EventTableOverviewText0.txt, but just say that it's lines of data are beyond the size of the viewable frame (thus the need for the resizing of the JPanel to allow the JScrollPanel to scroll). Can anyone provide any insight?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,475
    My Mood
    Hungover
    Thanks
    144
    Thanked 641 Times in 548 Posts

    Default Re: Adjusting Size of JPanel to Boundaries of Painting

    The size of a JPanel is mostly handled by the layout you're using with it. You could set the size of the JPanel, but make sure that the layout you're using honors it.

    You could also use a JScrollPane to allow scrolling.

    Or you could scale the painting to the size of the JPanel- making the JPanel smaller or larger makes the stuff drawn inside of it smaller or larger.

    Or you could use an actual text component instead of trying to reinvent the wheel.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,684
    Thanks
    25
    Thanked 333 Times in 309 Posts

    Default Re: Adjusting Size of JPanel to Boundaries of Painting

    Quote Originally Posted by KevinWorkman View Post
    The size of a JPanel is mostly handled by the layout you're using with it. You could set the size of the JPanel, but make sure that the layout you're using honors it.

    You could also use a JScrollPane to allow scrolling.

    Or you could scale the painting to the size of the JPanel- making the JPanel smaller or larger makes the stuff drawn inside of it smaller or larger.

    Or you could use an actual text component instead of trying to reinvent the wheel.
    The text is one part of a much larger thing, including pictures and whatnot on a Scrollable JPanel. At first I had it set up to have the pictures on JLabels and the text in JTextAreas, but that just doesnt seem to cut it.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,475
    My Mood
    Hungover
    Thanks
    144
    Thanked 641 Times in 548 Posts

    Default Re: Adjusting Size of JPanel to Boundaries of Painting

    Well you know JTextPanes support embedded images and whatnot, right?

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,684
    Thanks
    25
    Thanked 333 Times in 309 Posts

    Default Re: Adjusting Size of JPanel to Boundaries of Painting

    Quote Originally Posted by KevinWorkman View Post
    Well you know JTextPanes support embedded images and whatnot, right?
    No I did not. How would I do something like 3 Images of unknown height and under each of those a set of text of an unknown height, all Scrollable as 1 thing?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,475
    My Mood
    Hungover
    Thanks
    144
    Thanked 641 Times in 548 Posts

  7. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,684
    Thanks
    25
    Thanked 333 Times in 309 Posts

    Default Re: Adjusting Size of JPanel to Boundaries of Painting

    So could I send the JTextPane a .rtf file somehow and just get it to display the same way in the JTextPane as it does on the document?

    EDIT: Ok, I actually found an example of this here: Viewing RTF format : Text EditorPaneSwing JFCJava

    However, the images are not read in. Any suggestions as to how to get the images as well as the text?
    Last edited by aussiemcgr; November 22nd, 2010 at 12:27 PM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. painting Image on JPanel inside a JScrollPane
    By becca23 in forum AWT / Java Swing
    Replies: 1
    Last Post: September 29th, 2010, 07:28 PM
  2. [SOLVED] Extends JPanel painting problem
    By Philip in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 28th, 2010, 03:16 PM
  3. Limit File Size or Request Size
    By tarek.mostafa in forum Java Servlet
    Replies: 3
    Last Post: June 12th, 2010, 04:28 PM
  4. Limit File Size or Request Size
    By tarek.mostafa in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: June 11th, 2010, 07:21 AM
  5. Help with adjusting a program. NEw to java!!!!!
    By raidcomputer in forum Loops & Control Statements
    Replies: 1
    Last Post: September 27th, 2009, 08:05 PM