KeyPressed event in java

Depending on where you want to trap the “enter” key, you could use an ActionListener (on such components such as text components or buttons) or attach a key binding to you component This will rely on the component being focused.

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: Later, you’ll have to define selectionButtonPressed(). … Read more

Java: Difference between the setPreferredSize() and setSize() methods in components

Usage depends on whether the component’s parent has a layout manager or not. setSize() — use when a parent layout manager does not exist; setPreferredSize() (also its related setMinimumSize and setMaximumSize) — use when a parent layout manager exists. The setSize() method probably won’t do anything if the component’s parent is using a layout manager; the places this will typically have an … Read more

Resizing image in Java

If you have an java.awt.Image, rezising it doesn’t require any additional libraries. Just do: Ovbiously, replace newWidth and newHeight with the dimensions of the specified image.Notice the last parameter: it tells to the runtime the algorithm you want to use for resizing. There are algorithms that produce a very precise result, however these take a large time to complete.You can use any of the following … Read more