How to draw in JPanel? (Swing/graphics Java)

Note the extra comments. import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; class JavaPaintUI extends JFrame { private int tool = 1; int currentX, currentY, oldX, oldY; public JavaPaintUI() { initComponents(); } private void initComponents() { // we want a custom Panel2, not a generic JPanel! jPanel2 = new Panel2(); jPanel2.setBackground(new java.awt.Color(255, 255, 255)); jPanel2.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED)); … Read more

Align text in JLabel to the right

This can be done in two ways. JLabel Horizontal Alignment You can use the JLabel constructor: JLabel(String text, int horizontalAlignment) To align to the right: JLabel label = new JLabel(“Telephone”, SwingConstants.RIGHT); JLabel also has setHorizontalAlignment: label.setHorizontalAlignment(SwingConstants.RIGHT); This assumes the component takes up the whole width in the container. Using Layout A different approach is to … Read more

add controls vertically instead of horizontally using flow layout

I hope what you are trying to achieve is like this. For this please use Box layout. package com.kcing.kailas.sample.client; import javax.swing.BoxLayout; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.WindowConstants; public class Testing extends JFrame { private JPanel jContentPane = null; public Testing() { super(); initialize(); } private void initialize() { this.setSize(300, … 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; … Read more

How to add an image to a JPanel?

If you are using JPanels, then are probably working with Swing. Try this: BufferedImage myPicture = ImageIO.read(new File(“path-to-file”)); JLabel picLabel = new JLabel(new ImageIcon(myPicture)); add(picLabel); The image is now a swing component. It becomes subject to layout conditions like any other component.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)