Reputation: 187
I am trying to add text in a JTextArea. But i need some of the text to be strike through and some to added as it is. I have searched over the internet, but couldn't find any answer. Any help on what to refer?
Upvotes: 1
Views: 3094
Reputation: 1962
JTextArea
allow you set font style, but you cannot set style to text partially. Please see the code below, you can use setFont
method to specify font with strikethru style, but it applies to all text in the JTextArea
:
JTextArea area = new JTextArea();
Font font = new Font("arial", Font.PLAIN, 12);
Map fontAttr = font.getAttributes();
fontAttr.put (TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
Font myFont = new Font(fontAttr);
area.setFont (myFont);
area.setText ("Hello");
Whereas, if you want some text are in strikethru and some not, then you have to use JTextPane
with StyledDocument
, but I do not recommend this because you need a lot of tweaking to display your content with specific style. Below code may give you the idea:
DefaultStyledDocument doc = new DefaultStyledDocument();
StyleContext sc = new StyleContext();
Style style = sc.addStyle("strikethru", null);
StyleConstants.setStrikeThrough (style,true);
doc.insertString (0, "Hello ", null);
doc.insertString (6, "strike through ", style);
JTextPane pane = new JTextPane(doc);
Upvotes: 4
Reputation: 750
You can use below code. Here some of the strike text "@11@","@22@" and some to added text "@yicHFRx1nc@" ,"@icHFRx1nc@" which replace of strike text.
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class JTextAreaExample {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public JTextAreaExample() {
prepareGUI();
}
public static void main(String[] args) {
JTextAreaExample swingControlDemo = new JTextAreaExample();
swingControlDemo.showTextAreaDemo();
}
private void prepareGUI() {
mainFrame = new JFrame("JTextArea Example");
mainFrame.setSize(400, 400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
});
headerLabel = new JLabel("", JLabel.CENTER);
statusLabel = new JLabel("", JLabel.CENTER);
statusLabel.setSize(350, 100);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showTextAreaDemo() {
headerLabel.setText("JTextArea");
JLabel descriptionLabel = new JLabel("Description: ", JLabel.RIGHT);
final JTextArea descriptionTextArea = new JTextArea("Enter String ", 5, 20);
JScrollPane scrollPane = new JScrollPane(descriptionTextArea);
JButton showButton = new JButton("Show");
showButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String b = descriptionTextArea.getText().replace("@11@", "@yicHFRx1nc@")
.replace("@22@", "@icHFRx1nc@");
System.out.println("b=" + b);
statusLabel.setText(b);
}
});
controlPanel.add(descriptionLabel);
controlPanel.add(scrollPane);
controlPanel.add(showButton);
mainFrame.setVisible(true);
}
}
Upvotes: 1