How to center the text in a JLabel?

String text = “In early March, the city of Topeka, Kansas,” + “<br>” + “temporarily changed its name to Google…” + “<br>” + “<br>” + “…in an attempt to capture a spot” + “<br>” + “in Google’s new broadband/fiber-optics project.” + “<br>” + “<br>” +”<br>” + “source: http://en.wikipedia.org/wiki/Google_server#Oil_Tanker_Data_Center”; JLabel label = new JLabel(“<html><div style=”text-align: center;”>” … Read more

How to add an image to a JPanel?

Here’s how I do it (with a little more info on how to load an image): import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.JPanel; public class ImagePanel extends JPanel{ private BufferedImage image; public ImagePanel() { try { image = ImageIO.read(new File(“image name and path”)); } catch (IOException … Read more

Implementing an actionlistener to a JTextField

From what you’ve provided, it looks like you’ve imported or implemented a class other than java.awt.event.ActionListener named, ActionListener (class name conflict). Try qualifying the parameter as java.awt.event.ActionListener: //Creates textField JTextField input=new JTextField(20); input.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent event) { direction=input.getText(); } });

How to Set JPanel’s Width and Height?

please, something went xxx*x, and that’s not true at all, check that JButton Size – java.awt.Dimension[width=400,height=40] JPanel Size – java.awt.Dimension[width=640,height=480] JFrame Size – java.awt.Dimension[width=646,height=505] code (basic stuff from Trail: Creating a GUI With JFC/Swing , and yet I still satisfied that that would be outdated ) EDIT: forget setDefaultCloseOperation() import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import … Read more

addMouseListener for a JPanel

JPanel doesn’t have ActionListener capabilities. Instead, you need to use a MouseListener import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.GridLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JFrame; import javax.swing.JPanel; public class CoordBoutons extends JFrame { CoordBoutons() { super(“GridLayout”); setDefaultCloseOperation(EXIT_ON_CLOSE); Container contenant = getContentPane(); contenant.setLayout(new GridLayout(8, 8)); for (int i = 0; i < 8; i++) { for (int … Read more

How do you add an ActionListener onto a JButton in Java

Two ways: 1. Implement ActionListener in your class, then use jBtnSelection.addActionListener(this); Later, you’ll have to define a menthod, public void actionPerformed(ActionEvent e). However, doing this for multiple buttons can be confusing, because the actionPerformed method will have to check the source of each event (e.getSource()) to see which button it came from. 2. Use anonymous inner classes: jBtnSelection.addActionListener(new ActionListener() { public void … Read more

How to make PopUp window in java

The same answer : JOptionpane with an example 🙂 package experiments; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; public class CreateDialogFromOptionPane { public static void main(final String[] args) { final JFrame parent = new JFrame(); JButton button = new JButton(); button.setText(“Click me to show dialog!”); parent.add(button); parent.pack(); parent.setVisible(true); button.addActionListener(new java.awt.event.ActionListener() { @Override public void actionPerformed(java.awt.event.ActionEvent evt) { String … Read more