JComponents disappearing after calling mouseClicked()

Consider using a JList for the right panel to take advantage of the flexible layout options and selection handling, as shown here and below. import java.awt.Component; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridLayout; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import javax.swing.BorderFactory; import javax.swing.DefaultListCellRenderer; import javax.swing.DefaultListModel; import javax.swing.Icon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import … Read more

Change Font at runtime

For instance: Create a new Font from the FontFamily list, and apply it to your component with c.setFont (font); A second approach is, to search for TTF-Files (for example), and create new Fonts with the static method Font.createFont (new File (“…”)); This simple app will create a List of Fonts by family, and apply it … Read more

How to get all elements inside a JFrame?

You can write a recursive method and recurse on every container: This site provides some sample code: public static List<Component> getAllComponents(final Container c) { Component[] comps = c.getComponents(); List<Component> compList = new ArrayList<Component>(); for (Component comp : comps) { compList.add(comp); if (comp instanceof Container) compList.addAll(getAllComponents((Container) comp)); } return compList; } If you only want the … Read more

Java swing JComponent “size”

As an alternative, consider the The Button API, which includes the method setRolloverIcon() “to make the button display the specified icon when the cursor passes over it.” Addendum: For example, import java.net.MalformedURLException; import java.net.URL; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class ButtonIconTest { public static void main(String[] args) … Read more

JPanel which one of Listeners is proper for visibility is changed

If you want to listen EXACTLY the visibility changes – use ComponentListener or ComponentAdapter: JPanel panel = new JPanel (); panel.addComponentListener ( new ComponentAdapter () { public void componentShown ( ComponentEvent e ) { System.out.println ( “Component shown” ); } public void componentHidden ( ComponentEvent e ) { System.out.println ( “Component hidden” ); } } … Read more

Swing HTML drawString

If a fan of Java2D; but to get the most leverage from HTML in Swing components and layouts, I’d encourage you to use the component approach suggested by @camickr. If necessary, you can use the flyweight renderer approach seen in JTable, et al, in which a single component is used repeatedly for drawing. The example … Read more