Avatar of kristian_gr
kristian_grFlag for Norway asked on

Opening, changing and saving a css file

I'm in a bit of a hurry here, so without r.t.f.m, I ask you:
I need to create my own Style Sheet editor.
I have a File (test.css) that I need to open.
Then I need to edit some of the styles, before I save the file.

Any help is appreciated
Best regards

Avatar of undefined
Last Comment

8/22/2022 - Mon
Mick Barry

wouldn't u be better off using an existing one (such as in eclipse)
building your own isn't a small job

I'd preffer not.
My need for this is rather spesial, so one way or another I would need to recreate the gui.
I thought that would be the larges part of the job?

Do you need color coding? If not, why don't you use a simple text-editor? Then even a simple textarea element in a html-form could do the trick.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy

You will just need to edit and save the files, or you must have syntax highlighting and all that stuff?

no need for any higlighting or text editor at this point. I'm starting easy.
First and formost, I need to read a localfile (a css file) in a way that I end up with a StyleSheet object.
Then I need to write that StyleSeet object to a new file.


So that's basically a normal text file. I don't believe there is anything special about CSS here, well maybe the .css file extension.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Mick Barry

JEditorPane would be a good starting point

Hi there, hope you can help me some more here.
Consider the following code, I got two questions:
1.      How can I read a StyleSheet from a local file (File f = new File(c:/test/test.css));
2.      The code prints the name of the style, and the attributes, but who do I get the attribute value?

HTMLEditorKit kit = new HTMLEditorKit();
            HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
            FileReader file = new FileReader("C:/test/test.html");
            kit.read(file, doc, 0);
            StyleSheet styles = doc.getStyleSheet();
            Enumeration enu = styles.getStyleNames();
            while (enu.hasMoreElements()) {
                String name = (String) enu.nextElement();
                System.out.println(" Style name: " + name);
                Style rule = styles.getStyle(name);
                Enumeration enuRule = rule.getAttributeNames();
                while (enuRule.hasMoreElements()) {
                    Object o = (Object) enuRule.nextElement();
                    if (o instanceof CSS.Attribute) {
                        CSS.Attribute a = (CSS.Attribute) o;
                        System.out.println("\tAttribute:" + a.toString());

<style type="text/css">
      body {
            background-color: #0033FF;
      #test {
            font-family: Arial, Helvetica, sans-serif;
            background-color: #CCCCCC;
            padding: 15px;
      <div id="test">test</div>
Mick Barry

1. http://www.objects.com.au/java/qa/1052695124.html
shows how to read file, or if using a JEditorPane it'll do the reading for you

2. style.getAttribute(a);
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck

Could not find any (StyleSheet)     styles.getAtribute(a).
I'm guessing you mean (Style)    rule.getAttribute(a).

I've tried this and it lead me to yet another question:
Object ob = rule.getAttribute(a);
System.out.println("objectName:" + ob.getClass().getName());
result: "objectName:javax.swing.text.html.CSS$FontFamily"

CSS$FontFamily <- is the problem. I can't find any CSS.FontFamily.
And the Object is not CSS?

 I'm trying to do something as :
if(ob instanceof CSS) {
     System.out.println("ob = true--------------");
Mick Barry

> I'm guessing you mean (Style)    rule.getAttribute(a).

sorry, yes thats what i meant

Mick Barry

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

hmm, ther must be a way to get the value for ex: #test   font-family
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Mick Barry

have u tried toString() ?