Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Stupid DefaultMetalTheme problem  RSS feed

 
Michael Szul
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to change the color of the Default Metal theme, but seem to be having a problem. Only certain elements are changing color. I opened up the demo programs in the SDK (MetalWorks, SwingSet) and looked at their code for changing the theme and they mimic mine. I can see where the problem is. Any help would be appreciated. Below is a small app consisting of two files.
the theme changing code is here:
import javax.swing.plaf.*;
import javax.swing.plaf.metal.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;

public class PumpkinTheme extends DefaultMetalTheme {
public String getName() { return "Pumpkin"; }
private final ColorUIResource primary1 = new ColorUIResource(255, 140, 0);
private final ColorUIResource primary2 = new ColorUIResource(255, 161, 0);
private final ColorUIResource primary3 = new ColorUIResource(255, 191, 0);
protected ColorUIResource getPrimary1() { return primary1; }
protected ColorUIResource getPrimary2() { return primary2; }
protected ColorUIResource getPrimary3() { return primary3; }
}
and the code for Hermes.java is below:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.*;
import javax.swing.plaf.metal.*;
import javax.swing.tree.*;
import java.io.*;
public class Hermes extends JFrame {
//declare your global variables that will be used across the application.
public static JMenuBar menubar = new JMenuBar();
public static JDesktopPane desktop = new JDesktopPane();
public static JInternalFrame frame = new JInternalFrame("Explorer", true, true, true, true);
//Create the tree nodes for the left side of the application.
public static MutableTreeNode root = new DefaultMutableTreeNode("Case Files");
public static final DefaultTreeModel model = new DefaultTreeModel(root);
public static final JTree tree = new JTree(model);
public static JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(tree), desktop);
public Hermes() {
super("Hermes v0.1");
setSize(600,400);
setLocation(0,0);
createTree();
JMenu help = new JMenu("Help");
help.setMnemonic(KeyEvent.VK_A);
JMenuItem about = new JMenuItem("About Hermes");
about.setMnemonic(KeyEvent.VK_A);
about.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, Event.CTRL_MASK));
about.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showAbout();
}
});
JMenuItem quit = new JMenuItem("Quit");
quit.setMnemonic(KeyEvent.VK_Q);
quit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, Event.CTRL_MASK));
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
help.add(about);
help.addSeparator();
help.add(quit);
JMenu view = new JMenu("View");
view.setMnemonic(KeyEvent.VK_V);
JMenuItem explorer = new JMenuItem("Explorer");
explorer.setMnemonic(KeyEvent.VK_E);
explorer.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, Event.CTRL_MASK));
explorer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
openExplorer();
}
});
view.add(explorer);
JMenuItem metal = new JMenuItem("Metal");
metal.setMnemonic(KeyEvent.VK_D);
metal.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, Event.CTRL_MASK));
metal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
viewMetal();
}
});
view.add(metal);
JMenuItem motif = new JMenuItem("Motif");
motif.setMnemonic(KeyEvent.VK_M);
motif.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, Event.CTRL_MASK));
motif.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
viewMotif();
}
});
view.add(motif);
menubar.add(help);
menubar.add(view);
}
public void showAbout() {
final JFrame sa = new JFrame("About Hermes v0.1");
sa.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
sa.setVisible(false);
}
});
sa.setSize(300,300);
sa.setLocation(200,200);
sa.setVisible(true);
}
public void openExplorer() {
try {
if(frame.isVisible()) {
frame.setVisible(false);
desktop.remove(frame);
}
else {
frame.setSize(425,325);
frame.setLocation(0,0);
frame.setVisible(true);
desktop.add(frame);
}
}
catch(Exception e) {
System.out.println("Explorer already open: " + e);
}
}
public void viewMetal() {
try {
UIManager.setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel());
SwingUtilities.updateComponentTreeUI(menubar);
SwingUtilities.updateComponentTreeUI(desktop);
SwingUtilities.updateComponentTreeUI(frame);
SwingUtilities.updateComponentTreeUI(tree);
SwingUtilities.updateComponentTreeUI(split);
}
catch (UnsupportedLookAndFeelException e) {
System.out.println("Metal isn't Supported " + e);
}
}
public void viewMotif() {
try {
UIManager.setLookAndFeel(new com.sun.java.swing.plaf.motif.MotifLookAndFeel());
SwingUtilities.updateComponentTreeUI(menubar);
SwingUtilities.updateComponentTreeUI(desktop);
SwingUtilities.updateComponentTreeUI(frame);
SwingUtilities.updateComponentTreeUI(tree);
SwingUtilities.updateComponentTreeUI(split);
}
catch (UnsupportedLookAndFeelException e) {
System.out.println("Motif isn't Supported " + e);
}
}
public void createTree() {
File file = new File("Cases");
if(file.isDirectory()) {
String[] files = file.list();
for(int i = 0; i < files.length; i++) {
int count = i;
MutableTreeNode node = new DefaultMutableTreeNode(files[i]);
root.insert(node, count);
File nextDir = new File ("Cases/" + files[i]);
if(nextDir.isDirectory()) {
String[] nextFiles = nextDir.list();
for(int n = 0; n < nextFiles.length; n++) {
int secondCount = n;
MutableTreeNode node2 = new DefaultMutableTreeNode(nextFiles[n]);
node.insert(node2, secondCount);
}
}
}
}
}
public static void main (String argv[]) {
//The next line is commented out until I can get the theme to work properly.
MetalLookAndFeel.setCurrentTheme(new PumpkinTheme());
split.setDividerLocation(150);
split.setOneTouchExpandable(true);
frame.setSize(425,325);
frame.setLocation(0,0);
frame.setVisible(true);
desktop.add(frame);
JFrame main = new Hermes();
main.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
main.setJMenuBar(menubar);
main.getContentPane().add(split);
main.setVisible(true);
}
}
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,
I think you forgot the other three final values.
secondary1, secondary2, secondary3
If you use these then more colors will change.
Regards,
Manfred.
 
Michael Szul
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had tried them once. I'll plug them back in to see if it makes a difference, but it's my understanding (and I could be wrong) that the secondary colors are shades of gray.
In addition, the demo's that come with the SDK from Sun only change the primary colors.
Some examples of my problem would be:
1. Tree nodes are orange but the highlighter remains purple.
2. Menu outlines are orange, but again highlighted selection remains purple.
3. JInternalFrame is orange, but the drag control (the bumpy section that lets you drag around the frame) remains purple.
4. JInternalFrame is orange, but the JDesktopPane background and JInternalFrame background remain purple.
I'll try that secondary thing again, but I don't think it's the problem.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!