Keep textArea size after switching the tap - java

I have a simple GUI with multiple tabs like that:
GUI
The problem is that, after filling the text area up to the bottom (filled console) and switching the tab - it completely breaks the markup (bottom-broken)
Here is the code example of the textArea and Constraints
JTextArea textArea = new JTextArea();
textArea.setEditable(false);
tabbedPanelOne.add(textArea);
CustomAppender.setTextArea(textArea);
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridy++;
gbc.gridx = 0;
gbc.gridwidth = 5;
gbc.weightx = 1.0;
gbc.weighty = 10.0;
Font font = textArea.getFont();
float size = font.getSize() - 4.0f;
textArea.setFont(font.deriveFont(size));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
//textArea.setPreferredSize(new Dimension(500, 300));
layout.setConstraints(textArea, gbc);
/*
* Scrolls for TextArea
*/
JScrollPane scroll = new JScrollPane (textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
tabbedPanelOne.add(scroll);
layout.setConstraints(scroll, gbc);
//textArea.setPreferredSize(new Dimension(500, 300)); works fine, but it disables vertical scroller.

layout.setConstraints(textArea, gbc);
First of all when add a component to a scrollpane you would never attempt to set constraints on the component. A JScrollPane uses its own custom layout manager so you don't do anything special when you add the component to the viewport of the scroll pane.
However, the simplest solution would be to use a BorderLayout on the panel you add to the tab. Something like:
JPanel top = new JPanel();
top.add(...);
JTextArea textArea = new JTextArea(5, 20);
JScrollPane scrollPane = new JScrollPane( textArea );
JPanel main = new JPanel( new BorderLayout() );
main.add(top, BorderLayout.PAGE_START);
main.add(scrollPane, BorderLayout.CENTER);
Now the scroll pane will take up all the available space in the tab and scrollbars will appear when needed.

Related

JTextField no size?

Currently my JTextField component is sizing to what appears to be 0. I have attempted to setPreferredSize, setSize(new Dimension), added columns & removed the anchor with GridBagConstraints. Nothing has worked thus far. Here is my code:
private void createPayFrame() {
JFrame payFrame = new JFrame();
payFrame.setSize(new Dimension(450, 300));
payFrame.setLocationRelativeTo(null);
payFrame.setLayout(new GridBagLayout());
JLabel payment = new JLabel("<html>Your bill has been generated. It is located in your "
+ System.getProperty("user.home") + "\\eclipse-workspace\\Turbo Team directory</html>");
GridBagConstraints gbc = new GridBagConstraints();
JTextField payField = new JTextField(10);
payField.setToolTipText("Enter in the amount due here");
gbc.anchor = GridBagConstraints.ABOVE_BASELINE;
gbc.insets = new Insets(0,0,20,0);
payFrame.add(payment, gbc);
gbc.gridy = 1;
gbc.insets = new Insets(10,0,0,0);
payFrame.add(payField, gbc);
payFrame.setVisible(true);
}
This is what it looks like when I run the code:
Any idea what is going on here?
Set the fill for the textfield
gbc.fill = GridBagConstraints.HORIZONTAL;
I've had this problem with GridBagLayout when the JTextField was too large to display. Have you tried with less columns? Maybe try to start at new JTextField(1) and work your way up to see if it works at smaller lengths.

How to make JTexArea fit the window

I have created JTextArea inserted in JScrollPane inside JPanel which is inside the JFrame.
I had to put exact number of colls and rows in the JTextArea's constructor, if didn't the the JTextArea was not visible, but when I resize the window the JTextArea has fixed size and I don't want it. I would like it to fix the Window. How to do it? Thank you.
jpMiddle = new JPanel();
JTextArea ta = new JTextArea(20,42);
JScrollPane sp = new JScrollPane(
ta, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
jpMiddle.add(sp);
EDIT : Figured it out myself:
jpMiddle = new JPanel(new GridLayout(1,1));
JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(
ta, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
jpMiddle.add(sp);

JScrollPane Scrolling Horizontally when it Should be Scrolling Vertically

I have a panel with FlowLayout specified that is full of labels, text fields, and text areas. The frame isn't that wide, but the panel becomes tall because of all the components inside. I want to add the panel to a JScrollPane, so I can scroll vertically through the panel. However, when I add the panel to the scroll pane and add the scroll pane to the frame, all the components are right next to each other and it scrolls horizontally through them. Here's the code:
public class Form {
JTextField jtfName = new JTextField(15);
JTextField jtfTitle = new JTextField(15);
JTextField jtfAuthor = new JTextField(15);
JTextArea jtaSetting = new JTextArea(5, 15);
JTextArea jtaMainChars = new JTextArea(5, 15);
JTextArea jtaConflict = new JTextArea(5, 15);
JTextArea jtaQuote = new JTextArea(5, 15);
JTextArea jtaMainCharShows = new JTextArea(5, 15);
JPanel jpnlName = new JPanel();
JPanel jpnlTitle = new JPanel();
JPanel jpnlAuthor = new JPanel();
Form() {
// Create a new JFrame container.
JFrame jfrm = new JFrame("Organizer");
jfrm.setLayout(new GridLayout(0,1));
// Give the frame an initial size.
jfrm.setSize(300, 300);
// Terminate the program when the user closes the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a panel.
JPanel jpnl = new JPanel();
// Create labels.
JLabel jlabName = new JLabel("Student Name:");
JLabel jlabTitle = new JLabel("Title:");
JLabel jlabAuthor = new JLabel("Author:");
JLabel jlabSetting = new JLabel("Setting (Time and Place):");
jlabSetting.setHorizontalAlignment(SwingUtilities.CENTER);
JLabel jlabMainChars = new JLabel("Main Characters:");
jlabMainChars.setHorizontalAlignment(SwingUtilities.CENTER);
JLabel jlabConflict = new JLabel("<html>Describe the major conflict of the<br>story in one well-written sentence:");
jlabConflict.setHorizontalAlignment(SwingUtilities.CENTER);
JLabel jlabQuote = new JLabel("<html>Find and write down a passage (quote<br>from the book that reveals a significant<br>personality trait of one of the main characters<br>and GIVE THE PAGE #:");
jlabQuote.setHorizontalAlignment(SwingUtilities.CENTER);
JLabel jlabMainCharShows = new JLabel("<html>Explain in your own words what the<br>passage (quote) shows about the main character.");
jlabMainCharShows.setHorizontalAlignment(SwingUtilities.CENTER);
// Add text fields to panel.
jpnlName.add(jlabName);
jpnlName.add(jtfName);
jpnlTitle.add(jlabTitle);
jpnlTitle.add(jtfTitle);
jpnlAuthor.add(jlabAuthor);
jpnlAuthor.add(jtfAuthor);
// Add components to main panel.
jpnl.add(jpnlName);
jpnl.add(jpnlTitle);
jpnl.add(jpnlAuthor);
jpnl.add(jlabSetting);
jpnl.add(jtaSetting);
jpnl.add(jlabMainChars);
jpnl.add(jtaMainChars);
jpnl.add(jlabConflict);
jpnl.add(jtaConflict);
jpnl.add(jlabQuote);
jpnl.add(jtaQuote);
jpnl.add(jlabMainCharShows);
jpnl.add(jtaMainCharShows);
// Add the panel to a scroll pane.
JScrollPane jspPanel = new JScrollPane(jpnl);
// Add the scroll pane to the frame.
jfrm.getContentPane().add(jspPanel);
// Display the frame.
jfrm.setVisible(true);
}
}
Without know exactly the layout you want, the best solution I can suggest is to try the WrapLayout
It addresses the major problem with the FlowLayout, it doesn't wrap.
Why don't you try BoxLayout:
jpnl.setLayout(new BoxLayout(jpnl, BoxLayout.PAGE_AXIS));

trying to create a scroll pane for text area

I'm trying to create a scroll pane for my jtextarea.
Below is my code snippet. I wonder why it didn't work. Can someone provide me some insights? Thanks.
JTextArea textArea = new JTextArea(st);
textArea.setBounds(145, 51, 327, 53);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
//ScrollPane Code
JScrollPane areaScrollPane = new JScrollPane(textArea);
areaScrollPane.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
areaScrollPane.setPreferredSize(new Dimension(250, 250));
panel.add(textArea);
Did I miss something? There is no scrollpane appear in my textarea.
You're adding textArea to panel instead of areaScrollPane. Try
panel.add(areaScrollPane);

Java: Text not wrapping in JFrame dialog

I'm using a JFrame to present a message box with some text and 2 buttons. How do I get the text to wrap automatically based on the size of the box?
Here's my current code:
dialogFrame = new JFrame();
JButton newButton = new JButton("New");
newButton.addActionListener(new newUploadAction());
JButton resumeButton = new JButton("Resume");
resumeButton.addActionListener(new resumeUploadAction());
//dialogFrame.setUndecorated(true);
JPanel addPanel = new JPanel();
JPanel addPanel2 = new JPanel();
addPanel.add(newButton);
addPanel.add(resumeButton);
String text = "<html><p>A previous control file exists for this file. ";
text += "Would you like to initiate a new transfer or resume the previous one?</p></html>";
JLabel testLabel = new JLabel(text);
//testLabel.setPreferredSize(new Dimension(1, 1));
addPanel2.add(testLabel);
Container content = dialogFrame.getContentPane();
//content.setBackground(Color.white);
content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
content.add(addPanel2);
content.add(addPanel);
dialogFrame.setSize(200,200);
dialogFrame.setVisible(true);
dialogFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
I read somewhere that calling
testLabel.setPreferredSize(new Dimension(1, 1));
would cause the wrapping behavior I want, but that just resulted in the text not showing up at all.
You could place the text in a JTextArea and call setLineWrap(true) and setWrapStyleWord(true) on the JTextArea. If you want it to look more JLabel-ish, then just change the color settings of the JTextArea to your liking.
EDIT 1
Also another thing that could work: consider having the holding JPanel use a BorderLayout:
JPanel addPanel2 = new JPanel(new BorderLayout()); //!! added BorderLayout
So that the JLabel added will fill the addPanel2.

Categories

Resources