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:
Code java:
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?
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.
Re: Adjusting Size of JPanel to Boundaries of Painting
Quote:
Originally Posted by
KevinWorkman
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.
Re: Adjusting Size of JPanel to Boundaries of Painting
Well you know JTextPanes support embedded images and whatnot, right?
Re: Adjusting Size of JPanel to Boundaries of Painting
Quote:
Originally Posted by
KevinWorkman
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?
Re: Adjusting Size of JPanel to Boundaries of Painting
Re: Adjusting Size of JPanel to Boundaries of Painting
Quote:
Originally Posted by
KevinWorkman
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?