Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use CSS for Java Swing?

Tags:

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

like image 683
ricardo Avatar asked May 10 '11 21:05

ricardo


People also ask

Can we use CSS in Java Swing?

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.

Can we use HTML in Java Swing?

HTML formatting can be used in all Swing buttons, menu items, labels, tool tips, and tabbed panes, as well as in components such as trees and tables that use labels to render text.

What design pattern does Java Swing use?

The Swing toolkit uses a modified MVC design pattern. It has a single UI object for both the view and the controller. This modified MVC is sometimes called a separable model architecture. In the Swing toolkit, every component has its model, even the basic ones like buttons.

Is Java Swing still used in 2020?

Absolutely yes. Legacy swing applications are still supported and enhanced.


2 Answers

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; } 
like image 176
Kyle Avatar answered Oct 22 '22 09:10

Kyle


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

like image 22
StanislavL Avatar answered Oct 22 '22 10:10

StanislavL