Swing on top of OpenGL
in
Integration and Hardware
•
2 years ago
Yes, I know Swing & Processing don't play well together, but I had a need for hyperlinks in a text box, so here is some code I've put together that tries to address the HeavyWeight / LightWeight problem. It also doesn't use a separate frame, so you don't have to worry about trying to capture keystrokes and forcing a window to be "AlwaysOnTop" every few frames.
Love to see improvements if someone wants to hack at it. In this instance, I used a JEditorPane on a JScrollPane and added HyperLink support.
UPDATE: Improved version in my next post below
Love to see improvements if someone wants to hack at it. In this instance, I used a JEditorPane on a JScrollPane and added HyperLink support.
UPDATE: Improved version in my next post below
- import javax.swing.*;
- import processing.opengl.*;
- import java.awt.*;
- import javax.swing.event.HyperlinkListener;
- import javax.swing.event.HyperlinkEvent;
- import javax.swing.text.html.HTMLEditorKit;
- import javax.swing.text.html.HTMLDocument;
- import javax.swing.text.Document;
- import javax.swing.text.DefaultStyledDocument;
- import java.awt.Insets;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.awt.event.WindowListener;
- int position = 0;
- SwingGUI gui;
- void setup() {
- size(550, 400, OPENGL);
- gui = new SwingGUI(this, 100, 100, 300, 200);
- gui.setVisible(true);
- String myHTMLString = "";
- for (int i=0;i<50;i++) {
- myHTMLString += "<font color=white>This is a test of a JEditPane inside a JScrollPane within Processing using OpenGL. Click my Hyperlink for </font><font color=#FF8040><a href=\"http://www.google.com\">Google</a></font><br/><br/></font>";
- }
- gui.setText(myHTMLString);
- }
- void draw() {
- background(100);
- }
- // An inner class for all GUI elements.
- class SwingGUI {
- int x, y, w, h;
- JScrollPane scroll;
- JEditorPane panel;
- PApplet parent;
- boolean visible;
- JPopupMenu jm = new JPopupMenu();
- /** Creates new form GUI */
- public SwingGUI(PApplet parent, int x, int y, int w, int h) {
- this.parent = parent;
- scroll = new JScrollPane();
- panel = new JEditorPane();
- try {
- // panel.setPage(url);
- Insets iSet = new Insets(5, 2, 5, 5);
- scroll.setBorder(null);
- scroll.setViewportView(panel);
- panel.setEditorKit(new HTMLEditorKit());
- panel.setContentType("text/html");
- panel.setMargin(iSet);
- panel.addHyperlinkListener(new MyHyperlinkListener());
- panel.setEditable(false);
- panel.setBackground(Color.black);
- panel.setForeground(Color.white);
- //
- }
- catch (Exception e) {
- }
- jm.setDefaultLightWeightPopupEnabled(false);
- jm.add(scroll);
- setLocation(x, y);
- setSize(w, h);
- }
- void setVisible(boolean visible) {
- this.visible = visible;
- jm.setVisible(visible);
- }
- boolean isVisible() {
- return visible;
- }
- void setSize(int w, int h) {
- this.w = w;
- this.h = h;
- jm.setPopupSize(this.w, this.h);
- }
- void setLocation(int x, int y) {
- Point p = parent.frame.getLocation();
- this.x = p.x + x;
- this.y = p.y + y;
- jm.setLocation(this.x, this.y);
- }
- void setText(String textstring) {
- try {
- InputStream is = new ByteArrayInputStream(textstring.getBytes("UTF-8"));
- panel.read(is, "html");
- }
- catch (Exception e) {
- }
- }
- class MyHyperlinkListener implements javax.swing.event.HyperlinkListener {
- public void hyperlinkUpdate(HyperlinkEvent evt) {
- if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
- getlink(evt.getURL().toString());
- }
- }
- }
- void getlink(String url) {
- link(url);
- }
- }
- void keyPressed() {
- println(key);
- }
2