limit lines displayed in jscrollpanel - java

I have a JTextArea wrapped in a JScrollPane, which I use to log my application's output.
As the application produces many lines, the number of lines in the text area is growing really fast and the scroll becomes nearly invisible.
I'd like to have a text area like the Eclipse console.. I mean.. a text area with a vertical scroll but when using the scroll I can only show at maximum the latest 200 rows.
Here is the runnable code I'm using:
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import javax.swing.text.DefaultCaret;
public class WindowLog extends JFrame {;
private JPanel contentPane;
private static JTextArea textArea = new JTextArea();
public static void run() {
try {
WindowLog frame = new WindowLog();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void writeText(JTextArea ta, String s){
DefaultCaret caret = (DefaultCaret)ta.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
ta.append(s);
}
/**
* Create the panel.
*/
public WindowLog() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 523, 299);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
contentPane.setLayout(null);
JLabel lblLog = new JLabel("Log");
lblLog.setBounds(5, 5, 497, 14);
getContentPane().add(lblLog);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(5, 30, 492, 138);
//scrollPane.getViewport().setPreferredSize(new Dimension(300, 100));
textArea.setBorder(BorderFactory.createEmptyBorder(0, 6, 0, 6));
contentPane.add(scrollPane);
scrollPane.setViewportView(textArea);
Main.setTextProva(textArea);
}
}
and here is Main class:
public class Main {
static JTextArea textProva;
public static void setTextProva(JTextArea textProva) {
Main.textProva = textProva;
}
public static void main(String[] args) {
WindowLog.run();
System.out.println(textProva);
WindowLog.writeText(textProva, "hello"+"\n");
}
Thanks in advance.

Add a DocumentFilter to the document of the text area. In the filter check how many rows are there. If the max count is achieved remove previous content from the text area.

You can use
txt.setRows(int rows);
txt.setColumns(int cols)
by this you can control horizontal, vertical size.
or further reference you can read Oracle documentation

Related

Long text scroll in JFrame

I am building this program and I would like to have a square area that is scrollable since the text is long. I've tried some methods but it seems to cut out the text out of the length. Could anyone give me a tip on how to do this?
package gerador;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.Scrollable;
public class frameMyPasswords extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frameMyPasswords frame = new frameMyPasswords();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public frameMyPasswords() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JTextArea txtField = new JTextArea();
txtField.setLineWrap(true);
txtField.setEditable(false);
txtField.setToolTipText("");
txtField.setBounds(10, 38, 419, 144);
txtField.setText("LONG TEXT");
contentPane.add(txtField);
}
}

How can I add a scroll bar to a text area?

Please, anyone, tell me how to add the scrollbar to a JTextArea. I tried out many things. but still not able to get it. I copied some codes related to the text area.
public class main extends JPanel {
private JTextArea jcomp1;
public main() {
jcomp1 = new JTextArea(5, 5);
setPreferredSize(new Dimension(944, 574));
// setPreferredSize (new Dimension (1024, 1080));
setLayout(null);
//add components
add(jcomp1);
jcomp1.setBounds(110, 165, 330, 300);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Paraphrasing Tool");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new main());
frame.pack();
frame.setVisible(true);
}
}
Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Netbeans section.
As Andrew said, you have to place the JTextArea inside of a JScrollPane, then place the JScrollPane inside of a JPanel with a Swing layout. I used a BorderLayout.
Here's the GUI after I typed some lines.
Here's the complete runnable code.
import java.awt.BorderLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class JTextAreaExample extends JPanel {
private static final long serialVersionUID = 1L;
private JTextArea jcomp1;
public JTextAreaExample() {
this.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
this.setLayout(new BorderLayout());
jcomp1 = new JTextArea(5, 30);
jcomp1.setMargin(new Insets(5, 5, 5, 5));
JScrollPane scrollPane = new JScrollPane(jcomp1);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
add(scrollPane);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Paraphrasing Tool");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JTextAreaExample(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
});
}
}
Add the text area to (the viewport of) a JScrollPane. The easiest way is to add it in the constructor. Then add the scroll pane to a panel with a layout (in a GUI that uses layouts).

Adding JButton using loop (for) does nothing

I'm trying to add various JButtons to a JPanel using "for" but doesn't work. No compilation or other errors. Buttons just won't appear.
A bit of context, I create an ArryList in another class ("GestorFrigo") that gets data from DataBase, this works fine, the array has all the data and there's no problem getting back the data from the array.
This is my code: Thanks in advance.
import gestor.GestorFrigo;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JScrollBar;
#SuppressWarnings("serial")
public class VentanaInterior extends JFrame {
private JPanel contentPane;
private JButton btnPerfiles;
private JButton btnAadir;
private JButton btnRecetas;
private JScrollBar scrollBar;
private GestorFrigo frigo;
private ArrayList<JButton> botones;
private ArrayList<Object> boton;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
VentanaInterior frame = new VentanaInterior();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public VentanaInterior() {
//Componentes
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 556, 363);
setLocationRelativeTo(null);
setTitle("Tu Frigorífico Inteligente");
setIconImage(new ImageIcon(getClass().getResource("img/logo.png")).getImage());
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
btnPerfiles = new JButton("Perfiles");
btnPerfiles.setBounds(0, 302, 96, 23);
contentPane.add(btnPerfiles);
btnAadir = new JButton("A\u00F1adir alimento");
btnAadir.setBounds(369, 302, 148, 23);
contentPane.add(btnAadir);
btnRecetas = new JButton("Recetas");
btnRecetas.setBounds(96, 302, 103, 23);
contentPane.add(btnRecetas);
scrollBar = new JScrollBar();
scrollBar.setBounds(523, 0, 17, 325);
contentPane.add(scrollBar);
JButton btnQueso = new JButton();
btnQueso.setBounds(24, 35, 62, 61);
contentPane.add(btnQueso);
frigo = new GestorFrigo(); //creamos el gestor
String imagen = frigo.getArray().get(1).getImagen(); //cogemos la imagen asociada al alimento
btnQueso.setIcon(new ImageIcon("src/img/"+imagen));
for(int i=0;i<frigo.getArray().size();i++){
JButton boton = new JButton();
String imagen2 = frigo.getArray().get(i).getImagen();
boton.setIcon(new ImageIcon("src/img/"+imagen2));
contentPane.add(boton);
}
}
}
Don't use a null layout.
By default your buttons have a default size of (0, 0) so there is nothing to paint.
User a layout manager, probably a GridLayout, and the layout manager will determine the size and location of each button for you.
Read the section from the Swing tutorial o Using Layout Managers for more information and working examples.

How do I left and right align components in Swing?

I have what appears to be a simple issue. I have some labels that I'd like to align to the left but when I resize, they start to drift towards the middle. This is going to throw off the alignment of other components I plan on adding. What do I do to keep them to the left?
It's short, easy code, not sure what my problem is here:
package com.protocase.notes.views;
import com.protocase.notes.model.Note;
import com.protocase.notes.model.User;
import java.awt.Component;
import java.util.Date;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.BevelBorder;
/**
* #author dah01
*/
public class NotesPanel extends JPanel{
public NotesPanel(Note note){
this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
JLabel creatorLabel = new JLabel("Note by "+note.getCreator()+ " # "+note.getDateCreated());
creatorLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
creatorLabel.setHorizontalAlignment(JLabel.LEFT);
JTextArea notesContentsArea = new JTextArea(note.getContents());
notesContentsArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(notesContentsArea);
JLabel editorLabel = new JLabel(" -- Last edited by "+note.getLastEdited() +" at "+note.getDateModified());
editorLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
editorLabel.setHorizontalAlignment(JLabel.LEFT);
this.add(creatorLabel);
this.add(scrollPane);
this.add(editorLabel);
this.setBorder(new BevelBorder(BevelBorder.RAISED));
}
public static void main(String[] args) {
JFrame frame = new JFrame("Notes Panel");
Note note = new Note();
User user = new User();
user.setFirstName("d");
user.setLastName("h");
user.setUserID("dah01");
note.setCreator(user);
note.setLastEdited(user);
note.setDateCreated(new Date());
note.setDateModified(new Date());
note.setContents("A TEST CONTENTS");
NotesPanel np = new NotesPanel(note);
JScrollPane scroll = new JScrollPane(np);
frame.setContentPane(scroll);
np.setVisible(true);
frame.setVisible(true);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
If you want to align things in your panel, you have to align everything. You forgot to align your JScrollPane. If you add this line to your code, the alignment should be fixed for you:
scrollPane.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);
And what your new constructor would look like:
public NotesPanel(Note note){
this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
JLabel creatorLabel = new JLabel("Note by "+note.getCreator()+ " # "+note.getDateCreated());
creatorLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
creatorLabel.setHorizontalAlignment(JLabel.LEFT);
JTextArea notesContentsArea = new JTextArea(note.getContents());
notesContentsArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(notesContentsArea);
scrollPane.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);
JLabel editorLabel = new JLabel(" -- Last edited by "+note.getLastEdited() +" at "+note.getDateModified());
editorLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
editorLabel.setHorizontalAlignment(JLabel.LEFT);
this.add(creatorLabel);
this.add(scrollPane);
this.add(editorLabel);
this.setBorder(new BevelBorder(BevelBorder.RAISED));
}
As per your comments, I would recommend you to go with some other layout, I would recommend MigLayout
Here you go with MigLayout:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import javax.swing.JTextArea;
public class MigLayoutDemo {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new MigLayoutDemo();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MigLayoutDemo() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
frame.setContentPane(contentPane);
contentPane.setLayout(new MigLayout("", "[grow]", "[][grow][]"));
JLabel lblLabel = new JLabel("Label 1");
lblLabel.setBorder(BorderFactory.createLineBorder(Color.red));
contentPane.add(lblLabel, "cell 0 0,alignx left");
JTextArea textArea = new JTextArea();
contentPane.add(textArea, "cell 0 1,grow");
JLabel lblLabel_1 = new JLabel("Label 2");
lblLabel_1.setBorder(BorderFactory.createLineBorder(Color.red));
contentPane.add(lblLabel_1, "cell 0 2,alignx left");
frame.setVisible(true);
}
}
OUTPUT :
As you can see labels marked with red border are not stretched to the middle, they are left aligned .
Using a BorderLayout definitely fixes your issue.
this.setLayout(new BorderLayout());
this.add(creatorLabel, BorderLayout.NORTH);
this.add(scrollPane);
this.add(editorLabel, BorderLayout.SOUTH);
Alternatively, if you have more components to display in the UI than what you show in the sample code, you can still use a GridBagLayout. I know that not many people like to use this one because it's quite verbose but in my opinion, it's the most powerful layout manager of swing.

MigLayout resizing with JScrollPane

I searched a little bit and did not find a good answer to my problem.
I am working on a gui that has to be resizable. It contains a status JTextArea that is inside a JScrollPane. And this is my problem. As long as I don't manually resize my JFrame, the "initial" layout is kept and everything looks fine. As soon as I manually resize (if the JTextArea is already in scrolled mode), the layout gets messed up.
Here is a SSCCE (I got rid of most of the parts while keeping the structure of the code. I hope it's more readable that way):
import java.awt.Color;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JSlider;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import net.miginfocom.swing.MigLayout;
public class Tab extends JFrame {
private static final long serialVersionUID = 1L;
private JTextArea messageTextArea;
private JPanel optionPanel, messagePanel;
private JTabbedPane plotTabPane;
public static void main(String[] args) {
final Tab tab = new Tab();
tab.setSize(1000, 600);
tab.setVisible(true);
new Thread(new Runnable() {
#Override
public void run() {
int count = 0;
tab.printRawMessage("start");
while (true) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {}
tab.printRawMessage("\ntestMessage" + count++);
}
}
}).start();
}
public Tab() {
super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initComponents();
}
private void initComponents() {
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new MigLayout("insets 0", "", ""));
mainPanel.add(getLeftTopPanel(), "shrinky, top, w 450!");
mainPanel.add(getRightPanel(), "spany 5, wrap, grow, pushx, wmin 400");
mainPanel.add(getMessagePanel(), "pushy, growy, w 450!");
JScrollPane contentScrollPane = new JScrollPane(mainPanel);
contentScrollPane.setBorder(BorderFactory.createEmptyBorder());
setContentPane(contentScrollPane);
}
protected JPanel getLeftTopPanel() {
if (optionPanel == null) {
optionPanel = new JPanel();
optionPanel.setBorder(BorderFactory.createTitledBorder(null, "Configuration", TitledBorder.LEFT, TitledBorder.TOP, new Font("null", Font.BOLD, 12), Color.BLUE));
optionPanel.setLayout(new MigLayout("insets 0", "", "top, align 50%"));
JLabel label = new JLabel("Choose");
label.setHorizontalAlignment(JLabel.RIGHT);
optionPanel.add(label, "w 65!");
optionPanel.add(new JSeparator(JSeparator.VERTICAL), "spany 5, growy, w 2!");
optionPanel.add(new JComboBox(new String[] {"option1", "option2", "option3"}), "span, growx, wrap");
optionPanel.add(new JLabel("Type"), "right");
optionPanel.add(new JTextField("3"), "w 65!, split 2");
optionPanel.add(new JLabel("Unit"), "wrap");
optionPanel.add(new JLabel("Slide"), "right");
optionPanel.add(new JSlider(0, 100), "span, growx, wrap");
}
return optionPanel;
}
protected JTabbedPane getRightPanel() {
if (plotTabPane == null) {
plotTabPane = new JTabbedPane();
plotTabPane.add("Tab1", new JPanel());
plotTabPane.add("Tab2", new JPanel());
}
return plotTabPane;
}
protected JPanel getMessagePanel() {
if (messagePanel == null) {
messagePanel = new JPanel();
messagePanel.setBorder(BorderFactory.createTitledBorder(null, "Status Console", TitledBorder.LEFT, TitledBorder.TOP, new Font("null", Font.BOLD, 12), Color.BLUE));
messagePanel.setLayout(new MigLayout("insets 0", "", "top, align 50%"));
messagePanel.add(new JScrollPane(getMessageTextArea()), "push, grow");
}
return messagePanel;
}
protected JTextArea getMessageTextArea() {
if (messageTextArea == null) {
messageTextArea = new JTextArea();
messageTextArea.setEditable(false);
messageTextArea.setFont(new Font(null, Font.PLAIN, 20));
messageTextArea.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
}
return messageTextArea;
}
public void printRawMessage(String rawMessage) {
getMessageTextArea().append(rawMessage);
getMessageTextArea().setCaretPosition(getMessageTextArea().getText().length());
}
}
The layout stuff basically happens in initComponents().
To see the problem:
Start the Application (I used miglayout-4.0-swing.jar).
Wait a bit (don't resize the window), until there are enough messages to create the scrollbar in the status text area.
Now this is what I want. The JTextArea goes all the way to the bottom of the JFrame and is scrolled if neccessary.
Now resize the window. As you can see, everything gets messed up. It will only be fine, if the window is maximized.
Here are two screenshots. The first one is how I want it to be:
The second one is after resizing:
My question: Can somebody tell me, how I keep the layout the way it is before resizing? I want to have the JTextArea go all the way down to the bottom of the window. And if neccessary, the scrollbar should appear. The only way, the status panel can go below the bottom of the window is, if the window is too small (because the configuration panel has a fixed height).
I hope I made myself clear. If not, please ask. ;)
EDIT: You can see the behaviour I want, if you remove the top JScrollPanel (the one that holds all the components). Just change
JScrollPane contentScrollPane = new JScrollPane(mainPanel);
contentScrollPane.setBorder(BorderFactory.createEmptyBorder());
setContentPane(contentScrollPane);
to
setContentPane(mainPanel);
to see what I mean. Unfortunately, this way I loose the scrollbars if the window is very small.
Focusing on your status area and using nested layouts produces the result shown below. Note in particular,
Use invokeLater() to construct the GUI on the EDT.
Use javax.swing.Timer to update the GUI on the EDT.
Use pack() to make the window fit the preferred size and layouts of its subcomponents.
Use the update policy of DefaultCaret to control scrolling.
Avoid needless lazy instantiation in public accessors.
Avoid setXxxSize(); override getXxxSize() judiciously.
Critically examine the decision to extend JFrame.
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.border.TitledBorder;
import javax.swing.text.DefaultCaret;
public class Tab extends JFrame {
private JTextArea messageTextArea;
private JPanel optionPanel, messagePanel;
private JTabbedPane plotTabPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
final Tab tab = new Tab();
tab.setVisible(true);
Timer t = new Timer(200, new ActionListener() {
int count = 0;
#Override
public void actionPerformed(ActionEvent e) {
tab.printRawMessage("testMessage" + count++);
}
});
t.start();
}
});
}
public Tab() {
initComponents();
}
private void initComponents() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel(new GridLayout(1, 0));
Box leftPanel = new Box(BoxLayout.Y_AXIS);
leftPanel.add(getLeftTopPanel());
leftPanel.add(getMessagePanel());
mainPanel.add(leftPanel);
mainPanel.add(getRightPanel());
this.add(mainPanel);
this.pack();
this.setLocationRelativeTo(null);
}
protected JPanel getLeftTopPanel() {
optionPanel = new JPanel();
optionPanel.setBorder(BorderFactory.createTitledBorder(null,
"Configuration", TitledBorder.LEFT, TitledBorder.TOP,
new Font("null", Font.BOLD, 12), Color.BLUE));
JLabel label = new JLabel("Choose");
label.setHorizontalAlignment(JLabel.RIGHT);
optionPanel.add(label);
optionPanel.add(new JSeparator(JSeparator.VERTICAL));
optionPanel.add(new JComboBox(
new String[]{"option1", "option2", "option3"}));
optionPanel.add(new JLabel("Type"));
optionPanel.add(new JTextField("3"));
return optionPanel;
}
protected JTabbedPane getRightPanel() {
plotTabPane = new JTabbedPane();
plotTabPane.add("Tab1", new JPanel());
plotTabPane.add("Tab2", new JPanel());
return plotTabPane;
}
protected JPanel getMessagePanel() {
messagePanel = new JPanel(new GridLayout());
messagePanel.setBorder(BorderFactory.createTitledBorder(null,
"Status Console", TitledBorder.LEFT, TitledBorder.TOP,
new Font("null", Font.BOLD, 12), Color.BLUE));
final JScrollPane sp = new JScrollPane(getMessageTextArea());
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
messagePanel.add(sp);
return messagePanel;
}
protected JTextArea getMessageTextArea() {
messageTextArea = new JTextArea("", 10, 19);
messageTextArea.setEditable(false);
messageTextArea.setFont(new Font(null, Font.PLAIN, 20));
messageTextArea.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
DefaultCaret caret = (DefaultCaret) messageTextArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
return messageTextArea;
}
public void printRawMessage(String rawMessage) {
messageTextArea.append(rawMessage + "\n");
}
}
Add size constraints to your mainPanel in the initComponents method. For instance :
mainPanel.setMinimumSize(new Dimension(400, 400));
mainPanel.setPreferredSize(new Dimension(400, 400));
mainPanel.setMaximumSize(new Dimension(400, 400));

Categories