Why does horizontal scrollbar do not work with my JTextArea? - java

Trying to read and write a text document, everything is working but the horizontal scrollbar isn't visible.
I tried to activate the JScrollPane horizontal scrollbar manually but that wasnt a result.
public class JScrollPaneÜbung extends JFrame
{
private static final long serialVersionUID = 1L;
private JTextArea area;
private JTextField field;
private JScrollPane scroll;
private JButton dateisuche;
private JButton dateispeichern;
private Panel panel;
private Panel sPanel;
public JScrollPaneÜbung()
{
area = new JTextArea(32, 41);
field = new JTextField(30);
scroll = new JScrollPane(area);
dateispeichern = new JButton("Speichern");
dateisuche = new JButton("Durchsuchen");
panel = new Panel();
sPanel = new Panel();
createGUI();
}
public void createGUI() {
setBounds(200, 200, 600, 600);
BorderLayout b = new BorderLayout();
setLayout(b);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dateisuche.addActionListener(new JScrollPaneListener(this));
dateispeichern.addActionListener(new JScrollPaneListener(this));
panel.add(dateisuche);
panel.add(field);
panel.add(scroll);
sPanel.add(dateispeichern);
add(panel, BorderLayout.NORTH);
add(sPanel, BorderLayout.SOUTH);
setVisible(true);
}
public static void main(String[] args) {
new JScrollPaneÜbung();
}
No horizontal scrollbar visible, but my text document is longer than the JTextArea

You're using a regular JPane. Use a JScrollPane instead. You can find documentation here that will guide you.
Resource

Related

Why can't I type in a JTextArea unless I resize the window?

I've placed a JTextArea within a JFrame and come across an issue where I can only type within my JTextArea unless I resize the window. How can I get JTextArea to let me type as soon as the window runs without having to resize it?
public class Frame extends JFrame{
public Note() {
this.setSize(new Dimension(1000, 1000));
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public JScrollPane createContent(){
JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
this.add(scrollPane, BorderLayout.CENTER);
return null;
}
public static void main(String[] args) {
new Note();
Note mainWindow = new Note();
}
}
public class Note extends JFrame{
private static final long serialVersionUID = 1L;
public Note() {
createContent(); // add this line into your code.
int x = 400;
int y = 300;
this.setSize(new Dimension(x, y));
this.setTitle("Post-It Note");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public JScrollPane createContent(){
Color textAreaColor = new Color(248, 247, 235);
JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setBorder(null);
textArea.setBackground(textAreaColor);
scrollPane.setBackground(textAreaColor);
textArea.setMargin(new Insets(10, 15, 20, 20));
this.add(scrollPane, BorderLayout.CENTER);
return null;
}
public static void main(String[] args) {
new Note();
// mainWindow.createContent(); comment this line...
}
}
Note:
into your older code,
new Note(); // this one create 1-frame..
...
// Note mainWindow = new Note(); this one also create another frame so need to comment it
// mainWindow.createContent(); does not required because already this method called by constructor...

On JTree Click Move to Line in Scrollable JTextArea

Hi I am attempting to have my JTextArea Scroll to the line that the user clicks on from the JTree. However, I do not know how to have the JTextArea focus on that specific line.
For example, if the user clicks a JTree node that has information on line 118 then I want the JTextArea to scroll to that line. I have a picture below demonstrating what I mean, as well as parts of the code.
Code:
private static JFrame frame;
private static JLabel lblFileChosen;
private static JTextArea resultsTextArea;
private static JScrollPane scroll2;
private static JTextArea LogFileTA_1;
private static JTree errorTree;
private static DefaultTreeModel model;
private static ArrayList<logObject> rftdArrayList = new ArrayList<logObject>();
private static void clickErrorTree(MouseEvent me)
{
TreePath tp = errorTree.getPathForLocation(me.getX(), me.getY());
if (tp != null)
System.out.println(tp.toString());
else
System.out.println("huh");
}
private void initialize()
{
frame = new JFrame();
frame.setBounds(100, 100, frameW, frameH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout(0, 0));
LogFileTA_1 = new JTextArea(34,78);
frame.getContentPane().add(LogFileTA_1, BorderLayout.WEST);
LogFileTA_1.setWrapStyleWord(true);
LogFileTA_1.setLineWrap(true);
LogFileTA_1.setText("Log File");
LogFileTA_1.setEditable(false);
LogFileTA_1.setVisible(true);
JScrollPane scroll1 = new JScrollPane(LogFileTA_1);
frame.getContentPane().add(scroll1, BorderLayout.WEST);
lblFileChosen = new JLabel("File Chosen: ");
lblFileChosen.setVerticalAlignment(SwingConstants.BOTTOM);
lblFileChosen.setHorizontalAlignment(SwingConstants.LEFT);
frame.getContentPane().add(lblFileChosen, BorderLayout.SOUTH);
errorTree = new JTree();
errorTree.setVisibleRowCount(5);
errorTree.setModel(null);
resultsTextArea = new JTextArea();
//resultsTextArea.setRows(8);
resultsTextArea.setText("Results Area");
resultsTextArea.setColumns(10);
//frame.getContentPane().add(resultsTextArea, BorderLayout.CENTER);
resultsTextArea.setEditable(false);
scroll2 = new JScrollPane(errorTree);
scroll2.setColumnHeaderView(resultsTextArea); //errorTree
frame.getContentPane().add(scroll2, BorderLayout.CENTER);
mntmOpen.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
errorTree.setModel(null);
createFileChooser(frame);
}
});
errorTree.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
clickErrorTree(me);
}
});
}
, if the user clicks a JTree node that has information on line 118 then I want the JTextArea to scroll to that line.
Check out Text Utilities.
You can use the RXTextUtilities.gotoStartofLine(...) method.
If you want you could also use the RXTextUtilities.centerLineInScrollPane(...) method.

GridLayout is causing my components to disappear

I'm trying to create a window with a scrollable JTextArea and a JTextField below it. I want the frame to look like a chat window; one, large scrollable text area and a single lined text frame. I've tried variations but I can't get the text area scrollable without making the entire window scrollable. It's incredibly annoying. My current iteration only draws one panel to the screen:
private void buildGUI() {
Container chatClientContainer = getContentPane();
chatClientContainer.setLayout(new BorderLayout());
JPanel messagesReceivedPanel = new JPanel();
messagesReceivedPanel.setLayout(new GridLayout(1, 1, 5, 5));
JTextArea messagesReceived = new JTextArea("area");
messagesReceivedPanel.add(messagesReceived);
JPanel draftPanel = new JPanel();
draftPanel.setLayout(new GridLayout(1, 1, 5, 5));
JTextField draftMessage = new JTextField("field");
draftPanel.add(draftMessage);
chatClientContainer.add(new JScrollPane(messagesReceivedPanel));
chatClientContainer.add(draftPanel);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int windowWidth = 400;
int windowHeight = 600;
int posX = ((int) screenSize.getWidth())/2 - windowWidth/2;
int posY = (int) screenSize.getHeight()/2 - windowHeight/2;
setBounds(posX, posY, windowWidth, windowHeight);
setResizable(true);
setVisible(true);
}
How can I position this the way I want?
Why not just use BorderLayout? Place the JTextArea's JScrollPane BorderLayout.CENTER and the JTextField (not JTextFrame) BorderLayout.PAGE_END.
For example:
import java.awt.BorderLayout;
import javax.swing.*;
public class ChatPanel extends JPanel {
private static final long serialVersionUID = 1L;
private static final int ROWS = 15;
private static final int COLS = 30;
private JTextArea textArea = new JTextArea(ROWS, COLS);
private JTextField textField = new JTextField(COLS);
public ChatPanel() {
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
setLayout(new BorderLayout());
add(scrollPane, BorderLayout.CENTER);
add(textField, BorderLayout.PAGE_END);
}
private static void createAndShowGUI() {
ChatPanel paintEg = new ChatPanel();
JFrame frame = new JFrame("ChatPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(paintEg);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

JTextArea scrolling not working

I am trying to make a JTextArea vertically scrollable. I did some research and I'm pretty sure I'm using a LayoutManager, not adding JTextArea directly to the parent panel, and is setting the preferred size of both JTextArea and JScrollPane. Not sure what am I missing here... Here's the code:
public class ConsolePane extends JDialog {
private static final long serialVersionUID = -5034705087218383053L;
public static final Dimension CONSOLE_DIALOG_SIZE = new Dimension(400, 445);
public static final Dimension CONSOLE_LOG_SIZE = new Dimension(400, 400);
public static final Dimension CONSOLE_INPUT_SIZE = new Dimension(400, 25);
private static ConsolePane instance = new ConsolePane();
public static ConsolePane getInstance() {
return instance;
}
private JTextArea taLog;
private JTextField tfInput;
public ConsolePane() {
this.setTitle("Console");
JPanel contentPane = new JPanel();
this.setContentPane(contentPane);
contentPane.setLayout(new BorderLayout());
contentPane.add(createConsoleLog(), BorderLayout.CENTER);
contentPane.add(createConsoleInput(), BorderLayout.SOUTH);
contentPane.setPreferredSize(CONSOLE_DIALOG_SIZE);
}
private JComponent createConsoleLog() {
taLog = new JTextArea();
taLog.setLineWrap(true);
taLog.setPreferredSize(CONSOLE_LOG_SIZE);
((DefaultCaret) taLog.getCaret())
.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
JScrollPane container = new JScrollPane(taLog);
container
.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
container
.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
container.setPreferredSize(CONSOLE_LOG_SIZE);
return container;
}
private JComponent createConsoleInput() {
tfInput = new JTextField();
tfInput.setPreferredSize(CONSOLE_INPUT_SIZE);
tfInput.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
taLog.append(tfInput.getText() + "\r\n");
tfInput.setText("");
}
});
tfInput.requestFocus();
return tfInput;
}
public static void main(String[] args) {
ConsolePane.getInstance().pack();
ConsolePane.getInstance().setVisible(true);
}
}
Thx in advance!
Try the following code, It may help you
JTextArea txt=new JTextArea();
JScrollPane pane=new JScrollPane(txt,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
Figured it out myself. taLog.setPreferredSize() is preventing the scrolling. removed that and everything worked fine. Thx everyone for your help.

JTabbedPane JLabel, JTextField

Right, I have a JTabbedPane that has a JPanel that contains a JLabel and a JTextField.
my code
JTabbed Pane declaration :
this.tabPane = new JTabbedPane();
this.tabPane.setSize(750, 50);
this.tabPane.setLocation(10, 10);
tabPane.setSize(750,450);
tabPane.add("ControlPanel",controlPanel);
textfield declaration :
this.channelTxtFld = new JTextField("");
this.channelTxtFld.setFont(this.indentedFont);
this.channelTxtFld.setSize(200, 30);
this.channelTxtFld.setLocation(200, 10);
JLabel :
this.channelLabel = new JLabel("Channel name : ");
this.channelLabel.setSize(150, 30);
this.channelLabel.setLocation(10,10);
private void createPanels() {
controlPanel = new JPanel();
controlPanel.setSize(650,500);
}
private void fillPanels() {
controlPanel.add(channelLabel);
controlPanel.add(channelTxtFld);
}
So what my plan is, was to have a tabbed pane that has a JPanel where I have some Labels, textfields and buttons on fixed positions, but after doing this this is my result:
http://i.stack.imgur.com/vXa68.png
What I wanted was that I had the JLabel and next to it a full grown JTextfield on the left side not in the middle.
Anyone any idea what my mistake is ?
thank you :)
What kind of Layout Manager are you using for your controlPanel, you probably want BorderLayout, putting the Label in the West, and the TextField in the center.
BTW, setting the size and position of various components doesn't make sense unless you are using a Null Layout, which isn't a good idea. So i'd remove all that stuff and let the Layout Manager do it for you.
Use a LayoutManager and consider also the methods setPreferredSize, setMinimumSize, setMaximumSize to adjust components bounds according on which is your desired effect.
Assuming the default JPanel layout, FlowLayout, give the JTextField a non-zero number of columns, and give the JLabel a JLabel.LEFT constraint.
Addendum:
a full grown JTextField
Something like this?
import java.awt.*;
import javax.swing.*;
/**
* #see http://stackoverflow.com/questions/5773874
*/
public class JTabbedText {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
private final JTabbedPane jtp = new JTabbedPane();
#Override
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jtp.setPreferredSize(new Dimension(400, 200));
jtp.addTab("Control", new MyPanel("Channel"));
f.add(jtp, BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
});
}
private static class MyPanel extends JPanel {
private final JLabel label = new JLabel("", JLabel.LEFT);
private final JTextField text = new JTextField();
public MyPanel(String name) {
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
label.setText(name);
label.setAlignmentY(JLabel.TOP_ALIGNMENT);
text.setAlignmentY(JTextField.TOP_ALIGNMENT);
this.add(label);
this.add(text);
}
}
}

Categories