fixing the size of a JTextField within a GUI - java

I have the following code to create GUI. The issue i am having is that the JTextField appears to expand as the GUI is resized.
I want the JTextField to stay the same size as the GUI is enlarged. The JTextField also seems to reside at the bottom of the control panel rather than at the top. How do i have control of this?
Can someone provide some helo:
public class HtmlDemo extends JPanel
implements ActionListener {
JLabel theLabel;
JTextArea htmlTextArea;
private boolean bLastName = false;
private boolean bNickName = false;
private boolean bMarks = false;
private JFreeChart chart;
private ChartPanel chartPanel;
private JLabel date1Label, date2Label,date3Label,date4Label;
private JTextField date1Field, date2Field,date3Field,date4Field;
private JButton button,button1;
private Container window;
// public createChart stackedChart = new createChart();
public XYPlot plot1;
private JPanel panel, panel1,panel3,panel4;
public HtmlDemo() {
setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
chart = createChart(null);
chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(1060, 370));
chartPanel.setMouseZoomable(true, false);
//panel4.add(chartPanel);
htmlTextArea = new JTextArea(10, 20);
//htmlTextArea.setText(initialText);
JScrollPane scrollPane = new JScrollPane(chartPanel);
theLabel = new JLabel() {
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
public Dimension getMinimumSize() {
return new Dimension(200, 200);
}
public Dimension getMaximumSize() {
return new Dimension(200, 200);
}
};
theLabel.setVerticalAlignment(SwingConstants.CENTER);
theLabel.setHorizontalAlignment(SwingConstants.CENTER);
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
leftPanel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder(
"Rolling Demand"),
BorderFactory.createEmptyBorder(10,10,10,10)));
leftPanel.add(scrollPane);
leftPanel.add(Box.createRigidArea(new Dimension(0,10)));
//leftPanel.add(changeTheLabel);
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.PAGE_AXIS));
rightPanel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Controls"),
BorderFactory.createEmptyBorder(10,10,10,10)));
rightPanel.add(theLabel);
button = new JButton();
date1Label = new JLabel("Enter Start Date");
rightPanel.add(date1Label);
date1Field = new JTextField(5);
rightPanel.add(date1Field);
date2Label = new JLabel("Enter End Date");
rightPanel.add(date2Label);
date2Field = new JTextField(5);
rightPanel.add(date2Field);
panel1 = new JPanel();
date3Label = new JLabel("Enter comparator Start Date");
rightPanel.add(date3Label);
date3Field = new JTextField(5);
rightPanel.add(date3Field);
date4Label = new JLabel("Enter comparator End Date");
rightPanel.add(date4Label);
date4Field = new JTextField(5);
rightPanel.add(date4Field);
button.setText("Get Data");
rightPanel.add(button);
setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
add(leftPanel);
add(Box.createRigidArea(new Dimension(10,0)));
add(rightPanel);
}
private JFreeChart createChart(final XYDataset dataset)
{
// System.out.println("hello");
return ChartFactory.createTimeSeriesChart(
"Test",
"Seconds",
"Value",
dataset,
false,
false,
false);
}
//React to the user pushing the Change button.
public void actionPerformed(ActionEvent e) {
theLabel.setText(htmlTextArea.getText());
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("HtmlDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add content to the window.
frame.add(new HtmlDemo());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}

A Layout controls what gets put where depending on the size. As long as you have a layout, it will always resize. Most of the time, you want this behavior to happen, but to disable simply put
parentContainer.setLayout(null);
This is called a null layout. Note I put parentContainer because you have a bunch of text fields so I don't know which one you want.
Then you can use the textField.setBounds(x, y, width, height) to specify location.
But I do recommend you don't use null layouts.

Related

JButton not responsive - have to click twice

I created a GUI using java swing and in a specific situation, the JButton is unresponsive and I have to click it twice. On the click, it takes the info in the textArea and sends it to a TextParser class to be parsed. If I type more stuff in the area after, and click the evaluateButton, it doesn't respond and I have to click it again to work. Does anyone know if this is a known bug or how I can fix this?
The code for the class is as follows.
/**
* Add the components to the GUI.
* #param pane - the pane for the GUI
*/
public static void addComponentsToPane(Container pane) {
pane.setLayout(new BorderLayout());
JPanel instructionsPanel = new JPanel();
JLabel instructions = new JLabel("Enter the email text below");
instructionsPanel.setBackground(Color.LIGHT_GRAY);
instructionsPanel.add(instructions);
pane.add(instructionsPanel, BorderLayout.NORTH);
JPanel textAreaPanel = new JPanel();
textAreaPanel.setBackground(Color.LIGHT_GRAY);
final JTextArea textArea = new JTextArea();
textArea.setBackground(Color.WHITE);
textArea.setMinimumSize(new Dimension(400,350));
textArea.setMaximumSize(new Dimension(400,350));
textArea.setPreferredSize(new Dimension(400,350));
textArea.setLineWrap(true);
Border border = BorderFactory.createLineBorder(Color.BLACK);
textArea.setBorder(border);
textArea.setMinimumSize(new Dimension(500, 200));
textArea.setFont(new Font("Serif", Font.PLAIN, 16));
textAreaPanel.add(textArea);
pane.add(textAreaPanel, BorderLayout.CENTER);
JPanel scoringPanel = new JPanel();
JButton evaluateButton = new JButton("Evaluate Email");
final JLabel scoreLabel = new JLabel("");
JButton uploadFileBtn = new JButton("Upload File");
JButton importTermsBtn = new JButton("Import Terms");
scoringPanel.add(evaluateButton);
scoringPanel.add(uploadFileBtn);
scoringPanel.add(importTermsBtn);
scoringPanel.add(scoreLabel);
pane.add(scoringPanel, BorderLayout.SOUTH);
evaluateButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
try {
String email = textArea.getText();
TextParser textParser = new TextParser(email);
double score = textParser.parse();
scoreLabel.setText(score+"");
} catch (Exception ex) {
System.out.println(ex);
}
}
});
uploadFileBtn.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
scoreLabel.setText("Feature not yet available.");
}
});
importTermsBtn.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
DatabaseInput d = new DatabaseInput();
d.main(null);
}
});
}
/**
* Create the GUI and show it.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("EmailGUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setLocationRelativeTo(null);
frame.setPreferredSize(new Dimension(500,500));
frame.setTitle("Email Text Input");
frame.setResizable(true);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setSize(screenSize.width, screenSize.height);
//Set up the content pane.
addComponentsToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
My main method just calls createAndShowGUI(). I am new to StackOverflow so if I need to give more or less information in my post please let me know!
As Reimeus and Jason C said in the comments, I should have been using ActionListener which works perfectly.

Effect on JTextfield due to JComboBox

I am using JCombobox and just below that there is a panel which contains JTextFields. Whenever i click on the dropdown (JCombobox) some portion of JTextField disappears itself. I don't know what to do. I am unable to post a pic of the problem here because i am a new user and my reputation is below 10.
public class MainClass {
public static void main(String args[]){
Form form = new Form();
int width = 400;
int height = 400;
form.setSize(width, height);
form.setVisible(true);
form.setTitle("Create Network");
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
form.setLocation((screenSize.width-width)/2, (screenSize.height-height)/2);
}
}
This is the form class
public class Form extends JFrame {
private JLabel ipAddress, networkTopo, numNodes;
private JTextField nodes;
private JButton addIp;
private JButton removeIp;
private JButton next, back;
private JPanel jPanel, jPanel1, jPanel2, commonPanel;
private JComboBox<String> dropDown;
private String[] topologies = { "Grid", "Diagnol Grid", "Bus", "Ring",
"Star" };
public Form() {
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
setResizable(false);
this.getContentPane().setBackground(Color.WHITE);
// add(Box.createRigidArea(new Dimension(0,10)));
GridLayout commonPanelLayout = new GridLayout(0, 2);
commonPanelLayout.setVgap(10);
commonPanel = new JPanel(commonPanelLayout);
commonPanel.setVisible(true);
commonPanel.setAlignmentX(commonPanel.LEFT_ALIGNMENT);
commonPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
commonPanel.setBackground(Color.white);
numNodes = new JLabel("Number of Nodes");
commonPanel.add(numNodes);
nodes = new JTextField(20);
commonPanel.add(nodes);
networkTopo = new JLabel("Network Topology");
commonPanel.add(networkTopo);
dropDown = new JComboBox<String>(topologies);
dropDown.setBackground(Color.WHITE);
commonPanel.add(dropDown);
add(commonPanel);
add(Box.createRigidArea(new Dimension(0, 10)));
ipAddress = new JLabel("IP Addresses");
ipAddress.setAlignmentX(ipAddress.LEFT_ALIGNMENT);
ipAddress.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
add(ipAddress);
add(Box.createRigidArea(new Dimension(0, 10)));
jPanel = new JPanel(new FlowLayout());
jPanel.setVisible(true);
jPanel.setAlignmentX(jPanel.LEFT_ALIGNMENT);
jPanel.setBackground(Color.WHITE);
// jPanel1.setAutoscrolls(true);
add(jPanel);
GridLayout layout = new GridLayout(0, 1);
jPanel1 = new JPanel();
layout.setVgap(10);
// jPanel1.setBackground(Color.WHITE);
jPanel1.setVisible(true);
JScrollPane scroll = new JScrollPane();
scroll.setViewportView(jPanel1);
scroll.setAutoscrolls(true);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setPreferredSize(new Dimension(300, 150));
jPanel1.setPreferredSize(new Dimension(scroll.getPreferredSize().width,
Integer.MAX_VALUE));
jPanel.add(scroll);
jPanel2 = new JPanel(new GridLayout(0, 1, 10, 10));
jPanel2.setBackground(Color.WHITE);
jPanel2.setVisible(true);
jPanel.add(jPanel2);
addIp = new JButton("Add");
jPanel2.add(addIp);
removeIp = new JButton("Remove");
jPanel2.add(removeIp);
JPanel savePanel = new JPanel(new FlowLayout());
savePanel.setVisible(true);
savePanel.setAlignmentX(savePanel.LEFT_ALIGNMENT);
savePanel.setBackground(Color.white);
back = new JButton("Back");
back.setAlignmentX(next.LEFT_ALIGNMENT);
back.setEnabled(false);
savePanel.add(back);
next = new JButton("Next");
next.setAlignmentX(next.LEFT_ALIGNMENT);
savePanel.add(next);
add(savePanel);
AddIPEvent addIPEvent = new AddIPEvent();
addIp.addActionListener(addIPEvent);
RemoveIPEvent removeIPEvent = new RemoveIPEvent();
removeIp.addActionListener(removeIPEvent);
}
public class AddIPEvent implements ActionListener {
public void actionPerformed(ActionEvent e) {
JPanel subPanel = new JPanel(new FlowLayout());
// subPanel.setBackground(Color.WHITE);
subPanel.setVisible(true);
JCheckBox jCheckBox = new JCheckBox();
// jCheckBox.setBackground(Color.WHITE);
subPanel.add(jCheckBox);
JTextField ip = new JTextField(12);
subPanel.add(ip);
JTextField nodesInIp = new JTextField(6);
nodesInIp.setVisible(false);
subPanel.add(nodesInIp);
jPanel1.add(subPanel);
jPanel1.repaint();
jPanel1.revalidate();
}
}
public class RemoveIPEvent implements ActionListener {
public void removeIP(Container container) {
for (Component c : container.getComponents()) {
if (c instanceof JCheckBox) {
if (((JCheckBox) c).isSelected()) {
jPanel1.remove(container);
jPanel.revalidate();
jPanel1.repaint();
}
} else if (c instanceof Container) {
removeIP((Container) c);
}
}
}
public void actionPerformed(ActionEvent e) {
removeIP(jPanel1);
}
}
}
After Clicking on the Add button and then clicking on the JComboBox will make portion of JTextField dissapear. Could someone pls point out the mistake?
Whenever i click on the dropdown (JCombobox) some portion of JTextField disappears itself.
//jPanel1.setPreferredSize(new Dimension(scroll.getPreferredSize().width, Integer.MAX_VALUE));
Don't set the preferred size of components. The layout manager will determine the preferred size as components are added/removed. Then scrollbars will appear as required.
jPanel1.repaint();
jPanel1.revalidate();
The order should be:
jPanel1.revalidate();
jPanel1.repaint();
The revalidate() first invokes the layout manager before it is repainted.
//form.setSize(width, height);
form.pack();
Again, don't try to set the size. Let the layout managers do their jobs. The pack() will size the frame based on the sizes of the components added to the frame.

How to set Bounds and Preferedsize of JTextPane

I'm writing a Java program and I use JSplitPane to split between two JTextPanes.
The bottom JtextPane is in JTabbedPane.
public class test extends JFrame{
private JTextPane commandTextPane = new JTextPane();
private JPanel tokenPanel;
private JTextPane tokenTextPane = new JTextPane();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test window = new test();
window.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public test (){
setBounds(100, 100, 900, 700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
commandTextPane.setBounds(0, 0, 200, 300);
commandTextPane.setPreferredSize(new Dimension(200, 300));
//create ressultTabPane
JTabbedPane resultTabPane = new JTabbedPane();
tokenTabPanel();
resultTabPane.addTab("Token", tokenPanel);
JScrollPane topScroll = new JScrollPane(commandTextPane,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
TextLineNumber topLine = new TextLineNumber(commandTextPane);
topScroll.setRowHeaderView(topLine);
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true,
topScroll, resultTabPane);
splitPane.setOneTouchExpandable(true);
getContentPane().add(splitPane);
setVisible(true);
}
public void tokenTabPanel() {
tokenPanel = new JPanel();
tokenTextPane.setBounds(0, 0, 200, 300);
tokenTextPane.setPreferredSize(new Dimension(200,300));
JScrollPane tokenScroll = new JScrollPane(tokenTextPane,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
TextLineNumber tokenLine = new TextLineNumber(tokenTextPane);
tokenScroll.setRowHeaderView(tokenLine);
tokenPanel.add(tokenScroll);
}
}
The problem is I can't set Bounds and PreferedSize of those JTextPanes. When I set them, the top JTextPane is not expect size and the bottom JTextPane is not in the top-left also its size.
image --> (http://upic.me/show/54858871)
Do you know how to fix them? Sorry for my English.
Thanks in advance.

Resize JPanel to fit new JLabel icon

I'm trying to create an animation (spinning text) by repeatedly changing the icon on a JLabel. The issue is the images are not of the same size, and when they are bigger than the size of the first image, they are clipped.
One way around this is to setPreferredSize for the JLabel so that all images fit - but I imagine there must be a way dinamically resize the JPanel containing the JLabel?
In the code bellow I've also tried removing the JLabel alltogether, creating a new one and then adding the new one, but to the same effect.
public class AnimationPanelv2 extends JPanel{
private JButton start = new JButton("Start Animation");
private JLabel img = new JLabel();
private JTextField animSpeed = new JTextField(10);
private JTextField filePrefix = new JTextField(10);
private JTextField noOfImg = new JTextField(10);
private JTextField audioFile = new JTextField(10);
private Timer timer;
private AudioClip clip;
private ArrayList<ImageIcon> icon = new ArrayList<>();
private int step=0;
public AnimationPanelv2() {
//button is for starting the animation
start.addActionListener(new Animatie());
this.setLayout(new BorderLayout());
add(start, BorderLayout.NORTH);
//showing the label with the first frame
Class metaObj = this.getClass();
URL url = metaObj.getResource("/image/L1.gif");
img.setIcon(new ImageIcon(url));
// img.setPreferredSize(new Dimension(500,550));
add(img, BorderLayout.CENTER);
//control panel
JPanel controls = new JPanel(new GridLayout(4,2));
controls.setBorder(new TitledBorder("Enter information for animation"));
controls.add(new JLabel("Animation speed in ms"));
controls.add(animSpeed);
controls.add(new JLabel("Image file prefix"));
controls.add(filePrefix);
controls.add(new JLabel("Number of images"));
controls.add(noOfImg);
controls.add(new JLabel("Audio file"));
controls.add(audioFile);
//
add(controls, BorderLayout.SOUTH);
}
private class TimerAnimation implements ActionListener {
public void actionPerformed(ActionEvent e) {
remove(img);
img = new JLabel(icon.get(step++));
img.setVisible(true);
add(img, BorderLayout.CENTER);
// img.revalidate();
// img.repaint();
validate();
repaint();
updateUI();
if (step==Integer.parseInt(noOfImg.getText())) step=0;
}
}
private class Animatie implements ActionListener {
public void actionPerformed(ActionEvent e) {
//getting data from the text fields
int ms = Integer.parseInt(animSpeed.getText());
String s = filePrefix.getText();
int nr = Integer.parseInt(noOfImg.getText());
String audioFilePath = audioFile.getText();
// clip
Class metaObj = this.getClass();
URL url = metaObj.getResource("/audio/"+audioFilePath);
clip = Applet.newAudioClip(url);
//image loading
for (int i=1; i<=nr; i++){
url = metaObj.getResource("/image/"+s+i+".gif");
System.out.println("/image/"+s+i+".gif");
icon.add(new ImageIcon(url));
}
//timer
timer = new Timer(ms, new TimerAnimation());
timer.start();
clip.loop();
}
}
public static void main(String[] args) {
JFrame jf = new JFrame("This class test");
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(new AnimationPanelv2());
jf.pack();
jf.setVisible(true);
}
}
This whole panel will be used in an applet.
This is a screenshot: http://screencast.com/t/UmqQFZHJVy
The images that are supposed to be the frames, should be located in an /images/ sub-directory and if the user enters n for the number of frames and F for the image
prefix, then the files are F1, F2, and so on, to Fn (GIFs). The sound file should be in an /audio/ sub-directory, and the entire file name is given by the user.
You can try to create list of JLabels for each image, add them to a panel with CardLayout and swap cards.
Okay well a JLabel should size automatically to its given content, so to solve JPanel issue, simply override getPreferredSize() of JPanel containing the JLabel and return Dimensions according to the JLabel size.
public class MyPanel extends JPanel {
JLabel label=...;
#Override
public Dimension getPreferredSize() {
return new Dimension(label.getWidth(),label.getHeight());
}
}
also dont forget when you change Icon of JLabel call revalidate() and repaint() on JPanel instance in order for size changes to refelect.

Adding ScrollPane to JTextArea

I am working on a project for my college course. I was just wondering if anyone knew how to add a scrollBar to a JTextArea. At present I have the GUI laid out correctly, the only thing missing is the scroll bar.
This is what the GUI looks like. As you can see on the second TextArea I would like to add the Scrollbar.
This is my code where I create the pane. But nothing seems to happen... t2 is the JTextArea I want to add it to.
scroll = new JScrollPane(t2);
scroll.setBounds(10,60,780,500);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
Any help would be great, thanks!
The Scroll Bar comes when your text goes beyond the bounds of your view area. Don't use Absolute Positioning, for such a small talk at hand, always prefer Layout Managers, do read the first para of the first link, to know the advantage of using a Layout Manager.
What you simply need to do is use this thingy :
JTextArea msgArea = new JTextArea(10, 10);
msgArea.setWrapStyleWord(true);
msgArea.setLineWrap(true);
JScrollPane msgScroller = new JScrollPane();
msgScroller.setBorder(
BorderFactory.createTitledBorder("Messages"));
msgScroller.setViewportView(msgArea);
panelObject.add(msgScroller);
Here is a small program for your understanding :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JTextAreaScroller
{
private JTextArea msgArea;
private JScrollPane msgScroller;
private JTextArea logArea;
private JScrollPane logScroller;
private JButton sendButton;
private JButton terminateButton;
private Timer timer;
private int counter = 0;
private String[] messages = {
"Hello there\n",
"How you doing ?\n",
"This is a very long text that might won't fit in a single line :-)\n",
"Okay just to occupy more space, it's another line.\n",
"Don't read too much of the messages, instead work on the solution.\n",
"Byee byee :-)\n",
"Cheers\n"
};
private ActionListener timerAction = new ActionListener()
{
#Override
public void actionPerformed(ActionEvent ae)
{
if (counter < messages.length)
msgArea.append(messages[counter++]);
else
counter = 0;
}
};
private void displayGUI()
{
JFrame frame = new JFrame("Chat Messenger Dummy");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(5, 5));
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(0, 1, 5, 5));
logArea = new JTextArea(10, 10);
logArea.setWrapStyleWord(true);
logArea.setLineWrap(true);
logScroller = new JScrollPane();
logScroller.setBorder(
BorderFactory.createTitledBorder("Chat Log"));
logScroller.setViewportView(logArea);
msgArea = new JTextArea(10, 10);
msgArea.setWrapStyleWord(true);
msgArea.setLineWrap(true);
msgScroller = new JScrollPane();
msgScroller.setBorder(
BorderFactory.createTitledBorder("Messages"));
msgScroller.setViewportView(msgArea);
centerPanel.add(logScroller);
centerPanel.add(msgScroller);
JPanel bottomPanel = new JPanel();
terminateButton = new JButton("Terminate Session");
terminateButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent ae)
{
if (timer.isRunning())
timer.stop();
else
timer.start();
}
});
sendButton = new JButton("Send");
bottomPanel.add(terminateButton);
bottomPanel.add(sendButton);
contentPane.add(centerPanel, BorderLayout.CENTER);
contentPane.add(bottomPanel, BorderLayout.PAGE_END);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
timer = new Timer(1000, timerAction);
timer.start();
}
public static void main(String... args)
{
EventQueue.invokeLater(new Runnable()
{
#Override
public void run()
{
new JTextAreaScroller().displayGUI();
}
});
}
}
Here is the outcome of the same :
The scroll bar by default will only be shown when the content overfills the available viewable area
You can change this via the JScrollPane#setVerticalScrollBarPolicy method, passing it ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS

Categories