The java application I'm building utilises the Swing framework. I'm new to Swing, and I'm trying to display some text from a file. The problem is that not all my text is displayed using the code below - it is cut off.
The code
So far, I've created a JFrame component to contain everything:
//initial frame that holds everything
frame = new JFrame(WINDOW_NAME);
frame.setSize(new Dimension((SCREEN_WIDTH.intValue()/100)*80, (SCREEN_HEIGHT.intValue()/100)*80));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Within that, I've created a scroll pane to allow the user to read through the contents of the text file by scrolling vertically; it's quite a large text file - a few chapters from a book.
JScrollPane scrollPane = new JScrollPane(contentPane);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setWheelScrollingEnabled(true);
frame.setContentPane(scrollPane);
The scroll pane contains a JPanel object. I've utilised the GridBagLayout layout manager for this. The panel itself holds an EditorPane which I've instantiated with the text file's URL.
JPanel contentPane = new JPanel();
contentPane.setPreferredSize(new Dimension(frame.getWidth(), frame.getHeight()));
buildAndAddComponentsToFrame(contentPane);
private void buildAndAddComponentsToFrame(Container container) {
container.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 0;
c.weightx = 1.0;
c.weighty = 1.0;
c.insets = new Insets((frame.getHeight()/100)*10, (frame.getWidth()/100)*10, (frame.getHeight()/100)*10, (frame.getWidth()/100)*10);
//create a non-editable editor pane to hold the contents of the religious texts
JEditorPane textContainer = new JEditorPane();
textContainer.setEditable(false);
//TODO load the content of the pane from a URL (local)
File textFile = new File(*my text file*);
try {
textContainer.setPage(textFile.toURI().toURL());
} catch (IOException e) {
e.printStackTrace();
}
container.add(textContainer,c);
}
This works - I'm able to read the contents of the file but not in its entirety: it only shows what it can fit within the height of the JPanel object. I don't want to place the text content within a ScrollPane - but make the JPanel's height be relative to the contents of the text file.
Is there any way to achieve this? I've used the container to make the "text area" appear like a microsoft word document hence the insets - it looks like a paper document in the middle of the jframe - this is intentional.
Thanks for any help - sorry if my explanation was a bit vague. I've added a screenshot to help explain.
You're creating a JPanel, using default FlowLayout, you're forcing its preferred size to some value, and then wondering why it won't get bigger when the JTextPane it holds has text greater than it's size. This is not happening for the mistakes that you're making above.
Suggestions: