2010-07-28 13 views
8

Ich verwende ein JTextPane, um HTML anzuzeigen, das eine Tabelle mit einem Rahmen enthält. Ich möchte, dass es eine einfache 1-Pixel-Grenze hat.1 Pixel-Tabellenrahmen in JTextPane mit HTML

Ich versuchte mit style="border: 1px solid; border-collapse:collapse". Dies funktioniert in einem Webbrowser, aber nicht in JTextPane.

Gibt es eine Möglichkeit, einen einfachen 1-Pixel-Tabellenrahmen mit HTML in einem JTextPane zu haben?

Antwort

3

Hier ist ein komplettes Beispiel:

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 für [sscce] (http://sscce.org/); "SystemColor" ist auch eine nette Geste. – trashgod

1

javax.swing.text.html basiert auf HTML 3.2, aber Sie können die Grenze Attribut des <table> Tag verwenden.

+0

Wenn border = 1 festgelegt wird, wird kein 1-Pixel-Rahmen erstellt, da jede Zelle über einen eigenen Rahmen verfügt. –

+0

@Chris B: Möglicherweise müssen Sie auch 'cellspacing' und' cellpadding' anpassen. – trashgod

0

trashgod ist richtig - Javas HTML-Unterstützung ist begrenzt - warum also nicht eine HTML-Problemumgehung verwenden? Platzieren Sie Ihren Tisch (ohne Rahmen) einfach in eine andere Tabelle mit einer Zelle, die einen Rahmen hat.

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

Es ist nicht die sauberste der Methoden, aber es geht um HTML 3.2 Einschränkungen.

8

Verwenden Sie eine Kombination von

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

Erstaunlicher Hack. Vielen Dank! –

+0

Große Problemumgehung! Um Wiederholungscode zu reduzieren, habe ich folgende Stile erstellt: '' 'table {border: 1px black solid; background-color: black;} '' 'und' '' td, th {Hintergrundfarbe: weiß; } '' ' – ruX

0

Hier ist ein Beispiel eine Grenze zu einer Tabelle auf bevorzugte Farbe in HTML 3.2 zu erstellen:

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