How to make a window look like this in Java? - java

How do I create a window which looks like this in Java:
I want that window layout, instead of the standard Windows-borders, and I don't know how this is called.
Edit: look and feel doesn't work for me:

If you want your Look and Feel to draw the window decoration (that's what the "border" is called), then you need to call JFrame.setDefaultLookAndFeelDecorated(true) before creating your JFrame objects and JDialog.setDefaultLookAndFeelDecorated(true) before creating your JDialog objects.

that's called look and feel, you can find a detailed explanation here http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

You will need to first set the look and feel to use the cross platform look and feel (As someone commented before it's called metal). Then before you create the Frame you need to request that the borders are drawn by the look and feel.
try
{
UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName());
}
catch (Exception e) { }
This will set the look and feel to the one you want. As the cross platform look and feel is metal in Sun's JRE.
// Get window decorations drawn by the look and feel.
JFrame.setDefaultLookAndFeelDecorated(true);
// Create the JFrame.
JFrame frame = new JFrame("A window");
And this will make the created JFrame have borders like you describe.

Add this to your main() method:
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(testjframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(testjframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(testjframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(testjframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}

To set windows look and feel for swing write following code in main method.
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("windows".equalsIgnoreCase(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception ex) {
System.out.println(e);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
MainFrame mainFrame = new MainFrame();
mainFrame.setExtendedState(MAXIMIZED_BOTH);
mainFrame.setVisible(true);
InitDatabaseDialog initDatabaseDialog = new InitDatabaseDialog(mainFrame, true);
initDatabaseDialog.setVisible(true);
}
});
}

Related

Can't add any component to JFrame - IllegalArgumentException

When I try to add anything to JFrame in constructor and I have IllegalArgumentException.
Similar code is working in documentation:
https://docs.oracle.com/javase/tutorial/uiswing/layout/none.html
Why my very simple code doesn't work? It is created in Netbeans 10.
Edit:
I also tried add label with position and sieze (setBounds) it doesn't help. I changed the code with setBounds method.
First, main class in JavaTestApp.java:
package javatestapp;
public class JavaTestApp {
public static void main(String[] args) {
TestForm mainFrame = new TestForm();
mainFrame.setLocation(300, 150);
mainFrame.setVisible(true);
mainFrame.toFront();
mainFrame.repaint();
}
}
Second file is:
package javatestapp;
import javax.swing.JLabel;
public class TestForm extends javax.swing.JFrame {
/**
* Creates new form TestForm
*/
public TestForm() {
initComponents();
JLabel label = new JLabel("Test label");
label.setBounds(10,10,100,25);
getContentPane().add(label);
}
// GENERATED CODE BELOW
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
pack();
}// </editor-fold>
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TestForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TestForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TestForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TestForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TestForm().setVisible(true);
}
});
}
Stacktrace, regarding to #VGR request:
Exception in thread "main" java.lang.IllegalArgumentException
at org.netbeans.lib.awtextra.AbsoluteLayout.addLayoutComponent(Unknown Source)
at java.awt.Container.addImpl(Container.java:1120)
at java.awt.Container.add(Container.java:410)
at javatestapp.TestForm.<init>(TestForm.java:16)
at javatestapp.JavaTestApp.main(JavaTestApp.java:5)
BUILD STOPPED (total time: 9 seconds)
Link to project uploaded on weetransfer:
Project (scanned via my Bitdefender antivir)
I also chosen absolute layout
And that is your problem. You are not using it correctly.
getContentPane().add(label);
You can't just add the label to the frame without specifying the proper constraints. I've never used AbsoluteLayout (because I believe in proper layout management) but I would guess you need to specify constraints like x, y, width, height.
The layout manager can't guess where you want to position the component so you need to specify all the information. That is why you should be using a layout manager. Then the layout manager will position the component based on the rules of the layout manager. Much easier, once you practice it a little.

How can I set the backgorund color for a JFrame using a JComboBox from another JFrame?

Actually, I'm trying to do a "Setting" window for a game and I want to set the background color of another window. I have no idea what to do. Some ideas pls?
You can implement it in different places. One of the ways is constructor, for example:
public YourClassPanel() {
// to set Look&Feel
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (ClassNotFoundException ex) {
Logger.getLogger(ControlPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(ControlPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(ControlPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(ControlPanel.class.getName()).log(Level.SEVERE, null, ex);
}
SwingUtilities.updateComponentTreeUI(this);
this.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
ControlPanel.tgp = null;
}
});
this.setBounds(0, 0, 710, 580);
this.setTitle("Buffer Allocation Panel");
this.setPreferredSize(null);
this.setResizable(false);
this.setBackground(Color.yellow); //to set Background
this.setForeground(Color.magenta); // to set Foreground
this.setOpaque(a!=255); // to set Opaque
}
Set a different color for JPanel background from Properties.

JOptionPane button size (Nimbus LAF)

Recently I've been working on a Swing project with Nimbus Look and Feel. I want to set all buttons in a JOptionPane to have the same size, but in vain.
import javax.swing.*;
public class NimbusTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
try {
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if (("Nimbus").equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
UIManager.put("OptionPane.sameSizeButtons", true);
String[] options = new String[]{"--------------------","short","1"};
int option = JOptionPane.showOptionDialog(null, "Nimbus problem", "JOptionPane", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
}
});
}
}
What I want is a JOptionPane with three buttons having the same size. However, I got the following result:
My code UIManager.put("OptionPane.sameSizeButtons", true); seems to be ignored. What should I do to create a JOptionPane with same size buttons if I don't want to recreate a JOptionPane-like dialog?
Replace
UIManager.put("OptionPane.sameSizeButtons", true);
with
UIManager.getLookAndFeelDefaults().put("OptionPane.sameSizeButtons", true);
And it works like a charm

Java software looks part Windows, part Metal

What I want is entirely WindowsLookAndFeel, and I have this code in my JFrame.
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception ex) {
ex.printStackTrace();
}
However, what I got it part Windows, part Metal (the cross platform style which looks awful. I think I know where the problem is:
The abnormal part(metal part) is a JPanel, and it is not originally built in the JFrame. I have a button. If pressed, the JPanel will be added to the JFrame.
if (((JToggleButton) e.getSource()).isSelected()) {
getContentPane().add(Console.getInstance(), BorderLayout.EAST);
} else {
remove(Console.getInstance());
}
revalidate();
pack();
But still, I don't know why this happens, and how to solve it.
You need to define the look and feel outside of the EDT. Create a class having a main method. In that method set the LAF and then display the app:
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch(Exception e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
jFrame.pack();
jFrame.setVisible(true);
}
});

Icons on JOptionPane

Sorry, this is really basic. My first experience with Java Swing using Eclipse. I'm trying to write a very simple JOptionPane. I want the question mark icon to appear, but all I am getting is the Java coffee cup icon. What am I doing wrong? Thanks!
Object[] options = {"Encrypt", "Decrypt"};
int n = JOptionPane.showOptionDialog(new JFrame(),
"What Do You Want to Do?",
"Crypto",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, //do not use a custom Icon
options, //the titles of buttons
options[0]); //default button title
Please have a look at How to Set the Look and Feel
Try with different themes
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
// Set Motif L&F on any platform
// UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
// Set cross-platform Java L&F (also called "Metal")
// UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
// Set System L&F
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
Object[] options = { "Encrypt", "Decrypt" };
JOptionPane.showOptionDialog(new JFrame(), "What Do You Want to Do?", "Crypto",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, // do not use a
// custom Icon
options, // the titles of buttons
options[0]); // default button title
}
});
With different themes:

Categories

Resources