2010-07-28 18 views
8

Estoy usando un JTextPane para mostrar algo de HTML que contiene una tabla con un borde. Quiero que tenga un simple borde de 1 píxel.Borde de tabla de 1 píxel en JTextPane usando HTML

Intenté usar style="border: 1px solid; border-collapse:collapse". Esto funciona en un navegador web, pero no en JTextPane.

¿Hay alguna manera de tener un borde de tabla de 1 píxel simple usando HTML en un JTextPane?

Respuesta

3

Aquí está un ejemplo completo:

package test 

import java.awt.SystemColor; 
import javax.swing.BorderFactory; 
import javax.swing.BoxLayout; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.SwingConstants; 
import javax.swing.SwingUtilities; 

@SuppressWarnings("serial") 
public class HtmlDemo extends JPanel { 

    public HtmlDemo() { 
     setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS)); 

     String rgb = Integer.toHexString(SystemColor.window.getRGB()); 
     String backgroundColor = rgb.substring(2, rgb.length()); 

     String html = "<html>\n" 
      + "<head>\n" 
      + "<style type=\"text/css\">\n" 
      + "table {\n" + "width: 100%\n" + "}\n" 
      + "td, th {\n" + "background-color: #" + backgroundColor + "\n" 
      + "}\n" 
      + "</style>\n" 
      + "</head>\n" 
      + "<body>\n" 
      + "HTML table test:\n" 
      + "<div style=\"background-color: black\">\n" 
      + "<table border=\"0\" cellpadding=\"2\" cellspacing=\"1\">\n" 
      + "<tr>\n" + "<td>\n" + "cell1\n" + "</td>\n" + "<td>\n" + "cell2\n" + "</td>\n" + "</tr>\n" 
      + "<tr>\n" + "<td>\n" + "cell3\n" + "</td>\n" + "<td>\n" + "cell4\n" + "</td>\n" + "</tr>\n" 
      + "</div>\n" 
      + "</body>\n" 
      + "</html>"; 

     JLabel label = new JLabel(html); 
     label.setVerticalAlignment(SwingConstants.CENTER); 
     label.setHorizontalAlignment(SwingConstants.CENTER); 

     setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); 
     add(label); 
    } 

    public static void main(String[] args) { 

     SwingUtilities.invokeLater(new Runnable() { 

      public void run() { 
       JFrame frame = new JFrame("HtmlDemo"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new HtmlDemo()); 
       frame.pack(); 
       frame.setVisible(true); 
      } 
     }); 
    } 
} 
+0

+1 para [sscce] (http://sscce.org/); 'SystemColor' es un buen toque, también. – trashgod

1

javax.swing.text.html se basa en HTML 3.2, pero se puede utilizar la frontera atributo de la etiqueta <table>.

+0

Establecer border = 1 no crea un borde de 1 píxel, porque cada celda tiene su propio borde. –

+0

@Chris B: Es posible que deba ajustar 'cellspacing' y' cellpadding' también. – trashgod

0

trashgod es correcto, el soporte de HTML de Java es limitado, ¿por qué no utilizar una solución HTML? Simplemente ponga su mesa (sin bordes) dentro de otra mesa con una celda que tenga un borde.

<table id='outerTable' border='1'><tr><td> 
    <table id='innerTable'> 
     // Content here 
    </table> 
</td></tr></table> 

No es el método más limpio, pero supera las limitaciones de HTML 3.2.

8

Use una combinación de

public static final String TD = "<td style='background-color: white'></td>"; 
public static final String TABLE_PROP = "style='border: 1px black solid; background-color: black' width='100%' cellspacing='1' cellpadding='2'"; 


String html = "<table " + TABLE_PROP + ">" + "<tr>" + TD + TD + "</tr><tr>" + TD + TD + "</tr></table>"; 
try 
{ 
      htmlEditorKit.insertHTML(htmlDocument, caretPosition, html, 0, 0, null); 
} 
+0

Increíble hack. ¡Gracias! –

+0

¡Gran solución! Para reducir la repetición del código, he creado los siguientes estilos: '' 'table {border: 1px black solid; background-color: black;} '' 'y' '' td, th {background-color: white; } '' ' – ruX

0

He aquí un ejemplo para crear un borde de una mesa en el color preferible en HTML 3.2:

<table width="100%" cellpadding="1" bgcolor="#000000"> 
     <tr><td> 
      <table width="100%" bgcolor="#F6F6F6"> 
       <tr><td> 
       Test     
       </td></tr>    
      </table> 
     </td></tr></table> 
Cuestiones relacionadas