Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
353 views
in Technique[技术] by (71.8m points)

Vertically centering text in HTML table cell in Java JLabel

I have an HTML table (styles with CSS) that is displayed in a JLabel. I would like the contents (a single, short line of text) of the cells to be both horizontally and vertically centered. Horizontal centering is easy, but I cannot seem to center the text vertically. I've tried vertical-align: and valign: with both middle and center arguments. I've looked at several of the tricks, but none of them seemed doable, and the ones I tried didn't work.

What I have now:

Inline (in the <style> tag) CSS:

table.outer {
    background:#F0F0F0;
    border-collapse:collapse;
    border:none;
}

td.outer {
    border-style:solid;
    border-width:1pt;
    border-color:#888888;
    padding:0pt 0pt 0pt 0pt;
}

table.inner {
    border-collapse:collapse;
    border:none;
}

td.title {
    width:75pt;
    background:#BFBFBF;
    padding:1.5pt 0pt 1.5pt 0pt;
}

The relevant portion of the HTML:

<body bgcolor=#F0F0F0>
<table class='outer'>
  <tr>
    <td class='outer' valign=top>
    <table class='inner'>
      <tr>
        <td class='title' rowspan=3>
          <p class='title'>Current Run</p>
        </td>
      </tr>
    </table>
    </td>
  </tr>
</table>
</body>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Support for HTML in Swing Components is limited to 3.2, but valign="middle" should work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...