Results 1 to 14 of 14

Thread: Nice GUI

  1. #1
    karq is offline Member
    Join Date
    May 2010
    Posts
    63
    Rep Power
    0

    Default Nice GUI

    Hey!

    I designed my apps GUI with swing, but I dont like the windows metal look, is there a way to style GUI elements like jtextfields,buttons etc?

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    You can use different L&Fs
    Lesson: Modifying the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    to use ready made styles or you can override various methods from the UI classes.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  4. #4
    karq is offline Member
    Join Date
    May 2010
    Posts
    63
    Rep Power
    0

    Default

    Thanks for the hint :)

    Maybe someone can help me?

    So I made a class for my synth design
    Java Code:
    public class MainDesign {
    
        public MainDesign(){
    
            //Load design
            designLoader();
    
        }
    
    
        private void designLoader(){
    
    
            //Design element
            try{
            //Instantiation
            SynthLookAndFeel laf = new SynthLookAndFeel();
            
            laf.load(MainDesign.class.getResourceAsStream("design.xml"), MainDesign.class);
            UIManager.setLookAndFeel(laf);
            System.out.println("proov");
    
            }catch(ParseException e){System.out.println(e.getMessage());}
             //catch(IOException n){System.out.println(n.getMessage());}
             catch(UnsupportedLookAndFeelException k){System.out.println(k.getMessage());}
             
        }
    
    }

    Heres my xml file:
    Java Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <!--
        Document   : design.xml
        Created on : August 4, 2010, 8:38 PM
        Author     : kaarel 
        Description:
            Purpose of the document follows.
            Design for the gui
    -->
    
    <synth>
        <style id="default">
    
       <font name="Aharoni" size="12"/>
    
       <state>
    
          <color value="#e50707" type="FOREGROUND"/>
    
       </state>
    
    </style>
    
    <bind style="default" type="region" key=".*"/>
    
      
    </synth>
    And in the main gui class I
    loaded MainDesign design = new MainDesign();

    And if I run my app text should be red, but it isnt, what am I doing wrong?
    Last edited by karq; 08-04-2010 at 08:40 PM.

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,843
    Rep Power
    19

    Default

    I think you might want to take a look at Nimbus.
    That's the current customisable framework that has replaced synth (I think it might actually sit on top of synth, I'm not sure).

  6. #6
    karq is offline Member
    Join Date
    May 2010
    Posts
    63
    Rep Power
    0

    Default

    I tried nimbus, but something is really wrong.

    I used this code to check if nimbus is available
    Java Code:
    try {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            System.out.println(info.getName());
            
        }
    } catch (Exception e) {
        
    }
    And Yes it was available.

    So I gave it a try
    Java Code:
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    And when I run my app, then the gui looks like this


    This isnt nimbus?!?!?

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,843
    Rep Power
    19

    Default

    Not knowing what your app normally looks like I couldn't say.
    It appears on mine with no exceptions, and it's definitely different to the standard L&F.

    You got no exceptions running setLookAndFeel?
    Are you catching the exceptions and printing the stack trace?

  8. #8
    karq is offline Member
    Join Date
    May 2010
    Posts
    63
    Rep Power
    0

    Default

    Found the problem, but I dont know how to solve it.

    Heres an example:
    Java Code:
    public class Walkthrough extends JFrame {
      public Walkthrough() {
        super("Sample app");
        this.setLayout(new FlowLayout());
        this.add(new JButton("button"));
        this.add(new JCheckBox("check"));
        this.add(new JLabel("label"));
        this.add(new JList());
    
    
        this.setSize(new Dimension(250, 80));
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
      design des = new design(); <--------------------- DOSENT WORK
      static Walkthrough w = new Walkthrough();    <----------- DOSENT WORK
    
      public static void main(String[] args) {
        
    
        SwingUtilities.invokeLater(new Runnable() {
                @Override
          public void run() {
                 
            design des = new design(); <--------------------- WORKS
            Walkthrough w = new Walkthrough(); <----------------WORKS
            
            w.setVisible(true);
            
    
    
           
                    
    
    
          }
        });
      }
    }
    And the bad part is this that I have to declare walkthrough before the main part, because I use alot the w.setVisible(); option in other classes.

    Hope you got what I meant!

    Really need help on this :)

  9. #9
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,843
    Rep Power
    19

    Default

    What does "doesn't work" mean?
    What are you trying to do?

  10. #10
    karq is offline Member
    Join Date
    May 2010
    Posts
    63
    Rep Power
    0

    Default

    I'll try to explain it again.

    Heres the design class:
    Java Code:
    public class design {
    
        public design(){
    
            designLoad();
            
        }
    
    
    
    public void designLoad(){
    
            try {
              UIManager.setLookAndFeel("org.pushingpixels.substance.api.skin.SubstanceBusinessLookAndFeel");
            } catch (Exception e) {
              System.out.println("Substance Graphite failed to initialize");
            }
        }
    }

    Heres the working way:

    Java Code:
    public class Walkthrough extends JFrame {
      public Walkthrough() {
        super("Sample app");
        this.setLayout(new FlowLayout());
        this.add(new JButton("button"));
        this.add(new JCheckBox("check"));
        this.add(new JLabel("label"));
        this.add(new JList());
    
    
        this.setSize(new Dimension(250, 80));
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
      
      public static void main(String[] args) {
        
    
        SwingUtilities.invokeLater(new Runnable() {
                @Override
          public void run() {
                 
            design des = new design(); 
            Walkthrough w = new Walkthrough(); 
            
            w.setVisible(true);
            
    
    
           
                    
    
    
          }
        });
      }
    }
    And Heres the non working way:

    Java Code:
    public class Walkthrough extends JFrame {
      public Walkthrough() {
        super("Sample app");
        this.setLayout(new FlowLayout());
        this.add(new JButton("button"));
        this.add(new JCheckBox("check"));
        this.add(new JLabel("label"));
        this.add(new JList());
    
    
        this.setSize(new Dimension(250, 80));
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
      design des = new design(); 
      static Walkthrough w = new Walkthrough();    
    
      public static void main(String[] args) {
        
    
        SwingUtilities.invokeLater(new Runnable() {
                @Override
          public void run() {
                 
            
            
            w.setVisible(true);
            
    
    
           
                    
    
    
          }
        });
      }
    }

    And the problem is this, that I cant use the WORKING WAY, because I use the w.setVisible(); option in other classes and if I declare the design and walkthrough in the main part, then I cant use the w.setVisible(); option in other classes or am I wrong?

  11. #11
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,843
    Rep Power
    19

    Default

    Someone who's used Swing more than me might correct me, but isn't the default UI simply set during the invokeLater/run() method? Prior to creating everything?

    Why do you feel the need to do it in its own Design class?

  12. #12
    karq is offline Member
    Join Date
    May 2010
    Posts
    63
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Someone who's used Swing more than me might correct me, but isn't the default UI simply set during the invokeLater/run() method? Prior to creating everything?

    Why do you feel the need to do it in its own Design class?

    That's a greate question, maybe because I dont like to have to much code in one file. I'm a newbe so I really dont know what is a good/bad way to write code, theres only that way what works for me :D

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Quote Originally Posted by Tolls View Post
    Someone who's used Swing more than me might correct me, but isn't the default UI simply set during the invokeLater/run() method? Prior to creating everything?
    No, that's not it. The static member Walkthrough w is constructed when the class Walkthrough is loaded, which is *before* the construction of the instance w constructs its instance field design (should be Design, class names by convention start with an uppercase letter) des.

    Also, the excess vertical whitespace in your code only makes it more difficult to read.

    Without testing, I'd guess that invoking SwingUtilities.updateComponentTreeUI(w) at any point after Design des is constructed should [be a bad] fix [for] the issue.

    Bad, because static members are by and large evil (class constants excepted).

    db
    Last edited by DarrylBurke; 08-08-2010 at 08:10 PM.

  14. #14
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,843
    Rep Power
    19

    Default

    Quote Originally Posted by Darryl.Burke View Post
    No, that's not it. The static member Walkthrough w is constructed when the class Walkthrough is loaded, which is *before* the construction of the instance w constructs its instance field design (should be Design, class names by convention start with an uppercase letter) des.

    Also, the excess vertical whitespace in your code only makes it more difficult to read.

    Without testing, I'd guess that invoking SwingUtilities.updateComponentTreeUI(w) at any point after Design des is constructed should [be a bad] fix [for] the issue.

    Bad, because static members are by and large evil (class constants excepted).

    db
    Well, yes, but I was wondering why all the focus on a Design class in the first place. Remove the static, create the Walkthrough (or UI entrypoint, whatever that may be) in the invokeLater(), and include the LookAndFeel assignment in there. At least I think that's the sort of structure I've seen before. Something along the lines of:

    Java Code:
        SwingUtilities.invokeLater(new Runnable() {
                @Override
          public void run() {
            try {
                UIManager.setLookAndFeel("org.pushingpixels.substance.api.skin.SubstanceBusinessLookAndFeel");
            } catch (Exception e) {
              System.out.println("Substance Graphite failed to initialize");
                // Does setLookAndFeel default at this point?
            }
            // Build the JFrame or whatever here.
          }
        });
    Also isn't extending JFrame generally frowned upon?

Similar Threads

  1. nice question
    By pavankumar2215 in forum New To Java
    Replies: 17
    Last Post: 06-27-2010, 03:46 AM
  2. Replies: 3
    Last Post: 03-10-2010, 05:51 PM
  3. Nice Forum
    By VijayKumar23 in forum Introductions
    Replies: 0
    Last Post: 04-15-2009, 12:22 PM
  4. [SOLVED] Building a nice GUI applet?
    By bobleny in forum New To Java
    Replies: 3
    Last Post: 05-09-2008, 07:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •