• We just launched and are currently in beta. Join us as we build and grow the community.

Change User Interface(UI) in Java

slimestio

Network Pentester
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
151
Likes
61
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
This tutorial will teach you how to create a program that can choose and change the User Interface(UI) that is installed already in java.

So, now let's start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of UILookAndFeel.java.

2. Import the following package library:
  1. import

    java.awt.event.*

    ;

    //used to access the ActionEvent and ActionListener class
  2. import

    java.awt.*

    ;

    //used to access the GridLayout class
  3. import

    javax.swing.*

    ;

    //used to access the JButton,JList,JTextArea,JFrame,JPanel,SwingUtilities and UIManager class

3. Initialize the following variable components in your Main.
  1. final

    JFrame

    frame =

    new

    JFrame

    (

    "Change User Interface"

    )

    ;
  2. JList

    comboBox =

    new

    JList

    (

    new

    String

    [

    ]

    {

    "www.sourcecodester.com"

    , "This is a List."

    }

    )

    ;
  3. JTextArea

    txtArea =

    new

    JTextArea

    (

    "www.facebook.com/BermzISware \n

    This is a TextArea."

    )

    ;
  4. JPanel

    panel =

    new

    JPanel

    (

    )

    ;

4. To have an event for changing the UI of the frame, we will use the ActionListener and when clicking a particular component it will change the UI directly.
  1. ActionListener

    changeUI =

    new

    ActionListener

    (

    )

    {
  2. public

    void

    actionPerformed(

    ActionEvent

    actionEvent)

    {
  3. String

    plaf =

    null

    ;
  4. plaf =

    actionEvent.getActionCommand

    (

    )

    ;
  5. String

    finalLafClassName =

    plaf;
  6. try

    {
  7. UIManager

    .setLookAndFeel

    (

    finalLafClassName)

    ;
  8. SwingUtilities

    .updateComponentTreeUI

    (

    frame)

    ;
  9. }

    catch

    (

    Exception

    exception)

    {
  10. System

    .out

    .print

    (

    "Can't change look and feel."

    )

    ;
  11. }

  12. }
  13. }

    ;

To get all the installed User Interface in java, have this code below:
  1. UIManager.LookAndFeelInfo

    UIlooks[

    ]

    =

    UIManager

    .getInstalledLookAndFeels

    (

    )

    ;

5. Now, create a JButton component in which it will get all the installed UI in your java software. This will also call the ActionListener that we have declared above when clicking the button.
  1. for

    (

    int

    i =

    0

    , n =

    UIlooks.length

    ;

    i <

    n;

    i++

    )

    {
  2. JButton

    button =

    new

    JButton

    (

    UIlooks[

    i]

    .getName

    (

    )

    )

    ;
  3. button.setActionCommand

    (

    UIlooks[

    i]

    .getClassName

    (

    )

    )

    ;
  4. button.addActionListener

    (

    changeUI)

    ;
  5. panel.add

    (

    button)

    ;
  6. }

6. Lastly, add all the components, set the layout into GridLayout, set the size, visibility, and the close operation of the frame. Have this code below:
  1. frame.getContentPane

    (

    )

    .setLayout

    (

    new

    GridLayout

    (

    1

    ,1

    )

    )

    ;
  2. frame.getContentPane

    (

    )

    .add

    (

    comboBox)

    ;
  3. frame.getContentPane

    (

    )

    .add

    (

    txtArea)

    ;
  4. frame.getContentPane

    (

    )

    .add

    (

    panel)

    ;
  5. frame.setSize

    (

    580

    , 250

    )

    ;
  6. frame.setVisible

    (

    true

    )

    ;
  7. frame.setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;


Output:

uijava.png


Here's the full code of this tutorial:

  1. import

    java.awt.event.*

    ;

    //used to access the ActionEvent and ActionListener class
  2. import

    java.awt.*

    ;

    //used to access the GridLayout class
  3. import

    javax.swing.*

    ;

    //used to access the JButton,JList,JTextArea,JFrame,JPanel,SwingUtilities and UIManager class

  4. public

    class

    UILookAndFeel {

  5. public

    static

    void

    main(

    String

    args[

    ]

    )

    {
  6. final

    JFrame

    frame =

    new

    JFrame

    (

    "Change User Interface"

    )

    ;
  7. JList

    comboBox =

    new

    JList

    (

    new

    String

    [

    ]

    {

    "www.sourcecodester.com"

    , "This is a List."

    }

    )

    ;
  8. JTextArea

    txtArea =

    new

    JTextArea

    (

    "www.facebook.com/BermzISware \n

    This is a TextArea."

    )

    ;
  9. JPanel

    panel =

    new

    JPanel

    (

    )

    ;


  10. ActionListener

    changeUI =

    new

    ActionListener

    (

    )

    {
  11. public

    void

    actionPerformed(

    ActionEvent

    actionEvent)

    {
  12. String

    plaf =

    null

    ;
  13. plaf =

    actionEvent.getActionCommand

    (

    )

    ;
  14. String

    finalLafClassName =

    plaf;
  15. try

    {
  16. UIManager

    .setLookAndFeel

    (

    finalLafClassName)

    ;
  17. SwingUtilities

    .updateComponentTreeUI

    (

    frame)

    ;
  18. }

    catch

    (

    Exception

    exception)

    {
  19. System

    .out

    .print

    (

    "Can't change look and feel."

    )

    ;
  20. }

  21. }
  22. }

    ;

  23. UIManager.LookAndFeelInfo

    UIlooks[

    ]

    =

    UIManager

    .getInstalledLookAndFeels

    (

    )

    ;


  24. for

    (

    int

    i =

    0

    , n =

    UIlooks.length

    ;

    i <

    n;

    i++

    )

    {
  25. JButton

    button =

    new

    JButton

    (

    UIlooks[

    i]

    .getName

    (

    )

    )

    ;
  26. button.setActionCommand

    (

    UIlooks[

    i]

    .getClassName

    (

    )

    )

    ;
  27. button.addActionListener

    (

    changeUI)

    ;
  28. panel.add

    (

    button)

    ;
  29. }

  30. frame.getContentPane

    (

    )

    .setLayout

    (

    new

    GridLayout

    (

    1

    ,1

    )

    )

    ;
  31. frame.getContentPane

    (

    )

    .add

    (

    comboBox)

    ;
  32. frame.getContentPane

    (

    )

    .add

    (

    txtArea)

    ;
  33. frame.getContentPane

    (

    )

    .add

    (

    panel)

    ;
  34. frame.setSize

    (

    580

    , 250

    )

    ;
  35. frame.setVisible

    (

    true

    )

    ;
  36. frame.setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;
  37. }
  38. }

For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.

Best Regards,

Engr. Lyndon R. Bermoy

IT Instructor/System Developer/Android Developer

Mobile: 09079373999

Telephone: 826-9296

E-mail:[email protected]

Visit and like my page on Facebook at: Bermz ISware Solutions

Subscribe at my YouTube Channel at: SerBermz

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

442,401

317,942

317,951

Top