Found 7442 Articles for Java

How to add action listener to JButton in Java

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

10K+ Views

The following is an example to add action listener to Button:Examplepackage my; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingDemo {    private JFrame frame;    private JLabel headerLabel;    private JLabel statusLabel;    private JPanel controlPanel;    public SwingDemo(){       prepareGUI();    }    public static void main(String[] args){       SwingDemo swingControlDemo = new SwingDemo();       swingControlDemo.showButtonDemo();    }    private void prepareGUI(){       frame = new JFrame("Java Swing");       frame.setSize(500, 500);       frame.setLayout(new GridLayout(3, 1));       frame.addWindowListener(new WindowAdapter() {         ... Read More

Can we get the supported image types in Java

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

126 Views

Yes, we can get the supported image types with ImageIO class in Java. The following is an example to get supported image types in Java:Examplepackage my; import javax.imageio.ImageIO; public class SwingDemo {    public static void main(String[] args) throws Exception {       String[] imgTypes = ImageIO.getReaderFileSuffixes();       System.out.print("Supported Image Types = ");       for (String type : imgTypes) {          System.out.print("" + type);       }    } }OutputSupported Image Types = jpg tif tiff bmp gif png wbmp jpeg

Java Program to use Soft Bevel Border in Swing

Daniol Thomas
Updated on 30-Jul-2019 22:30:26

262 Views

Here, we are creating soft beven border on JComboBox:JComboBox comboBox = new JComboBox(list);Now, set bevel border:comboBox.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));The following is an example to use soft bevel border in Swing:Exampleimport java.awt.Font; import java.awt.GridBagLayout; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.BevelBorder; import javax.swing.border.SoftBevelBorder; public class SwingDemo {    static final String list[] = { "One", "Two", "Three", "Four", "Five", "Six" };    public static void main(String[] args) {       JFrame window = new JFrame("ComboBox Example");       JPanel panel = new JPanel();       panel.setLayout(new GridBagLayout());       JComboBox comboBox = new JComboBox(list);       comboBox.setBorder(new ... Read More

How to leave space with EmptyBorder in Java Swing

Daniol Thomas
Updated on 30-Jul-2019 22:30:26

552 Views

Llet us first create a JPanel and set titled border:JPanel panel = new JPanel(); panel.setBorder(BorderFactory.createTitledBorder("Demo Panel"));Now to create Empty Border:JPanel panel2 = new JPanel(new BorderLayout()); panel2.add(panel, BorderLayout.CENTER); panel2.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));The following is an example to leave space with EmptyBorder in Java Swing:Exampleimport java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) {       JPanel panel = new JPanel();       panel.setBorder(BorderFactory.createTitledBorder("Demo Panel"));       JPanel panel2 = new JPanel(new BorderLayout());       panel2.add(panel, BorderLayout.CENTER);       panel2.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));       ... Read More

Java Program to create rounded borders in Swing

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

6K+ Views

Let us first create a Frame:JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setUndecorated(true);Now, create rounded borders:double x = 50; double y = 50; frame.setShape(new RoundRectangle2D.Double(x, y, 100, 100, 50, 50));The following is an example to create rounded borders in Swing:Exampleimport java.awt.geom.RoundRectangle2D; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo extends JPanel {    public static void main(String[] args) {       JFrame frame = new JFrame();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       frame.setUndecorated(true);       double x = 50;       double y = 50;       frame.setShape(new RoundRectangle2D.Double(x, y, 100, 100, 50, 50));     ... Read More

Java Program to add Titled Border to Panel in Swing

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

270 Views

To set Titled Border to Panel, let us first create a Panel for our Java Swing Application:JPanel panel = new Jpanel(new BorderLayout());Now, set the titled border:panel.setBorder(new TitledBorder("Displaying Titled Border"));The following is an example to add Titled Border to Panel in Swing:Exampleimport java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.border.TitledBorder; public class SwingDemo {    public static void main(String[] args) {       JPanel panel = new JPanel(new BorderLayout());       panel.setBorder(new TitledBorder("Displaying Titled Border"));       panel.add(new JButton("Demo Button"), BorderLayout.SOUTH);       JOptionPane.showMessageDialog(null, panel);    } }OutputRead More

How to set fullscreen mode for Java Swing Application?

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

6K+ Views

To set fullscreen mode for your Java Swing application, use the setFullScreenWindow() method:GraphicsDevice device = graphics.getDefaultScreenDevice(); JFrame frame = new JFrame("Fullscreen"); device.setFullScreenWindow(frame);The following is an example to set fullscreen mode for Java Swing Application:Exampleimport java.awt.Color; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) {       GraphicsEnvironment graphics =       GraphicsEnvironment.getLocalGraphicsEnvironment();       GraphicsDevice device = graphics.getDefaultScreenDevice();       JFrame frame = new JFrame("Fullscreen");       JPanel panel = new JPanel();       JLabel label = new JLabel("", JLabel.CENTER);     ... Read More

Make a custom cursor appear when the user moves the mouse over some text in a Java Swing JDialog

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

725 Views

At first set the label on which you want the custom cursor to be visible on hover:JLabel label = new JLabel("Demo text! Hand cursor is visible on hover...");Now, set cursor to be visible as Hand Cursor instead of the default Cursor:label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));The following is an example to make a custom cursor appear when the user moves the mouse over some text:Exampleimport java.awt.Cursor; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; public class SwingDemo extends JFrame {    private void ShowDialog() {       JLabel label = new JLabel("Demo text! Hand cursor is visible on hover...");       label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));   ... Read More

How do you get the font metrics in Java Swing?

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

556 Views

To get the font metrics, use the FontMetrics class:Graphics2D graphics = (Graphics2D) gp.create(); String str = getWidth() + "(Width) x (Height)" + getHeight(); FontMetrics m = graphics.getFontMetrics();Now to display it:int xValue = (getWidth() - m.stringWidth(str)) / 2; int yValue = ((getHeight() - m.getHeight()) / 2) + m.getAscent(); graphics.drawString(str, xValue, yValue);The following is an example to get the font metrics in Java Swing:Exampleimport java.awt.BorderLayout; import java.awt.Dimension; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Color; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame("Font Metrics");       ... Read More

Java Program to create Arrow Button positioning North

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

265 Views

To create Arrow Button at position north, use BasicArrowButton:BasicArrowButton arrow = new BasicArrowButton(BasicArrowButton.NORTH);Above, we have set the arrow to NORTH. Now add it to Panel:panel.add(arrow, BorderLayout.NORTH);The following is an example to create Arrow Button positioning North:Exampleimport java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.plaf.basic.BasicArrowButton; public class SwingDemo extends JPanel {    public SwingDemo() {       setLayout(new BorderLayout());       JPanel panel = new JPanel(new BorderLayout());       add(panel, BorderLayout.EAST);       BasicArrowButton arrow = new BasicArrowButton(BasicArrowButton.NORTH);       panel.add(arrow, BorderLayout.NORTH);    }    public static void main(String[] args) {       JFrame frame = ... Read More

Advertisements