Bom dia pessoal,
Como faço para poder alinhar o texto de um JTextArea a direita?
Vi em alguns sites sobre o setHorizontalAlignment mas não existe essa função para o JTextArea pelo que vi até agora.
Alguém poderia me ajudar
Grato
Bom dia pessoal,
Como faço para poder alinhar o texto de um JTextArea a direita?
Vi em alguns sites sobre o setHorizontalAlignment mas não existe essa função para o JTextArea pelo que vi até agora.
Alguém poderia me ajudar
Grato
o método setHorizontalAlignment é da classe JTextField.
Aconselho a você utilizar a JTextPane.
Veja esta página do The Java Tutorial: http://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html
package guj;
import java.awt.BorderLayout;
public class TesteAlinhamentoJTextArea extends JFrame {
private JPanel contentPane;
private JPanel panel;
private JRadioButton rdbtnLeft;
private JRadioButton rdbtnCenter;
private JRadioButton rdbtnRight;
private JRadioButton rdbtnJustified;
private JScrollPane scrollPane;
private final ButtonGroup buttonGroup = new ButtonGroup();
private JTextPane textPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TesteAlinhamentoJTextArea frame = new TesteAlinhamentoJTextArea();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TesteAlinhamentoJTextArea() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
contentPane.add(getPanel(), BorderLayout.SOUTH);
contentPane.add(getScrollPane(), BorderLayout.CENTER);
}
private JPanel getPanel() {
if (panel == null) {
panel = new JPanel();
panel.add(getRdbtnLeft());
panel.add(getRdbtnCenter());
panel.add(getRdbtnRight());
panel.add(getRdbtnJustified());
}
return panel;
}
private JRadioButton getRdbtnLeft() {
if (rdbtnLeft == null) {
rdbtnLeft = new JRadioButton("Left");
rdbtnLeft.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet center = new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_LEFT);
doc.setParagraphAttributes(0, doc.getLength(), center, false);
}
});
buttonGroup.add(rdbtnLeft);
}
return rdbtnLeft;
}
private JRadioButton getRdbtnCenter() {
if (rdbtnCenter == null) {
rdbtnCenter = new JRadioButton("Center");
rdbtnCenter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet center = new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
doc.setParagraphAttributes(0, doc.getLength(), center, false);
}
});
buttonGroup.add(rdbtnCenter);
}
return rdbtnCenter;
}
private JRadioButton getRdbtnRight() {
if (rdbtnRight == null) {
rdbtnRight = new JRadioButton("Right");
rdbtnRight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet center = new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_RIGHT);
doc.setParagraphAttributes(0, doc.getLength(), center, false);
}
});
buttonGroup.add(rdbtnRight);
}
return rdbtnRight;
}
private JRadioButton getRdbtnJustified() {
if (rdbtnJustified == null) {
rdbtnJustified = new JRadioButton("Justified");
rdbtnJustified.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet center = new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_JUSTIFIED);
doc.setParagraphAttributes(0, doc.getLength(), center, false);
}
});
buttonGroup.add(rdbtnJustified);
}
return rdbtnJustified;
}
private JScrollPane getScrollPane() {
if (scrollPane == null) {
scrollPane = new JScrollPane();
scrollPane.setViewportView(getTextPane());
}
return scrollPane;
}
private JTextPane getTextPane() {
if (textPane == null) {
textPane = new JTextPane();
textPane.setText("Bom dia pessoal, Como faço para poder alinhar o texto de um JTextArea a direita? Vi em alguns sites sobre o setHorizontalAlignment mas não existe essa função para o JTextArea pelo que vi até agora. Alguém poderia me ajudar");
}
return textPane;
}
}