# TextArea auf Scrollpane



## Kaladial (22. Mai 2008)

moin

ich hab hier mal nen kleines testprogramm geschrieben um mein problem darzustellen:


```
package testapp;

import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;

import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.border.BevelBorder;
import java.awt.Color;
import javax.swing.JButton;

public class Tabbedtest extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JPanel jPanelS = null;
	private JPanel jPanelStatus = null;
	private JToolBar tbFooter = new JToolBar();                 // Fusszeile
	private static JTextArea jTextAreaStatus = new JTextArea(5,40);
	
	/**
	 * This method initializes jPanelS	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelS() {
		if (jPanelS == null) {
			JLabel jLabelS = new JLabel();
			float x_f=(this.getGraphicsConfiguration().getBounds().width)*(1.0f/2.0f)-250;
			int x=Math.round(x_f);
			jLabelS.setPreferredSize(new Dimension(x, 120));
			jPanelS = new JPanel();
			jPanelS.setLayout(new BorderLayout());
			jPanelS.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
			jPanelS.add(jLabelS, BorderLayout.WEST);
			jPanelS.add(getJPanelStatus(), BorderLayout.CENTER);
			jPanelS.setVisible(true);
		}
		return jPanelS;
	}

	/**
	 * This method initializes jPanelStatus	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelStatus(){
		if (jPanelStatus == null){
			jPanelStatus = new JPanel();
			jPanelStatus.setLayout(null);
			//jPanelStatus.setBounds(new Rectangle(0, 0, 350, 120));
			tbFooter.setFloatable(false);
		    tbFooter.setRollover(false);
		    addFooterBar(tbFooter);
		    jPanelStatus.add(tbFooter, null);
		}
		return jPanelStatus;
	}

	protected void addFooterBar(JToolBar toolBar) {
        JPanel tracePanel = new JPanel(new BorderLayout(3,3));
        tracePanel.setOpaque(false);
        tracePanel.setBackground(Color.green);
        tracePanel.add(addJTabbedPaneStatus(), BorderLayout.CENTER);
        tracePanel.add(new JLabel(" "), BorderLayout.PAGE_END);

        toolBar.add(tracePanel);
    }	

	JTabbedPane jTabbedPaneStatus = null;
	private JButton jButton1 = null;
	private JTabbedPane addJTabbedPaneStatus(){
		if (jTabbedPaneStatus == null) {
			jTabbedPaneStatus = new JTabbedPane();
			jTabbedPaneStatus.setName("");
			
			JPanel p1 = new JPanel(); 
	        p1.setOpaque(true); 
	        jTextAreaStatus.setEditable(false);
	        p1.add(new JScrollPane(jTextAreaStatus), null);
	        
	        jTabbedPaneStatus.addTab("Text", p1); 
		}
		return jTabbedPaneStatus;

	}
	
	
	public static void trace(String text) {
        if ( text!=null ) {
            String displayEvent = new String(text + "\n");
            //GlobaleFunction.writeMSG(displayEvent);
            jTextAreaStatus.append(displayEvent);
            jTextAreaStatus.setCaretPosition(jTextAreaStatus.getDocument().getLength());
        }
    }
	
	
	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("test");
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jTextAreaStatus.setText(jTextAreaStatus.getText()+"\n1");
				}
			});
		}
		return jButton1;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Tabbedtest thisClass = new Tabbedtest();
				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}

	/**
	 * This is the default constructor
	 */
	public Tabbedtest() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		tbFooter.setBounds(new Rectangle(0, 0, 460, 140));
		this.setUndecorated(true);
		int mainframe_x=this.getGraphicsConfiguration().getBounds().width;
		int mainframe_y=this.getGraphicsConfiguration().getBounds().height;

		this.setBounds(new Rectangle(0, 0, mainframe_x, mainframe_y));
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanelS(), BorderLayout.SOUTH);
			jContentPane.add(getJButton1(), BorderLayout.NORTH);
		}
		return jContentPane;
	}

}
```

zu meinem problem:
ich habe eine textarea auf einem scrollpanel liegen 
die textarea ist 40, 5 breit / hoch

und wenn ich da test rein schreib kommen auch 4 zeilen richtig...

bei der 5. zeile passiert aber folgendes:
es werden 2 scrollbars angezeigt die für hoch runter scrollen ist auch voll ok
aber wieso wird selbst wenn nur 1 zeichen in dem textarea steht die scrollbar für links-rechts scrollen angezeigt?

also das will ich eigentlich nicht bzw nur wenn der text auch wirklich nicht ins textarea passt... 

wie bekomm ich das hin??

thx für eure hilfe
Kala


----------



## Marco13 (22. Mai 2008)

Wenn der vertikale Scrollbalken erscheint, ist nichtmehr genug Platz (in hoizontaler Richtung). Entwerde die Größen anpassen, oder

           JScrollPane sp = new JScrollPane(jTextAreaStatus);
           sp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
           p1.add(sp, null);


----------



## André Uhres (22. Mai 2008)

```
private static JTextArea jTextAreaStatus = new JTextArea();
...
            JScrollPane scrollPane = new JScrollPane();
            scrollPane.setViewportView(jTextAreaStatus);
            scrollPane.setPreferredSize(new Dimension(400, 90));
            p1.add(scrollPane);
```


----------



## Kaladial (23. Mai 2008)

ok thx


----------

