Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you meant to say JPanel. You can add a custom mouseListener and handle mouse clicks, drags and mouse releases and then resize the panel programmaticaly.</p> <p>This will demonstrate this. Note that the jframe does NOT resize automatically with the JPanel. To make the effect more visible i painted the panel red and added a beveled border :</p> <pre><code>import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Point; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.BevelBorder; @SuppressWarnings("serial") public class ResizablePanel extends JPanel { private boolean drag = false; private Point dragLocation = new Point(); public ResizablePanel() { setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED)); setPreferredSize(new Dimension(500, 500)); final JFrame f = new JFrame("Test"); addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { drag = true; dragLocation = e.getPoint(); } @Override public void mouseReleased(MouseEvent e) { drag = false; } }); addMouseMotionListener(new MouseMotionAdapter() { @Override public void mouseDragged(MouseEvent e) { if (drag) { if (dragLocation.getX()&gt; getWidth()-10 &amp;&amp; dragLocation.getY()&gt;getHeight()-10) { System.err.println("in"); setSize((int)(getWidth()+(e.getPoint().getX()-dragLocation.getX())), (int)(getHeight()+(e.getPoint().getY()-dragLocation.getY()))); dragLocation = e.getPoint(); } } } }); f.getContentPane().setLayout(new BorderLayout()); f.getContentPane().add(this,BorderLayout.CENTER); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.setVisible(true); } public static void main(String[] args) { new ResizablePanel(); } public void paintComponent(Graphics g) { g.setColor(Color.red); g.fillRect(0, 0, getWidth(), getHeight()); } } </code></pre>
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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