Showing posts with label Swing. Show all posts
Showing posts with label Swing. Show all posts

Sunday, 3 December 2017

How to customize JOptionPane in Java/Swing

JOptionPane is pop up window to show any information, confirm any action or user input in Java. We can use JOptionPane class simply to show dialogue boxes for users. But it has default size, text and icons and some time we feel that, can we customise the properties of this dialogue box, like its window size, its text size, colour and type and many more?

So, obviously answer is yes, we can customise the properties of JOptionPane. Let see how?

To create dialogue box using JOptionPane, we simply use -

JOptionPane.showConfirmDialog(null,"Text Message", "Window Title",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
 
Output looks like -

Now If you want to customise the message text, we require to use label(JLabel), because we can customize the JLabel text and can apply css and HTML on it. See this post.

 JLabel label = new JLabel("<html><p><h1>Text Message</h1></p><html>");

JOptionPane.showConfirmDialog(null,"Text Message", "Window Title",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

Output looks like -

above we have changed the text colour and size.

 You can also customise JLabel text by using code like -

label.setFont(new Font("Arial", Font.BOLD, 18));

To change default image you can use code like -

ImageIcon icon = new ImageIcon(Pane.class.getResource("ss.jpg"));

JOptionPane.showConfirmDialog(null,"Text Message", "Window Title",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, icon);

Output will look like -

To change the window size, you can use code like -

UIManager.put("OptionPane.minimumSize",new Dimension(500,500));

JLabel label = new JLabel("<html><p><h1 style=color:green>Text Message</h1></p><html>");

ImageIcon icon = new ImageIcon(Pane.class.getResource("ss.jpg"));

JOptionPane.showConfirmDialog(null,label, "Window Title",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,icon);

Output looks like -

You can also do many more experience with JOptionPane customisation.

Thursday, 2 February 2017

How to create a Splash Screen in Java Swing.

Splash screen is window, which is displayed during an application startup. Many programs display such a screen, possibly containing copyright information, resource loading status, etc. Splash screen can not be moved or closed normally. It can only be closed or moved programmatically. Most seen splash screen by java user is Netbeans splash screen.

It is very easy to create a splash screen in java using JWindow.

Here is the complete example.

import javax.swing.*;
import java.awt.*;
public class Splash {
 public static void main(String[] args) {

 // Throw a nice little title page up on the screen first

 showSplash(20000);
 System.exit(0); // replace with application code!
 }
 // A simple little method to show a title screen in the
 // center of the screen for a given amount of time.

 public static void showSplash(int duration) {
JWindow splash = new JWindow();
 JPanel content = (JPanel)splash.getContentPane();

 // set the window's bounds, centering the window

 int width = 240;
 int height = 120;
 Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
 int x = (screen.width-width)/2;
 int y = (screen.height-height)/2;
 splash.setBounds(x,y,width,height);

 // build the splash screen

 JLabel label = new JLabel(new ImageIcon("picture069.jpg"));
 JLabel copyrt = new JLabel
 ("Copyright 1998, PussinToast Inc.", JLabel.CENTER);
 copyrt.setFont(new Font("Sans-Serif", Font.BOLD, 12));
 content.add(label, BorderLayout.CENTER);
 content.add(copyrt, BorderLayout.SOUTH);
 content.setBorder(BorderFactory.createLineBorder(Color.red, 10));

 // display it

 splash.setVisible(true);

 // Wait a little while, maybe while loading resources

 try { Thread.sleep(duration); } catch (Exception e) {}
 splash.setVisible(false);
 }
}

Saturday, 3 September 2016

Make JFrame unmovable, unclosable, unminimizable, unresizable & unmaximizable in Java Swing

Suppose, you have to develop a application which requires desktop locking- means user can not use the system if you don't allow to use it. For such kind of application we require that (I assume that it is a swing application) the JFrame should not be closed, maximized, minimized or movable.

To do so, we require remove title bar along with close, maximize and minimize buttons (windows buttons), so that JFrame could not be closed, moved, maximized or minimized

setUndecorated(true);    //remove title bar

setResizable(false);      //make frame static

setType(javax.swing.JFrame.Type.UTILITY);     //Jframe will not show in task bar

setAlwaysOnTop(true);       //set always on top if we don't close it using any action


Change image source dynamically on hyperlink

 Changing image source dynamically using JQuery. Here in this example I have created there hyperlink and stored all images in the same folde...