- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 4336 Articles for Java 8
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
846 Views
The following is an example to change JButton font dynamically:Exampleimport java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; public class SwingDemo extends JFrame { JButton button = new JButton("Change"); int fontSize = 10; public SwingDemo() { setSize(500, 400); setDefaultCloseOperation(EXIT_ON_CLOSE); add(button); // changing font size dynamically on button click button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { button.setFont(new Font("Dialog", Font.PLAIN, ++fontSize)); button.revalidate(); } }); setVisible(true); } public static void main(String[] args) { new SwingDemo(); } }OutputClick “Change” button above to change the font:
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
936 Views
Let us first set a JButton:JButton btn = new JButton("DemoButton");Now, attach action listener:btn.addActionListener(new ClickListener());If you have an ActionListener attached to your button it'll fire when you call the method doClick():btn.doClick();The following is an example to call JButton doClick() method to simulate a click action:Exampleimport java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JOptionPane; public class SwingDemo { public static void main(final String args[]) { JButton btn = new JButton("DemoButton"); btn.addActionListener(new ClickListener()); JOptionPane.showMessageDialog(null, btn); btn.doClick(); } } class ClickListener implements ActionListener { public void actionPerformed(ActionEvent e) { ... Read More
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
3K+ Views
With set action command, here we are displaying a message in the console on the click of a button.Set the button first:JButton btn = new JButton("Demo Button");Now, set Action Listener to fire when the button is clicked:ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent event) { String str = event.getActionCommand(); System.out.println("Clicked = " + str); } };The following is an example to set action command to JButton:Exampleimport java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JOptionPane; public class SwingDemo { public static void main(final String args[]) { JButton btn ... Read More
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
827 Views
For Button border, use createLineBorder() method in Java, which allows you to set the color of the Border as well:JButton button = new JButton("Demo Button!"); Border border = BorderFactory.createLineBorder(Color.BLUE);The following is an example to change button border in Java:Exampleimport java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.border.Border; public class SwingDemo { public static void main(String args[]) { JFrame frame = new JFrame("Button Border"); Container container = frame.getContentPane(); JButton button = new JButton("Demo Button!"); Border border = BorderFactory.createLineBorder(Color.BLUE); button.setBorder(border); ... Read More
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
7K+ 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
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
70 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
![Daniol Thomas](https://www.tutorialspoint.com/assets/profiles/13551/profile/60_130035-1512726078.jpg)
178 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
![Daniol Thomas](https://www.tutorialspoint.com/assets/profiles/13551/profile/60_130035-1512726078.jpg)
407 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
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
4K+ 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
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
173 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