Change JFrame's background in Sea-Glass Look and Feel - java

What's is the proper way to change to background of JFrame with Sea-Glass Look and Feel, so far I tried both :
frame.getContentPane().setBackground(Color.blue);
and
JPanel x = new JPanel();
x.setBackground(Color.red);
frame.setContentPane(x);
But it didn't make any effect :
import javax.swing.*;
import com.seaglasslookandfeel.*;
import java.awt.*;
import java.awt.Color.*;
public class BORDER_TEST {
public static void main(String[] a){
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel("com.seaglasslookandfeel.SeaGlassLookAndFeel");
JFrame frame = new JFrame();
JPanel x = new JPanel();
x.setBackground(Color.red);
frame.setContentPane(x);
//frame.getContentPane().setBackground(Color.blue);
frame.setPreferredSize(new Dimension(900,350));
frame.setAlwaysOnTop(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}

Seaglass Look and Feel is based on Nimbus, then you have to accepting its built-in themes or set Colors for Nimbus programatically

Related

Why can't I add a Component to a JFrame with a MouseListener?

I have a JFrame. When I click in the frame, I want to add a Component (in this case an extension of a Canvas). I added a MouseListener to the frame as follows:
frame.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
frame.add(canvas);
frame.repaint();
}
});
However, the component is not added when the mouse is clicked on the frame. I have no problem adding the component in my main method. Adding a print statement in the mouse listener prints correctly, as does removing components from the frame.
Like #sprinter9 said, add frame.pack() before repainting.
Try below
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Frame {
public static void main(String[] args){
final JFrame frame = new JFrame("Add Component");
final Canvas canvas = new Canvas();
canvas.setBackground(Color.BLACK);
frame.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.out.println("Clicked");
frame.add(canvas);
frame.pack();
frame.repaint();
}
});
frame.setMinimumSize(new Dimension(320,240));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Demo, http://kurungkurawal.com/gifs/frame-add-component.gif
You are adding the component directly to the JFrame. You should use it's contentPane instead:
frame.getContentPane().add(canvas);

How do I change the colour of the focus ring in a JCheckBox?

Our Swing GUI has a black panel with white controls. However the JCheckBox instance on the panel always shows the focus ring in black. It seems to ignore the foreground colour when rendering the focus ring. Here's an example, where I've set the content pane background to grey so that the focus ring is visible:
Here's the code I'm using:
import javax.swing.*;
import java.awt.*;
public class ScratchSpace {
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JCheckBox checkBox = new JCheckBox("Hello cruel world");
checkBox.setForeground(Color.WHITE);
checkBox.setOpaque(false);
JPanel contentPane = new JPanel();
contentPane.setOpaque(true);
contentPane.setBackground(new Color(0.5f, 0.5f, 0.5f));
contentPane.add(checkBox);
JFrame frame = new JFrame();
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
How can I tell a JCheckBox to render the focus ring in a specific colour? Ideally it would use the control's foreground colour.
You could try changing the look and feel property CheckBox.focus, note, doing this will effect ALL JCheckBoxs...
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestCheckBox {
public static void main(String[] args) {
new TestCheckBox();
}
public TestCheckBox() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
UIManager.put("CheckBox.focus", Color.RED);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.add(new JCheckBox("Hello world"));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}

How to change default operating system's frame around swing application

For example instead of the default operating system container around the application you could have something custom like this swing project below.
You can use a transparent image on a undecorated frame with a transparent background:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TransparentImageFrame
{
private static void createAndShowUI()
{
JLabel label = new JLabel( new ImageIcon("...") );
label.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount() == 2)
{
System.exit(0);
}
}
});
JFrame frame = new JFrame("Image Frame");
frame.setUndecorated(true);
frame.setBackground(new Color(0, 0, 0, 0));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add( label );
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
The mouse will only respond to non-opaque pixels in whatever image you use.
The host OS owns the frame decorations, but you can Create Translucent and Shaped Windows and use Frame#setUndecorated(), as shown here.

How to fix annoying white splash in this code?

The problem is when I run the code below, I see a white splash on my screen for about maybe 100ms which is extremely annoying:
import java.awt.*;
import javax.swing.*;
public class MyPanel extends JPanel {
public MyPanel() {
this.setBackground(Color.black);
JTextArea jtext = new JTextArea(10, 20);
jtext.setBackground(Color.black);
jtext.setForeground(Color.white);
jtext.setText("some stuff here");
add(jtext);
add(new JButton("some button"));
// TODO: gui elements
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("MyPanel");
frame.getContentPane().add(new MyPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
});
}
}
My entire work setup is in dark theme. Equivilant code written in C++ with Qt does not show any white splashs on the same system. I'm using Ubuntu 12.04 and Intel HD Graphics 3000, java version "1.7.0_45".
Please let me know how this can be fixed.
I googled this problem but all I found was: "how to create splash screen in java?" which what I'm trying to avoid.
update: solution: I'm not sure why this works:
I extended MyPanel to JFrame and put most of the functions there. then in the run() invoked it and set visible to true.
here is the code:
import java.awt.*;
import javax.swing.*;
public class MyPanel extends JFrame {
public MyPanel() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBackground(Color.black);
JTextArea jtext = new JTextArea(10, 20);
jtext.setBackground(Color.black);
jtext.setForeground(Color.white);
jtext.setText("some stuff here");
getContentPane().add(jtext);
// TODO: gui elements
setExtendedState(JFrame.MAXIMIZED_BOTH);
setSize(350, 250);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MyPanel mypanel = new MyPanel();
mypanel.setVisible(true);
}
});
}
}
I did not see the 'white flash' here, but try this altered code that calls pack() on the frame.
import java.awt.*;
import javax.swing.*;
public class MyPanel extends JPanel {
public MyPanel() {
this.setBackground(Color.black);
JTextArea jtext = new JTextArea(10, 20);
jtext.setBackground(Color.black);
jtext.setForeground(Color.white);
jtext.setText("some stuff here");
add(jtext);
add(new JButton("some button"));
// TODO: gui elements
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("MyPanel");
frame.getContentPane().add(new MyPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.pack();
frame.setVisible(true);
}
});
}
}
Although it looks normal to me on Ubuntu 12.04 / Intel Iris Pro 5200 / Java 6, I can see the effect by running in a slowed emulator. Setting the frame's background to black before setVisible() seems to help.
frame.setBackground(Color.black);

JFrame minimize icon query?

I was able to hide the the maximize icon by setting setResizable(false), how can achieve the same for the minimize icon?
Here is the way, how to remove min/max buttons from JFrame
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JFrameTest extends JDialog {
public JFrameTest(JFrame frame, String str) {
super(frame, str);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
public static void main(String[] args) {
JFrameTest frame = new JFrameTest(new JFrame(), "Title");
JPanel panel = new JPanel();
panel.setSize(200, 200);
JLabel lbl = new JLabel("JFrame without max/min");
panel.add(lbl);
frame.add(panel);
frame.setSize(400, 400);
frame.setVisible(true);
}
}
But it's really nasty to users and could be walk arounded by key shortcuts etc.
Check the example Specifying Window Decorations

Categories