/JCopyButton

JCopyButton is a Java Swing component designed to copy the contents of a JTextComponent to the system clipboard when clicked.

Primary LanguageJavaApache License 2.0Apache-2.0

JCopyButton is a Java Swing component designed to copy the contents of a JTextComponent to the system clipboard when clicked. It can be used with JTextComponent and all of its subclasses:

Table of Contents

Installation

JCopyButton is hosted on the JitPack package repository which supports Gradle, Maven, and sbt.

Gradle Gradle

Add JitPack to your build.gradle at the end of repositories.

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

Add JCopyButton as a dependency.

dependencies {
	implementation 'com.github.Valkryst:JCopyButton:2024.1.7'
}

Maven Maven

Add JitPack as a repository.

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

Add JCopyButton as a dependency.

<dependency>
    <groupId>com.github.Valkryst</groupId>
    <artifactId>JCopyButton</artifactId>
    <version>2024.1.7</version>
</dependency>

Scala SBT Scala SBT

Add JitPack as a resolver.

resolvers += "jitpack" at "https://jitpack.io"

Add JCopyButton as a dependency.

libraryDependencies += "com.github.Valkryst" % "JCopyButton" % "2024.1.7"

Example

This creates a new JCopyButton and displays it in a JFrame, along with a JTextArea that contains some text for the button to copy.

public class Driver {
  public static void main(final String[] args) {
    SwingUtilities.invokeLater(() -> {
      final JTextArea textArea = new JTextArea();
      textArea.setLineWrap(true);
      textArea.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
      textArea.setWrapStyleWord(true);

      final JFrame frame = new JFrame("JCopyButton Example");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setPreferredSize(new Dimension(500, 500));

      final Container contentPane = frame.getContentPane();
      contentPane.setLayout(new BorderLayout());
      contentPane.add(new JScrollPane(textArea), BorderLayout.CENTER);
      contentPane.add(new JCopyButton(textArea), BorderLayout.SOUTH);

      frame.setVisible(true);
      frame.pack();
      frame.setLocationRelativeTo(null);
    });
  }
}