Found 9150 Articles for Object Oriented Programming

How to create JLabel to hold multiline of text using HTML in Java?

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

1K+ Views

To hold multiline of text, set HTML under JLabel −JLabel = new JLabel("" + "Line1Line2",JLabel.LEFT);The above will create multiline text in the JLabel −Line1 Line2The following is an example to create JLabel to hold multiline of text −Exampleimport java.awt.Font; import javax.swing.*; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Label Example");       JLabel label;       label = new JLabel("" + "Line1       Line2",JLabel.LEFT);       label.setBounds(100, 100, 100, 30);       label.setFont(new Font("Verdana", Font.PLAIN, 13));       frame.add(label);       frame.setSize(500,300);       frame.setLayout(null);       frame.setVisible(true);    } }Output

How to change text font for JLabel with HTML in Java?

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

995 Views

To change text font, you can use the setFont() method of JLabel −label.setFont(new Font("Verdana", Font.PLAIN, 12));The following is an example to change text font for JLabel with HTML −Exampleimport java.awt.Font; import javax.swing.*; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Label Example");       JLabel label;       label = new JLabel("" + "ABC");       label.setBounds(50, 50, 100, 30);       label.setFont(new Font("Verdana", Font.PLAIN, 12));       frame.add(label);       frame.setSize(500,300);       frame.setLayout(null);       frame.setVisible(true);    } }Output

Java program to change JLabel text after creation

Samual Sam
Updated on 16-Sep-2024 23:24:47

3K+ Views

In this article, we will learn to change JLabel text after creation in Java. We'll cover two scenarios: changing the label text immediately after creation and updating it in response to a button click. Different approaches to change JLabel text after creation Below are the different approaches to change JLabel text after the creation − Using setText() Method Using a button click Update JLabel text using setText() method Following are the steps to update JLabel text immediately after creation − First we will import JFrame, JLabel, and ... Read More

How to change JLabel font in Java

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

14K+ Views

To change JLabel font, use the setFont() method −JLabel lable = label.setFont(new Font("Verdana", Font.PLAIN, 18));Examplepackage my; import java.awt.Font; import javax.swing.*; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Label Example");       JLabel label;       label = new JLabel("First Label");       label.setBounds(50, 50, 100, 30);       label.setFont(new Font("Verdana", Font.PLAIN, 18));       frame.add(label);       frame.setSize(300,300);       frame.setLayout(null);       frame.setVisible(true);    } }Output

Java Program to draw a line on a JFrame in Java

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

2K+ Views

The following is an example to draw a line on a JFrame −Examplepackage my; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Line2D; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo extends JFrame {    public SwingDemo() {       JPanel panel = new JPanel();       getContentPane().add(panel);       setSize(550, 300);    }    public void paint(Graphics gp) { super.paint(gp); Graphics2D graphics = (Graphics2D) gp;       Line2D line = new Line2D.Float(200, 150, 150, 220);       graphics.draw(line);    }    public static void main(String[] args) {       SwingDemo demo = new SwingDemo();       demo.setVisible(true);    } }Output

Draw a border around an undecorated JFrame in Java

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

784 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

578 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

1K+ 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

3K+ 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

Advertisements