Found 4336 Articles for Java 8

Draw a border around an undecorated JFrame in Java

Samual Sam
Updated on 30-Jul-2019 22:30:26

526 Views

At first, set an undecorated frame −setUndecorated(true);Now draw a border −getRootPane().setBorder (.createMatteBorder(3, 3, 3, 3, Color.ORANGE));The following is an example to draw a border around an undecorated JFrame −Exampleimport java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; public class SwingDemo extends JFrame {    JLabel label = new JLabel("Welcome!", JLabel.CENTER);    public SwingDemo() {       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       setSize(new Dimension(500, 300));       add(label, BorderLayout.CENTER);       setUndecorated(true);       getRootPane().setBorder(          BorderFactory.createMatteBorder(3, 3, 3, 3, Color.ORANGE));       setVisible(true);    }    public static void main(String[] args) {       new SwingDemo();    } }Output

How to create modeless and model JDialog in Java?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

429 Views

MODELESS TypeThe following is an example to set JDialog with Modality type MODELESS −Exampleimport java.awt.Cursor; import java.awt.Dialog.ModalityType; import java.awt.Dimension; import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       frame.setSize(new Dimension(600, 400));       JDialog dialog = new JDialog(frame, "New", ModalityType.MODELESS);       dialog.setSize(300, 300);       frame.add(new JButton(new AbstractAction("Click to generate") {          @Override          public void actionPerformed(ActionEvent e) {             frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));   ... Read More

How to set default button for JFrame in Java?

Samual Sam
Updated on 30-Jul-2019 22:30:26

882 Views

To set default button for JFrame, use the setDefaultButton() method −JFrame frame = new JFrame(); frame.getRootPane().setDefaultButton(button);The following is an example to set default button for JFrame −Exampleimport java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.JButton; import javax.swing.JFrame; public class SwingDemo {    public static void main(String args[]) {       JButton button = new JButton("Demo Button!");       JFrame frame = new JFrame();       frame.setSize(500, 300);       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       frame.getRootPane().setDefaultButton(button);       button.setMnemonic(KeyEvent.VK_A);       button.addActionListener(new ActionListener() {          public void actionPerformed(ActionEvent ae) {         ... Read More

How to set minimum size limit for a JFrame in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

2K+ Views

Use the setMinimumSize() method to set the minimum size limit for a JFrame −JFrame frame = new JFrame(); frame.setMinimumSize(new Dimension(500, 300));The following is an example to set minimum size limit for a JFrame −Exampleimport java.awt.Color; import java.awt.Dimension; import javax.swing.JButton; import javax.swing.JFrame; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       JButton button = new JButton("Close!");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       frame.setContentPane(button);       button.addActionListener(e -> {          frame.dispose();       });       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       frame.setMinimumSize(new Dimension(500, ... Read More

Get JFrame window size information in Java

Samual Sam
Updated on 30-Jul-2019 22:30:26

2K+ Views

To get JFrame window size information, you can use the following −environment.getMaximumWindowBounds();For Screen Size −config.getBounds()For Frame Size −frame.getSize());The following is an example to get JFrame window size information −Exampleimport java.awt.GraphicsConfiguration; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.Rectangle; import javax.swing.JFrame; public class SwingDemo {    public static void main(String[] args) {       GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();       Rectangle bounds = environment.getMaximumWindowBounds();       System.out.println("Screen Bounds = " + bounds);       GraphicsDevice device = environment.getDefaultScreenDevice();       GraphicsConfiguration config = device.getDefaultConfiguration();       System.out.println("Screen Size = " + config.getBounds());       JFrame frame ... Read More

How can we clear all selections in Java Swing JList?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

495 Views

To clear all selections, use the List clearSelection() method in Java −JList list = new JList(sports); list.clearSelection();Above, the elements in Sports array is a String array −String sports[]= { "Cricket", "Football", "Hockey", "Rugby"};The following is an example to clear all selection in JList −Examplepackage my; import java.awt.event.*; import java.awt.*; import javax.swing.*; class SwingDemo extends JFrame {    static JFrame frame;    static JList list;    public static void main(String[] args) {       frame = new JFrame("JList Demo");       SwingDemo s = new SwingDemo();       JPanel panel = new JPanel();       String sports[]= ... Read More

How to select the second index in Java JList?

Samual Sam
Updated on 30-Jul-2019 22:30:26

285 Views

To select the second index, use the setSelectedIndex() method −JList new JList(sports); list.setSelectedIndex(2);The following is an example to select the second index in Java JList −Examplepackage my; import java.awt.event.*; import java.awt.*; import javax.swing.*; class SwingDemo extends JFrame {    static JFrame frame;    static JList list;    public static void main(String[] args) {       frame = new JFrame("JList Demo");       SwingDemo s = new SwingDemo();       JPanel panel = new JPanel();       String sports[]= { "Cricket","Football","Hockey","Rugby"};       list = new JList(sports);       list.setSelectedIndex(2);       panel.add(list);       frame.add(panel);       frame.setSize(400,400);       frame.setVisible(true);    } }Output

Java Program to create JRadioButton from text

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

92 Views

The following is an example to create JRadioButton from text −Examplepackage my; import java.awt.FlowLayout; import java.awt.event.KeyEvent; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JRadioButton; public class SwingDemo {    public static void main(String[] args) {       JRadioButton radio1 = new JRadioButton("Cricket");       JRadioButton radio2 = new JRadioButton("Football");       ButtonGroup group = new ButtonGroup();       group.add(radio1);       group.add(radio2);       radio1.setSelected(true);       JFrame frame = new JFrame();       frame.setLayout(new FlowLayout());       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       frame.add(new JLabel("Fav Sports:"));       frame.add(radio1);     ... Read More

How to add background Image to JFrame in Java

Samual Sam
Updated on 30-Jul-2019 22:30:26

13K+ Views

To add background image to JFrame, use the getImage() method of the Image class −Image img = Toolkit.getDefaultToolkit().getImage("E:\rahul.jpg");Now, draw the image −public void paintComponent(Graphics g) {    super.paintComponent(g);    g.drawImage(img, 0, 0, null); }The following is an example to add Background Image to JFrame −Exampleimport java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; import java.io.IOException; import javax.swing.JPanel; public class SwingDemo extends javax.swing.JFrame {    Image img = Toolkit.getDefaultToolkit().getImage("E:\rahul.jpg");    public SwingDemo() throws IOException {       this.setContentPane(new JPanel() {          @Override          public void paintComponent(Graphics g) {             super.paintComponent(g);     ... Read More

How to create JFrame with no border and title bar in Java?

Samual Sam
Updated on 30-Jul-2019 22:30:26

534 Views

To create a JFrame with no border and title bar, use setUndecorated() −JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setPreferredSize(new Dimension(400, 300)); frame.setUndecorated(true);The following is an example to create JFrame with no border and title bar −Exampleimport java.awt.Dimension; import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       frame.setPreferredSize(new Dimension(400, 300)); frame.setUndecorated(true);       JPanel panel = new JPanel();       panel.add(new JLabel("Demo!"));       panel.add(new JButton(new AbstractAction("Close") {   ... Read More

Advertisements