Append text to certain columns in java text area
How do I append text to certain columns in java text area. I've set the columns to 2 but I want to append one text on 1 column and another text on the other column. How do I do开发者_开发问答 that?
jTextArea1.append("\n");
jTextArea1.setColumns(2);
jTextArea1.append("a");
jTextArea1.append("\n");
jTextArea1.append("b");
JTable
might be a better choice for columnar data.
Although it sound like setColumns
sets the number of textual columns on a JTextArea
what it really does is defining the number of single-character columns for calculating the preferred size of the text area. E.g. if you specify setColumns(80)
the preferred size is calculated to be at least 80 characters wide (if I remember correctly calculated as eighty times the with of 'm').
If you want to have multiple text columns you might use a JTable
as trashgod proposed or use two textareas side by side (if you put both within a scrollpane they will also scroll simultaneously).
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import javax.swing.event.*;
import javax.swing.ListSelectionModel;
class Listfile extends JFrame {
private String name_v;
private int age;
private JButton btn;
private JTextArea reply;
private JPanel pane;
private JScrollPane scrollbar;
public static void main(String[] args){
String name = "lee-roy";
String password = "anointed23";
String body = "hi my name is: ";
String body2 = "and this is my account im glad you could join in";
Listfile account1 = new Listfile();
account1.setListfile("Jamal", 19);
account1.getListfile();
Listfile app = new Listfile();
}
public Listfile(){
super("App chat log Gui");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(550, 600);
pane = new JPanel();
reply = new JTextArea(10, 35);
scrollbar = new JScrollPane(reply);
btn = new JButton("Send");
reply.setLineWrap(true);
reply.setWrapStyleWord(true);
scrollbar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
add(pane);
pane.add(scrollbar);
pane.add(btn);
AreaHandler handle = new AreaHandler();
btn.addActionListener(handle);
}
class AreaHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
if(event.getSource()==btn){
reply.append("Button has been clicked");
}
}
}
public void setListfile(String name, int age_r){
name_v = name;
age = age_r;
}
public void getListfile(){
JOptionPane.showMessageDialog(null, "Hi my name is " + name_v + " the discussion for to day is too sencetive so no viewers of under the age of " + age);
}
}
精彩评论