24

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

4
  • 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? Commented May 10, 2011 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. Commented May 11, 2011 at 12:46
  • 1
    Possible dublicate: stackoverflow.com/questions/1057137/css-with-swing Commented Aug 6, 2012 at 8:50
  • I don't know if we have something that parse the css style, but for Swing application I have been using Miglayout, that is not a translation of css, but it follows most of patterns Commented Dec 14, 2018 at 21:51

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.