matthieu
matthieu

Reputation: 109

allowing users to "draw" and resize JTextArea

I want to allow users to be able to "draw" with their mouse (click and drag) to create and size a JTextArea. As well, I would like to have the text areas as resizeable.

Something like this:

enter image description here

comes to mind, but as a JTextArea instead of just a square.

Is there something in Java that would allow me to easily do this? I first thought to allow the user to draw a rectangle and just grab the co-ordinates and size to create the JTextArea. I am unsure on how to do the resizing though.

Edit: "Component Resizer / Reszing" was the term I was looking for and I'm adding it here in case someone else is looking for something similar!

Upvotes: 1

Views: 2971

Answers (4)

camickr
camickr

Reputation: 324118

You should be able to use the Component Resizer.

Upvotes: 2

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

The resizing the JTextArea can be done easily enough via calling setBounds(...) on it -- or better on the JScrollPane that holds it, but you will need to use a null or similar (JLayeredPane) layout on the container that holds the JTextArea and will likely need to repaint the container after resizing the JScrollPane. You will also have to revalidate the scrollpane's viewport so it will re-layout the textarea that it holds.

e.g.,

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class ResizeableTextArea extends JPanel {
   private static final int PREF_WIDTH = 700;
   private static final int PREF_HEIGHT = 500;
   private static final int ROWS = 60;
   private static final int COLS = 80;
   private static final Color RECT_COLOR = new Color(180, 180, 255);

   private JTextArea textArea = new JTextArea(ROWS, COLS);
   private JScrollPane scrollPane = new JScrollPane(textArea);
   private int x, y, width, height;
   private boolean drawRect = false;

   public ResizeableTextArea() {
      setLayout(null);
      add(scrollPane);

      MyMouseAdapter myMouseAdapter = new MyMouseAdapter(); 
      addMouseListener(myMouseAdapter);
      addMouseMotionListener(myMouseAdapter);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      if (drawRect) {
         g.setColor(RECT_COLOR);
         g.drawRect(x, y, width, height);         
      }
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_WIDTH, PREF_HEIGHT);
   }

   private class MyMouseAdapter extends MouseAdapter {
      private int innerX, innerY;

      @Override
      public void mousePressed(MouseEvent e) {
         x = e.getX();
         y = e.getY();
         innerX = x;
         innerY = y;
         width = 0;
         height = 0;
         drawRect = true;
      }

      @Override
      public void mouseDragged(MouseEvent e) {
         calcBounds(e);

         drawRect = true;
         ResizeableTextArea.this.repaint();
      }

      @Override
      public void mouseReleased(MouseEvent e) {
         calcBounds(e);

         drawRect = false;
         scrollPane.setBounds(x, y, width, height);
         scrollPane.getViewport().revalidate();
         ResizeableTextArea.this.repaint();

      }

      private void calcBounds(MouseEvent e) {
         width = Math.abs(innerX - e.getX());
         height = Math.abs(innerY - e.getY());
         x = Math.min(innerX, e.getX());
         y = Math.min(innerY, e.getY());
      }

   }

   private static void createAndShowUI() {
      JFrame frame = new JFrame("ResizeableTextArea");
      frame.getContentPane().add(new ResizeableTextArea());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

Upvotes: 2

revo
revo

Reputation: 538

You can found a solution here I have already try it and the result is very well. In the tutorial there is a reference to another implementation here.

Upvotes: 2

mKorbel
mKorbel

Reputation: 109813

that not really good idea, sure is possible to put Image or ImageIcon as BackGround, better would be use for that JLabel with Icon, then you can painting selection easily

Upvotes: 1

Related Questions