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?