24

Is there any way to reuse my CSS in an application that uses Java Swing ?

Sled
  • 18,541
  • 27
  • 119
  • 168
ricardo
  • 621
  • 1
  • 11
  • 22
  • 2
    You already have a CSS file you use for something else? A web page perhaps? What would you like to use CSS for? Just colors and font sizes or layout and behaviour? – Pål Brattberg May 10 '11 at 21:53
  • Yes, I have CSS file that is used in my web application, but in my web application, i´m using one applet developed in swing. So, I want to know if exist possibilities of reuse my css. – ricardo May 11 '11 at 12:46
  • 1
    Possible dublicate: http://stackoverflow.com/questions/1057137/css-with-swing – javascript is future Aug 06 '12 at 08:50
  • I don't know if we have something that parse the css style, but for Swing application I have been using [Miglayout](http://www.miglayout.com/), that is not a translation of css, but it follows most of patterns – Jonatas Emidio Dec 14 '18 at 21:51

2 Answers2

10

Java swing generally isn't built to separate its controls from its presentation, but there is an open-source framework called Jaxx that has been written that might help you. With Jaxx you can do something like this:

<Application title='Calculator'>
  <style source='Calculator.css'/> //your style goes here...
  <script source='Calculator.script'/>
  <Table fill='both' id='table'>
<row>
  <cell columns='4'><JLabel id='display' text='0'/></cell>
</row>

<row>
  <cell columns='2'><JButton id='c' label='C' onActionPerformed='clear()' styleClass='clear'/></cell>      
  <cell><JButton id='ce'     label='CE' onActionPerformed='clearEntry()' styleClass='clear'/></cell>
  <cell><JButton id='equals' label='=' onActionPerformed='equal()' styleClass='operator'/></cell>
</row>

<row>
  <cell><JButton id='d7'   label='7' onActionPerformed='digit(7)' styleClass='digit'/></cell>
  <cell><JButton id='d8'   label='8' onActionPerformed='digit(8)' styleClass='digit'/></cell>
  <cell><JButton id='d9'   label='9' onActionPerformed='digit(9)' styleClass='digit'/></cell>
  <cell><JButton id='plus' label='+' onActionPerformed='add()' styleClass='operator'/></cell>
</row>

<row> 
  <cell><JButton id='d4'       label='4' onActionPerformed='digit(4)'   styleClass='digit'/></cell>
  <cell><JButton id='d5'       label='5' onActionPerformed='digit(5)'   styleClass='digit'/></cell>
  <cell><JButton id='d6'       label='6' onActionPerformed='digit(6)'   styleClass='digit'/></cell>
  <cell><JButton id='subtract' label='-' onActionPerformed='subtract()' styleClass='operator'/></cell>
</row>

<row>
  <cell><JButton id='d1'       label='1' onActionPerformed='digit(1)' styleClass='digit'/></cell>
  <cell><JButton id='d2'       label='2' onActionPerformed='digit(2)' styleClass='digit'/></cell>
  <cell><JButton id='d3'       label='3' onActionPerformed='digit(3)' styleClass='digit'/></cell>
  <cell><JButton id='multiply' label='x' onActionPerformed='multiply()' styleClass='operator'/></cell>
</row>

<row>
  <cell><JButton id='d0'     label='0' onActionPerformed='digit(0)' styleClass='digit'/></cell>
  <cell><JButton id='sign'   label='+/-' onActionPerformed='toggleSign()' styleClass='operator'/></cell>
  <cell><JButton id='dot'    label='.' onActionPerformed='dot()' styleClass='digit'/></cell>
  <cell><JButton id='divide' label='&#x00F7;' onActionPerformed='divide()' styleClass='operator'/></cell>
</row>

and then include a css file to style your components:

Application {
    lookAndFeel: system;
}
#display {
    background: #BCE5AD;
    opaque: true;
    horizontalAlignment: right;
    border: {BorderFactory.createBevelBorder(BevelBorder.LOWERED)};
    font-size: 22;
    font-weight: bold;
}
Kyle
  • 4,202
  • 1
  • 33
  • 41
8

http://code.google.com/p/flying-saucer/ Flying Saucer takes XML or XHTML and applies CSS 2.1-compliant stylesheets to it, in order to render to PDF (via iText), images, and on-screen using Swing or SWT

StanislavL
  • 56,971
  • 9
  • 68
  • 98