Java Qt Extensions

Examples

Example7 - Style Sheet

An example of applying style sheet to components.

Thanks to Qt, style sheets can be now applied to components, too. QSwing uses CSS-like syntax for describing style sheets. The main difference is that case-sensitive component names and mangled fully-qualified class names (dots (.) replaced with underscores (_)) are used as rule selectors (see the style sheet below).

Code

package yu.ac.bg.etf.javaqx.examples;

import yu.ac.bg.etf.javaqx.qswing.JQButton;
import yu.ac.bg.etf.javaqx.qswing.JQContainer;
import yu.ac.bg.etf.javaqx.qswing.JQLabel;
import yu.ac.bg.etf.javaqx.qswing.JQTextField;
import yu.ac.bg.etf.javaqx.qswing.QSwing;
import yu.ac.bg.etf.javaqx.qswing.events.ActionEvent;
import yu.ac.bg.etf.javaqx.qswing.events.ActionListener;
import yu.ac.bg.etf.javaqx.qswing.layouts.formlayout.CellConstraints;
import yu.ac.bg.etf.javaqx.qswing.layouts.formlayout.FormLayout;

/**
 * Style Sheet Example.
 */
public class Example7StyleSheet extends JQContainer {

  public Example7StyleSheet() {
    super(new FormLayout("6dlu, l:p, 4dlu, p:g, max(46dlu;p), 6dlu",
                         "6dlu, p, 3dlu, p, 3dlu, p, 4dlu, p, 6dlu"));
    setName("StyleSheet");
    CellConstraints cc = new CellConstraints();
    add(new JQLabel("Name:"), cc.xy(22));
    add(new JQTextField(), cc.xyw(422));
    add(new JQLabel("Phone:"), cc.xy(24));
    add(new JQTextField(), cc.xyw(442));
    add(new JQLabel("EMail:"), cc.xy(26));
    add(new JQTextField(), cc.xyw(462));
    JQButton closeButton = new JQButton("Close");
    closeButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        QSwing.stopEventLoop();
      }
    });
    add(closeButton, cc.xy(58));
  }

  public static void main(String[] args) {
    QSwing.setStyleSheetSafely("stylesheet/stylesheet.css");
    Example7StyleSheet styleSheetExample = new Example7StyleSheet();
    styleSheetExample.setVisible(true);
    QSwing.startEventLoop(true);
  }

}

Style Sheet


yu_ac_bg_etf_javaqx_qswing_JQComponent {
  background-color: #B4B4B4;
  font: bold;
}

yu_ac_bg_etf_javaqx_qswing_JQTextField {
  border-image: url(stylesheet/TextFieldBorder.png) 0;
  border-width: 5;
  padding: 2;
}

yu_ac_bg_etf_javaqx_qswing_JQTextField:hover {
  border-image: url(stylesheet/TextFieldBorder.png) 0;
  border-width: 4;
  padding: 2;
}

yu_ac_bg_etf_javaqx_qswing_JQButton {
  border-image: url(stylesheet/ButtonBorder.png) 5;
  border-width: 5;
}

yu_ac_bg_etf_javaqx_qswing_JQButton:hover {
  border-image: url(stylesheet/ButtonBorderHover.png) 5;
  border-width: 5;
}

yu_ac_bg_etf_javaqx_qswing_JQButton:pressed {
  border-image: url(stylesheet/ButtonBorderPressed.png) 5;
  border-width: 5;
}

Screenshots

StyleSheet on
Windows Vista
StyleSheet on
Windows XP
StyleSheet on
Windows 98
StyleSheet on
SUSE Linux
StyleSheet on
Ubuntu Linux