Java Swing revalidate() vs repaint()

You need to call repaint() and revalidate(). The former tells Swing that an area of the window is dirty (which is necessary to erase the image of the old children removed by removeAll()); the latter tells the layout manager to recalculate the layout (which is necessary when adding components). This should cause children of the … Read more

Java GUI frameworks. What to choose? Swing, SWT, AWT, SwingX, JGoodies, JavaFX, Apache Pivot? [closed]

Decision tree: Frameworks like Qt and SWT need native DLLs. So you have to ask yourself: Are all necessary platforms supported? Can you package the native DLLs with your app? See here, how to do this for SWT. If you have a choice here, you should prefer Qt over SWT. Qt has been developed by … Read more

How to set JFrame to appear centered, regardless of monitor resolution?

Use setLocationRelativeTo(null) This method has a special effect when you pass it a null. According to the Javadoc: If the component is null, or the GraphicsConfiguration associated with this component is null, the window is placed in the center of the screen. This should be done after setting the size or calling pack(), but before … Read more

Value Change Listener to JTextField

Add a listener to the underlying Document, which is automatically created for you. // Listen for changes in the text textField.getDocument().addDocumentListener(new DocumentListener() { public void changedUpdate(DocumentEvent e) { warn(); } public void removeUpdate(DocumentEvent e) { warn(); } public void insertUpdate(DocumentEvent e) { warn(); } public void warn() { if (Integer.parseInt(textField.getText())<=0){ JOptionPane.showMessageDialog(null, “Error: Please enter number … Read more

What is the difference between Swing and AWT?

AWT is a Java interface to native system GUI code present in your OS. It will not work the same on every system, although it tries. Swing is a more-or-less pure-Java GUI. It uses AWT to create an operating system window and then paints pictures of buttons, labels, text, checkboxes, etc., into that window and … Read more

How to programmatically close a JFrame

If you want the GUI to behave as if you clicked the X close button then you need to dispatch a window closing event to the Window. The ExitAction from Closing An Application allows you to add this functionality to a menu item or any component that uses Actions easily. frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));

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.

Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?

Should I completely avoid the use of those methods? Yes for application code. The methods have been defined for a reason. So when should I use them? In which context? For what purposes? I don’t know, personally I think of it as an API design accident. Slightly forced by compound components having special ideas about … Read more

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