views:

300

answers:

4

How do i put the text in specific columns with jTextArea?

        private javax.swing.JTextArea jTextArea1;
        jTextArea1.setColumns(4);
        jTextArea1.insert(price, 0);    //column 1
        jTextArea1.insert(cost, 0);    //column 2
        jTextArea1.insert(quantity, 0);       //column ect..
        jTextArea1.insert(itemName, 0);
        jTextArea1.insert("\n", 0);
A: 

Your code is inserting your text at the same position ! Replace 0 by whatever column you want the text to be at. And see the result

EDIT:

Column size means amount of characters, not words. As commented, have a look at JTable

Marc
Ya i dont really get the position property. basiclly i want to set the price, cost, quantity, name into individual columns
nubme
You are not using the right Swing component then.You better have a look at JTable. which have proper column. You simply set up an array containing all your values, each element of the array to be placed in its corresponding column.
Marc
ya i was thinking about using that, but i was unsure how to do it in a while statement since im reading in values from a file.
nubme
See other answer about JTable. They are not as easy to use as a simple JTextArea. See several examples, you will get it. JTable is what you need. The fact that you read values from a file, database or other is independent to what you use to display them.
Marc
+1  A: 

A column refers to a character, so if columns = 4 it means the width will be 4 characters wide.

JRL
oh, is there anyway to separate my text into columns or indent it so that it appears evenly in my textarea?
nubme
Use a JTable instead, or multiple JTextAreas
JRL
+1  A: 

The best approach is to use a JTable.

But if you really want to use a text cmponent you can use a JTextPane and play with tabs. See my example in JTextPane Tab Size.

camickr
A: 

There are situations when the (mighty) JTable is too much.

If you just want a JLabel/ JTextArea like component with some columns use a HTML-Table in a JTextPane or in a JEditorPane:

import java.awt.Font;
import javax.swing.JDialog;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.text.html.HTMLDocument;

public class ColumnsInJTextPane
{

  public ColumnsInJTextPane()
  {
    double price = 124.75;
    int quantity = 3;
    String itemName = " iPad";

    JTextPane t = new JTextPane();
    t.setContentType( "text/html" );

    StringBuilder text = new StringBuilder( 150 );
    text.append( "<html><body>" );
    text.append( "<table border='0' style='margin:4px 2px 12px 6px' width='100%'>" );

    text.append( "<tr>" + "<td width='30' align='left' valign='top' style='margin-right:8px'>" );
    text.append( price );
    text.append( "</td>" );

    text.append( "<td align='left' valign='top' style='margin-right:8px'>" );
    text.append( itemName );
    text.append( "</td>" );

    text.append( "<td width='20' align='left' valign='top' style='margin-right:8px'>" );
    text.append( quantity );
    text.append( "</td>" + "</tr>" );

    text.append( "<tr>" + "<td>" );
    text.append( price * 4 );
    text.append( "</td>" );

    text.append( "<td>" );
    text.append( (((Boolean) itemName.equals( itemName )).toString().concat( itemName )) );
    text.append( "</td>" );

    text.append( "<td>" );
    text.append( quantity / 2 );
    text.append( "</td>" + "</tr>" );

    text.append( "</table>" );
    text.append( "</body></html>" );

    t.setText( text.toString() );

    //to get a consistent (body) appearance use the font from the Label using a CSS rule (instead of the value in javax.swing.text.html.default.css)
    Font font = UIManager.getFont( "Label.font" );
    String bodyRule =
    "body { font-family: " + font.getFamily() + "; " + "font-size: " + font.getSize() + "pt; }";
    ((HTMLDocument) t.getDocument()).getStyleSheet().addRule( bodyRule );

    JDialog d = new JDialog();
    d.add( t );
    d.pack();
    d.setVisible( true );
  }

  public static void main( String[] args )
  {
    new ColumnsInJTextPane();
  }

}

There are some tweaks and tricks in this example which demonstrate how evil HTML really is ;-)

  • the trimming of spaces is utilized
  • the second column is growing/ shrinking with the JDialog size because of the omitted width in its td-Tag
  • css margins have been thrown in for no special reason
  • for a consistent appearance the font of the JLabel has to be fetched from the UIManager and set via CSS
  • if you reduce the width of the JDialog the text in the second column is nicely word-wrapped
    (ooh, wait! this is a nice feature of HTML)
bobndrew