user2609825
user2609825

Reputation:

CSS Style in JTable cell

I am trying to put HTML into a JTable cell, I have tested this piece of code in IE, but the thing is, it doesn't appear as it should in a table cell. Can I confirm that it doesn't work in a table cell? Below is the HTML.

<html>
<style>
div {
    *display: inline; /* For IE7 */
    display:inline-block;
    width: 50%;
    text-align: center;
</style>
  <div>A</div><div>B</div>
  <div>A1</div><div>B1</div>
  <div>A2</div><div>B2</div>
</html>

I have also tried to put the style inside the <div>, it works in IE but not the table cell. Can anyone help?

Upvotes: 0

Views: 1397

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168825

It appears the attributes of the display style are ignored by the simple CSS engine provided with the JSE. This source demonstrates that. The styled text is red, but the display attribute does not change anything.

HTML Display Style

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

class HTMLDisplayStyle {

    final static String EOL = System.getProperty("line.separator");
    final static String HTML_PRE = "<html>" + EOL
            + "<head>" + EOL
            + "<style>" + EOL
            + "span {" + EOL
            + "color: #FF0000;" + EOL
            + "display: ";
    final static String HTML_POST = ";" + EOL
            + "}" + EOL
            + "</style>" + EOL
            + "</head>" + EOL
            + "<body>" + EOL
            + "<p>" + EOL
            + "Some text " + EOL
            + "<span>text with display style</span> " + EOL
            + "some more text." + EOL
            + "</p>" + EOL
            + "</body>" + EOL
            + "</html>" + EOL;
    final static String[] ATTRIBUTES = {
        "inline",
        "block",
        "none"
    };

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                JPanel gui = new JPanel(new BorderLayout());

                String s = HTML_PRE + ATTRIBUTES[0] + HTML_POST;
                final JTextArea ta = new JTextArea(s, 15, 30);
                gui.add(new JScrollPane(ta), BorderLayout.PAGE_END);

                final JLabel l = new JLabel(s);
                gui.add(new JScrollPane(l));

                final JComboBox style = new JComboBox(ATTRIBUTES);
                gui.add(style, BorderLayout.PAGE_START);
                ActionListener styleListener = new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        String styleAttribute = 
                                style.getSelectedItem().toString();
                        String html = HTML_PRE + styleAttribute + HTML_POST;
                        ta.setText(html);
                        l.setText(html);
                    }
                };
                style.addActionListener(styleListener);


                JOptionPane.showMessageDialog(null, gui);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}

Upvotes: 3

Related Questions