The Artima Developer Community
Sponsored Link

Java Answers Forum
Help with code modification

2 replies on 1 page. Most recent reply: Apr 11, 2003 12:08 PM by Blake MacKay

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Blake MacKay

Posts: 5
Nickname: newtojava
Registered: Feb, 2003

Help with code modification Posted: Apr 10, 2003 6:46 PM
Reply to this message Reply
Advertisement
I am trying to change this code from doing a System.out.println to display a text field in the box that says Look and Feel: "Metal(or Motif or Windows)look in effect. Can anyone help? Thanks in advance
Here is the code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class TestChangePLAF extends JFrame {
private JLabel label;

JButton btnMetal, btnWindows, btnMotif;
TestChangePLAF app;

public static void main( String args[]) {
TestChangePLAF app = new TestChangePLAF();
app.app = app;
app.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
app.pack();
app.setVisible(true);
}

public TestChangePLAF() {
Container c = getContentPane();
c.setLayout(new BorderLayout());

ActionListener pl = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String strPLAF = new String();
try {
String s = e.getActionCommand();
System.out.println(s);




if (s.equals("Metal")) {
strPLAF = "javax.swing.plaf.metal.MetalLookAndFeel";
} else if (s.equals("Windows")) {
strPLAF = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
} else if (s.equals("Motif")) {
strPLAF = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
}
UIManager.setLookAndFeel(strPLAF);
SwingUtilities.updateComponentTreeUI(app);
app.pack();
} catch (Exception e2) {
e2.printStackTrace(System.err);
}
}
};

btnMetal = new JButton("Metal");
btnMetal.addActionListener(pl);

btnWindows = new JButton("Windows");
btnWindows.addActionListener(pl);

btnMotif = new JButton("Motif");
btnMotif.addActionListener(pl);

c.add(new JButton("Look and Feel ="), BorderLayout.NORTH);
// c.add(new JButton("South"), BorderLayout.SOUTH);
c.add(btnMetal, BorderLayout.EAST);
c.add(btnWindows, BorderLayout.CENTER);
c.add(btnMotif, BorderLayout.WEST);
}
}


Rahul

Posts: 52
Nickname: wildhorse
Registered: Oct, 2002

Re: Help with code modification Posted: Apr 11, 2003 12:35 AM
Reply to this message Reply
I've modified your program slightly

Declare: JTextField txtField
instantiate: txtField = new JTextField("");
add to container: c.add(txtField, BorderLayout.NORTH);
// commented out the button code (Look and Feel=)
fillin the text field: txtField.setText(strPLAF);

Check the relevant portions of the program:


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class TestChangePLAF extends JFrame {
private JLabel label;

JButton btnMetal, btnWindows, btnMotif;
JTextField txtField;
TestChangePLAF app;

public static void main( String args[]) {
TestChangePLAF app = new TestChangePLAF();
app.app = app;
app.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
app.pack();
app.setVisible(true);
}

public TestChangePLAF() {
Container c = getContentPane();
c.setLayout(new BorderLayout());

ActionListener pl = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String strPLAF = new String();
try {
String s = e.getActionCommand();
System.out.println(s);




if (s.equals("Metal")) {
strPLAF = "javax.swing.plaf.metal.MetalLookAndFeel";
} else if (s.equals("Windows")) {
strPLAF = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
} else if (s.equals("Motif")) {
strPLAF = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
}

txtField.setText(strPLAF);

UIManager.setLookAndFeel(strPLAF);
SwingUtilities.updateComponentTreeUI(app);
app.pack();
} catch (Exception e2) {
e2.printStackTrace(System.err);
}
}
};

btnMetal = new JButton("Metal");
btnMetal.addActionListener(pl);

btnWindows = new JButton("Windows");
btnWindows.addActionListener(pl);

btnMotif = new JButton("Motif");
btnMotif.addActionListener(pl);

txtField = new JTextField("");

//c.add(new JButton("Look and Feel ="), BorderLayout.NORTH);
c.add(txtField, BorderLayout.NORTH);
// c.add(new JButton("South"), BorderLayout.SOUTH);
c.add(btnMetal, BorderLayout.EAST);
c.add(btnWindows, BorderLayout.CENTER);
c.add(btnMotif, BorderLayout.WEST);
}
}

Blake MacKay

Posts: 5
Nickname: newtojava
Registered: Feb, 2003

Re: Help with code modification Posted: Apr 11, 2003 12:08 PM
Reply to this message Reply
Thank you for your help Rahul,
It works ok except for the text that is displayed when you select a button is "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" instead of "Look and Feel:"Windows Look in Effect"" or whichever buuton is selected. I still need to convert strPLAF to the corresponding text. Can you or someone help me with this please?
Thanks,
BM

Flat View: This topic has 2 replies on 1 page
Topic: help with  simple method code. Previous Topic   Next Topic Topic: Java Interface Help

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use