Reputation: 77
Am I not allowed to use an html table to print to a JTextArea? When I use the following code it prints the html code and doesn't format the tables. What do I do? This is probably a simple fix but since the question is so specific it would take me forever to google this.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.io.*;
public class Index2 extends JPanel{
private JLabel searchLabel;
private JTextArea searchField;
private JButton resultButton;
private JTextArea resultField;
public Index2(){
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.NORTH;
setBackground(Color.WHITE);
super.setLayout(gridbag);
searchLabel = new JLabel("Enter some text:");
searchField = new JTextArea("", 5, 20);
JScrollPane scrollSearch = new JScrollPane(searchField);
searchField.setLineWrap(true);
searchField.setWrapStyleWord(true);
searchField.setOpaque(true);
searchField.setLineWrap(true);
searchField.setWrapStyleWord(true);
searchField.setOpaque(true);
resultButton = new JButton("Count Occurences of Each Letter");
resultField = new JTextArea("", 13, 15);
JScrollPane scrollResult = new JScrollPane(resultField);
resultField.setLineWrap(true);
resultField.setWrapStyleWord(true);
resultField.setOpaque(true);
resultField.setEditable(false);
c.insets = new Insets(10, 10, 10, 10);
c.gridx = 0;
c.gridy = 0;
add(searchLabel, c);
c.gridx = 0;
c.gridy = 1;
add(scrollSearch, c);
c.gridx = 0;
c.gridy = 2;
add(resultButton, c);
c.gridx = 0;
c.gridy = 3;
add(scrollResult, c);
ResultButtonHandler rbhandler = new ResultButtonHandler();
resultButton.addActionListener(rbhandler);
}
class ResultButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
String str = "<html><table>";
int count[] = new int[26];
char letter[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
for (int i = 0; i < letter.length; i++ ){
int upper, lower= 0;
letter[i] = Character.toUpperCase(letter[i]);
upper = helpers.StringUtilities.numberCharacter(letter[i], searchField.getText());
letter[i] = Character.toLowerCase(letter[i]);
lower = helpers.StringUtilities.numberCharacter(letter[i], searchField.getText());
count[i] = upper + lower;
if ((i+1) % 2 == 0){
int h = i-1;
str += "<tr><td>" + letter[h] + ": " + count[h] +"</td><td>" + letter[i] + ": " + count[i] +"</td></tr>";
}
}
str += "</table>";
resultField.setText(str);
str = "";
}
}
}
Upvotes: 0
Views: 1930
Reputation: 347194
I tried the JEditorPane. It prints the same
Then you're using it wrong...
Make sure you set the content type before you set the text...
resultField.setContentType("text/html");
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Index2 extends JPanel {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Index2());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
private JLabel searchLabel;
private JTextArea searchField;
private JButton resultButton;
private JEditorPane resultField;
public Index2() {
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.NORTH;
setBackground(Color.WHITE);
super.setLayout(gridbag);
searchLabel = new JLabel("Enter some text:");
searchField = new JTextArea("", 1, 20);
JScrollPane scrollSearch = new JScrollPane(searchField);
searchField.setLineWrap(true);
searchField.setWrapStyleWord(true);
searchField.setOpaque(true);
searchField.setLineWrap(true);
searchField.setWrapStyleWord(true);
searchField.setOpaque(true);
resultButton = new JButton("Count Occurences of Each Letter");
resultField = new JEditorPane();
JScrollPane scrollResult = new JScrollPane(resultField);
resultField.setOpaque(true);
resultField.setEditable(false);
c.insets = new Insets(10, 10, 10, 10);
c.gridx = 0;
c.gridy = 0;
add(searchLabel, c);
c.gridx = 0;
c.gridy = 2;
add(resultButton, c);
c.weightx = 1;
c.weighty = 0.5;
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 1;
add(scrollSearch, c);
c.gridx = 0;
c.gridy = 3;
add(scrollResult, c);
ResultButtonHandler rbhandler = new ResultButtonHandler();
resultButton.addActionListener(rbhandler);
}
class ResultButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
String str = "<html><table border=1>";
int count[] = new int[26];
char letter[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
Random rnd = new Random();
for (int i = 0; i < letter.length; i++) {
if ((i + 1) % 2 == 0) {
int h = i - 1;
str += "<tr><td>" + letter[rnd.nextInt(letter.length)] + ": " + count[rnd.nextInt(letter.length)] + "</td><td>" + letter[rnd.nextInt(letter.length)] + ": " + count[rnd.nextInt(letter.length)] + "</td></tr>";
}
}
str += "</table>";
resultField.setContentType("text/html");
resultField.setText(str);
str = "";
}
}
}
You could also use a JLabel
which will format the HTML automatically, but you'll need to decide which is more suitable for your needs...
Have a look at How to Use Editor Panes and Text Panes for more details
Upvotes: 2
Reputation: 77
I just went with this
if ((i+1) % 2 == 0){
int h = i-1;
str += letter[h] + ": " + count[h] + "\t" + letter[i] + ": " + count[i] + "\n";
}
It eliminated the need for tables and I accomplished what I intended to do
Upvotes: 0