I'm trying to create a character counter in java, and I've got my core part of my code working. However, I'm having an issue with my GUI. When I press enter in my JTextArea field, it expands it, but it does not also expand my GUI. This leads to my textfield expanding vertically to where it covers all my button and character count. How can I make my GUI expand along with my JTextArea? Thank you :).
Note: apologies for the hyperlinks to pictures, I'm still new to this site so I don't have enough reputation to upload pictures.
This is how it looks with one line of text in my text area (works well with GUI)
This is how it looks with more than one line of text(or just a few "enter" key presses) in my text area(doesn't work well with GUI)
My code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class characterCounterTwov4{
//creates a new Jframe to put our frame objets in
JFrame frame = new JFrame();
//creates a text field JTextArea frame object
JTextArea txtField = new JTextArea();
//stores the string of the the jtextfield into a variable text
String text = txtField.getText();
//creates a text field that is uneditable with the word "characters"
String charString = "Characters: ";
JTextField charField = new JTextField(charString, 25);
//string that will be used in a text field to display the # of chars
String charCount = Integer.toString(text.length());
//Text field that displays charCount
JTextField charFieldTwo = new JTextField(charCount, 10);
public characterCounterTwov4(){
//disables the charField from being editable.
charField.setEditable(false);
//calculate button
JButton calcButton = new JButton("Calculate");
calcButton.addActionListener(new ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent event)
{
System.out.println("button pressed");
//stores the string of the the jtextfield into a variable text
text = txtField.getText();
//string that will be used in a text field to display the # of chars
String charCount = Integer.toString(text.length());
//Text field that displays charCount
charFieldTwo.setText(charCount);
}
});
/*******************/
/* Frame Setup */
/*******************/
//sets the layout of the frame
frame.setLayout(new BorderLayout());
//add's elements to the frame
frame.add(txtField, BorderLayout.NORTH);
frame.add(charField, BorderLayout.CENTER);
frame.add(charFieldTwo, BorderLayout.SOUTH);
frame.add(calcButton, BorderLayout.EAST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args){
new characterCounterTwov4();
System.out.println("End of program. Should not get here");
}
}
Put the JTextArea
in a JScrollPane
See How to Use Scroll Panes for more details.
Specify the row and column size via the JTextArea
's constructor, which will allow you to specify the desired, viewable area of the text area, before the scroll pane has to intervene and add scroll bars.
See JTextArea
constructor JTextArea(int rows, int columns)
for more details
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.