This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextArea scrollbars problem  RSS feed

 
tadi raja
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am appending data to JTextArea and i needed vertical scrollbars
always i didn't get scrollbars when using ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS.i was add the data using append() method ..is there any problem with using append()..?can please answer me as early as possible.
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by tadi raja:
can please answer me as early as possible.
Patience, patience.

What are you doing to add the JTextArea? You don't add scrollbars to a JTextArea, you use a JScrollPane and add the text area to that.
 
tadi raja
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to add scrollpanes to JTextArea always ...but i am using append() to add the text to JTextArea but it is not working ..is it right way to add scrollpanes to JTextArea using scrollPane.setVerticalScrollBarPolicyScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
 
Rob Spoor
Sheriff
Posts: 21088
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please show us the part of your code where you create the JTextArea and JScrollPane, and where you add those to its parent?
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob is right; show us your code. And you don't add scroll bars to a JTextArea; you add the JTextArea to a JScrollPane.
 
tadi raja
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code

tarea = new JTextArea();
spane = new JScrollPane();

ProductDetailsResponse response = (ProductDetailsResponse)response1;

System.out.println("\n ******In PrinData******");

tarea.setText("");
tarea.append("\n\n Product\t");
tarea.append(response.getResult().getDescription());
tarea.append("\n Description");
tarea.append("\n\n pH Range ");
tarea.append(response.getResult().getPhInformation().getMessage());
tarea.append("\n\n pH Minimum ");
tarea.append(response.getResult().getPhMinimum());
tarea.append("\n\n pH Nean ");
tarea.append(response.getResult().getPhMean());
tarea.append("\n\n pH Maximum ");
tarea.append(response.getResult().getPhMaximum());
tarea.append("\n\n pH Effects ");
tarea.append(response.getResult().getPhEffects().getMessage());
tarea.append("\n\n");
tarea.append("\n\n Stability \n\n\n");
tarea.append(response.getResult().getStability().getMessage());
tarea.append(PhEffect);

tarea.setBackground(Color.white);
tarea.setBorder(BorderFactory.createLineBorder(Color.blue,1));
tarea.setLineWrap(true);
tarea.setEditable(false);
//tarea.setAutoscrolls(true);
tarea.setFont(new Font("Arial",Font.PLAIN,10));
tarea.setCaretPosition(tarea.getText().length());

panel = new JPanel(new FlowLayout(FlowLayout.RIGHT,10,40));

printBtn = new StandardButton();
printBtn.setText("Print");
printBtn.addActionListener(this);
panel.add(printBtn);

closebtn = new StandardButton();
closebtn.setText("Close");
closebtn.addActionListener(this);
panel.add(closebtn);

spane.getViewport().setView(tarea);
spane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
//spane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

getContentPane().add(spane,BorderLayout.NORTH);
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Use Code Tags; your code is really difficult to read without them.

Why don't you simply pass the JTextArea to the JScrollPane constructor?
 
tadi raja
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package com.perse.swing.ramesh;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.List;

public class PrintData extends JDialog implements ActionListener
{
protected JTextArea tarea;
protected JScrollPane spane;
protected JButton printBtn,closebtn;
protected JLabel productDescLbl, prodTypeLbl, phRangeLbl, phMinLbl, phNeanLbl, phMaxLbl, phEffectLbl;
protected PrintUtilities printData;
protected JPanel panel;
protected String msg = "";

public PrintData(JFrame frame,Object response1)//, String msg)
{
super(frame);
// this.msg = msg;
// System.out.println("Ramesh " + msg );
setTitle("Drug Properties Report");
// setDefaultCloseOperation(StandardApplication.DISPOSE_ON_CLOSE);
setBounds(10,10,700,750);

printData = new PrintUtilities(new JTextArea());

productDescLbl = new JLabel("Product \n Description");
productDescLbl.setFont(new Font("Arial",Font.BOLD,12));

tarea = new JTextArea();
spane = new JScrollPane();

ProductDetailsResponse response = (ProductDetailsResponse) response1;

System.out.println("\n ******In PrinData******");

tarea.setText("");
tarea.append("\n\n Product\t");
tarea.append(response.getResult().getDescription());
tarea.append("\n Description");
tarea.append("\n\n pH Range ");
tarea.append(response.getResult().getPhInformation().getMessage());
tarea.append("\n\n pH Minimum ");
tarea.append(response.getResult().getPhMinimum());
tarea.append("\n\n pH Nean ");
tarea.append(response.getResult().getPhMean());
tarea.append("\n\n pH Maximum ");
tarea.append(response.getResult().getPhMaximum());
tarea.append("\n\n pH Effects ");
tarea.append(response.getResult().getPhEffects().getMessage());
tarea.append("\n\n");
tarea.append("\n\n Stability \n\n\n");
tarea.append(response.getResult().getStability().getMessage());

List PhEffects=response.getResult().getPhEffects().getCitation();
for(int i=0; i<PhEffects.size(); i++)
{
String PhEffect = (String)PhEffects.get(i);
tarea.append(PhEffect);
}

tarea.setBackground(Color.white);
tarea.setBorder(BorderFactory.createLineBorder(Color.blue,1));
tarea.setLineWrap(true);
tarea.setEditable(false);
tarea.setAutoscrolls(true);
tarea.setFont(new Font("Arial",Font.PLAIN,10));
tarea.setCaretPosition(tarea.getText().length());

panel = new JPanel(new FlowLayout(FlowLayout.RIGHT,10,40));

printBtn = new JButton();
printBtn.setText("Print");
printBtn.addActionListener(this);
panel.add(printBtn);

closebtn = new JButton();
closebtn.setText("Close");
closebtn.addActionListener(this);
panel.add(closebtn);

spane.getViewport().setView(tarea);
spane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
//spane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

getContentPane().add(spane,BorderLayout.NORTH);
//getContentPane().add(printBtn,BorderLayout.SOUTH);
getContentPane().add(panel,BorderLayout.SOUTH);
}

public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("Print"))
{
//printData.print();
printData.printComponent(getContentPane());
}
else if(ae.getActionCommand().equals("Close"))
{
this.setVisible(false);
}
}
}
 
Ananth Chellathurai
Ranch Hand
Posts: 349
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raja,

Please add code tags for any code when you post. It will make the post more readable.

Ananth Chellathurai
 
tadi raja
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja.tadi@tcs.com
 
Rob Spoor
Sheriff
Posts: 21088
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try adding the JScrollPane using BorderLayout.CENTER instead.

You have now added it to the top, meaning it will only use its preferred size. With BorderLayout, it's only the CENTER component that will be resized.
 
tadi raja
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob Prime

Even though i was used BorderLayout.CENTER for scrollpane it was not working .
 
Rob Spoor
Sheriff
Posts: 21088
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well that's strange because that fixed it for me.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!