trying to create a scroll pane for text area - java

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);

Related

JTextpane lost focus when double-struck input

I insert a jtextarea into a jtextpane.
When input in jtextarea, the focus always go into the outter textpanel.
It only occur when there is double-struck in the textpane.
It's ok if there is no double-struck charactor.
JFrame frame = new JFrame();
JTextPane jTextPane = new JTextPane();
jTextPane.setFont(new Font("Cambria Math", 0, 12));
JTextArea area = new JTextArea();
area.setBorder(BorderFactory.createLineBorder(Color.black));
jTextPane.getDocument().insertString(jTextPane.getDocument().getLength(), "a", null);
jTextPane.getDocument().insertString(jTextPane.getDocument().getLength(), "\uD835\uDD38", null);
jTextPane.getDocument().insertString(jTextPane.getDocument().getLength(), "b", null);
jTextPane.insertComponent(area);
frame.add(jTextPane);
frame.setSize(100, 100);
frame.setVisible(true);
I want to continuous input text in the textarea.

Keep textArea size after switching the tap

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.

how to add a "console" like window to a GUI?

I have created a GUI. It has several buttons. i would like to add a console like object underneath them in which i would be able to write messages so the user would see them.
what element/object/class should i use? i want it to be able to present messages in different lines.
here is my code for creating the GUI:
public ssGUI() {
setLayout(new BorderLayout());
bRunNoAdj = new JButton("Run no adjustment");
bRunNoAdj.setVerticalTextPosition(AbstractButton.CENTER);
bRunNoAdj.setHorizontalTextPosition(AbstractButton.LEADING);
bRunNoAdj.setMnemonic(KeyEvent.VK_E);
bRunNoAdj.addActionListener(this);
bRunNoAdj.setEnabled(false);
bRunNoAdj.setBackground(Color.white);
bRunAdj = new JButton("Run adjustment");
bRunAdj.setVerticalTextPosition(AbstractButton.CENTER);
bRunAdj.setHorizontalTextPosition(AbstractButton.LEADING);
bRunAdj.setMnemonic(KeyEvent.VK_E);
bRunAdj.addActionListener(this);
bRunAdj.setEnabled(false);
bRunAdj.setBackground(Color.white);
bConnect = new JButton("Connect");
bConnect.setMnemonic(KeyEvent.VK_E);
bConnect.addActionListener(this);
bConnect.setEnabled(true);
bConnect.setBackground(Color.white);
bDisconnect = new JButton("Disconnect");
bDisconnect.setMnemonic(KeyEvent.VK_E);
bDisconnect.addActionListener(this);
bDisconnect.setEnabled(false);
bDisconnect.setBackground(Color.white);
bStationary = new JButton("Show Stationary");
bStationary.setMnemonic(KeyEvent.VK_E);
bStationary.addActionListener(this);
bStationary.setEnabled(false);
bStationary.setBackground(Color.white);
bMoving = new JButton("Show Moving");
bMoving.setMnemonic(KeyEvent.VK_E);
bMoving.addActionListener(this);
bMoving.setEnabled(false);
bMoving.setBackground(Color.white);
JPanel topPanel = new JPanel();
topPanel.add(bConnect);
topPanel.add(bDisconnect);
add(topPanel, BorderLayout.NORTH);
JPanel centerPanel = new JPanel();
centerPanel.add(bRunNoAdj);
centerPanel.add(bRunAdj);
add(centerPanel, BorderLayout.CENTER);
JPanel bottomPanel = new JPanel();
bottomPanel.add(bStationary);
bottomPanel.add(bMoving);
add(bottomPanel, BorderLayout.SOUTH);
}
any help would be appreciated, thank you.
The easiest would probably be to use a JTextArea.
You would ofcourse prevent the user from editing the area, this can be done like this:
JTextArea area = new JTextArea();
area.setEditable(false);
And if you want the user to be able to scroll the area you can add it to a JScrollPane like this:
JTextArea area = new JTextArea();
JScrollPane scrollableArea = new JScrollPane(area);
And lastly you can add a line to the area by doing:
area.setText(area.getText() + "\n" + newLine);
Or preferably:
area.append("\n" + newLine);
I hope this helps :)
Use a JTextArea. Call text_area.setEditable(false); on it to make it read-only.
I think what you mean is JTextArea or JTextField

How to add the scrollbar inside the jpanel in java..?

JPanel mEvidenceFilePanel = new JPanel();
mEvidenceFilePanel.setLayout(null);
mEtitleSnoLbl = new JLabel("", JLabel.LEFT);
mEtitleEtypesLbl = new JLabel("", JLabel.LEFT);
mEtitleSnoLbl.setBounds(10, 1, 50, 50);
mEtitleEtypesLbl.setBounds(50, 1, 100, 50);
mEvidenceFilePanel.add(mEtitleSnoLbl);
mEvidenceFilePanel.add(mEtitleEtypesLbl);
hv.mMainFrame.getContentPane().add(mEvidenceFilePanel);
I cant figure it out of adding scrollbar inside the JPanel, not for Frame... anyone know help me...how to add the Scrollbar inside.. Thanks...
First of all it's a very bad idea to make your layouts null. As for the answer to your question try this..
JPanel pn= new JPanel();
pn.setLayout(null); //Do not do this, I'm just using this as an example
JScrollPane scroll = new JScrollPane(panel);
JScrollPane s = new JScrollPane(pn, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

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);

Categories

Resources