Note that there are some explanatory texts on larger screens.

plurals
  1. POProgramming a GUI with resizable components using Java.swing
    primarykey
    data
    text
    <p>I'm supposed to program a GUI. Every component in this GUI has to have the possibility to be resized dynamically. </p> <p>So far I worked with GlassPane and ContentPane, added a JPanel and on it a button. When clicking the GlassPane gets the event, analyses the underlying component, creates a new handle for this special component and handles it over to the said component. I added a button that should change its size automatically. Works like I wanted it to work. BUT: When I change the size of the frame and click on the button now, nothing happens. The GlassPane is able to identify the button, but something seems to be wrong...</p> <p>Here's the code for the interception of GlassPane and giving the event to the component:</p> <pre><code>private void resendEvent(MouseEvent e) { //Point p = SwingUtilities.convertPoint(content.getGlassPane(), e.getPoint(), content.getContentPane()); Point p = e.getPoint(); Component component = SwingUtilities.getDeepestComponentAt(content.getContentPane(), p.x, p.y); System.out.println(component.toString()); Point p2 = component.getLocation(); MouseEvent event = new MouseEvent(component, e.getID(), e.getWhen(), e.getModifiers(), p2.x, p2.y, e.getClickCount(), e.isPopupTrigger()); //following lines have the same effects Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(event); //component.dispatchEvent(event); } </code></pre> <p>Thanks for your help/suggestions</p> <hr> <p>Okay, here's some more Code:</p> <pre><code>import java.awt.*; import java.awt.event.*; import javax.swing.*; import resize.ResizablePanel; public class ExampleProblem extends JFrame { public ExampleProblem () { JPanel glassPane = new JPanel(); glassPane.setOpaque(false); glassPane.addMouseListener(new MouseAdapter(){ @Override public void mousePressed(MouseEvent e) { resendEvent(e); } @Override public void mouseReleased(MouseEvent e) { resendEvent(e); } }); this.setGlassPane(glassPane); glassPane.setVisible(true); JButton b = new JButton("Test"); b.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent arg0) { System.out.println("Button clicked"); }}); JPanel p = new JPanel(); p.add(b); setContentPane(p); pack(); setVisible(true); } private void resendEvent(MouseEvent e) {//Point p = SwingUtilities.convertPoint(content.getGlassPane(), e.getPoint(), content.getContentPane()); Point p = e.getPoint(); Component component = SwingUtilities.getDeepestComponentAt(this.getContentPane(), p.x, p.y); System.out.println(component.toString()); Point p2 = component.getLocation(); MouseEvent event = new MouseEvent(component, e.getID(), e.getWhen(), e.getModifiers(), p2.x, p2.y, e.getClickCount(), e.isPopupTrigger()); Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(event); } public static void main(String[] args) { new ExampleProblem(); } } </code></pre> <p>Hope, the problem is a bit clearer now. Is it possible, that I shouldn't have used setContentPane()? But we had to overwrite the JContentPane for resizing the components on it...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload