I am new to JRI/rJava/JavaGD and have some problems with it. I drew a simple R plot with JRI and want to include this plot in my customized JFrame. I added the GDCanvas in which the plot should appear to my JFrame. However the plot is not displayed in the GDCanvas, but opens in a new Frame. How can I visualize my R plot in my JFrame, instead of appearing in its own frame?
For me, another possibility would be to modify the new frame in which my plot pops up. But I couldn't add or modify anything there either. Is there a special way to modify frames that appear with JavaGD()?
Can someone please help me? Many thanks in advance.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import org.rosuda.JRI.Rengine;
import org.rosuda.javaGD.GDCanvas;
public class RjavaGD extends JFrame implements ActionListener {
private Rengine engine;
public static GDCanvas gdc;
private JButton btn;
public RjavaGD() {
super();
super.setTitle("My R Plot");
btn = new JButton("show plot");
btn.addActionListener(this);
gdc = new GDCanvas(400, 400);
gdc.setBackground(Color.PINK);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(btn, BorderLayout.PAGE_START);
this.getContentPane().add((GDCanvas) gdc, BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
// initialize R
engine = new Rengine(new String[] { "--vanilla" }, false, null);
engine.eval("library(JavaGD)");
engine.eval("Sys.putenv('JAVAGD_CLASS_NAME'='RjavaGDInterface')");
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new RjavaGD();
}
});
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn) {
engine.eval("JavaGD()");
engine.eval("a <- c(1,2,3,2,4)");
engine.eval("plot(a,type=\"l\")");
gdc.initRefresh();
engine.end();
this.setTitle("new random plot");
}
}
}
import org.rosuda.javaGD.GDInterface;
public class RjavaGDInterface extends GDInterface {
public void gdOpen(double w, double h)
{
c = RjavaGD.gdc;
}
}
I used Sys.setenv() instead of Sys.putenv().
I also found a way to integrate the plot into my own customized frame, so that I can add buttons and other stuff directly below the plot, which was not possible with the standard frame that opens automatically.
This code worked fine for me:
import org.rosuda.JRI.Rengine;
import org.rosuda.javaGD.GDCanvas;
public class RjavaGD extends JFrame implements ActionListener {
private Rengine engine;
private JButton btn;
public RjavaGD() {
super();
super.setTitle("My R Plot");
btn = new JButton("show plot");
btn.addActionListener(this);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(btn, BorderLayout.PAGE_START);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn) {
// initialize R
engine = new Rengine(new String[] { "--vanilla" }, false, null);
engine.eval("Sys.setenv('JAVAGD_CLASS_NAME'='RjavaGDInterface')");
engine.eval("library(JavaGD)");
engine.eval("JavaGD()");
engine.eval("a <- c(1,2,3,2,4)");
engine.eval("plot(a,type=\"l\")");
engine.end();
}
}
}
import org.rosuda.javaGD.GDInterface;
public class RjavaGDInterface extends GDInterface {
JFrame f;
#Override
public void gdOpen(double w, double h) {
super.gdOpen(w,h);
f = new JFrame();
f.setLayout(new BorderLayout());
c = new GDCanvas(w, h);
f.setTitle("New Plot");
f.getContentPane().add((GDCanvas) c, BorderLayout.CENTER);
f.getContentPane().add(buttonPanel(), BorderLayout.NORTH);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
private JPanel buttonPanel(){
JPanel p = new JPanel();
p.setBackground(Color.pink);
p.add(new JLabel("Options“));
p.add(new JButton("option1“))
return p;
}
}
I hope this will help other people with the same problem :)
Related
I am trying to use jcef browser inside my swing application but getting problems.
First of all i unable to add jcef browser as JPanel component on jFrame. Then i try to add directly on jframe
[code]getContentPane().add(browser.getUIComponent(), BorderLayout.CENTER);[/code]
Now when browser window load inside JFrame, and If i want to switch with other Jpanel then it is not working working in any way
I cant switch the screen after loading CEF browser. Can any one point out what i need to do. Here is my test jframe.
import org.cef.CefApp;
import org.cef.CefClient;
import org.cef.browser.CefBrowser;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class CardLayoutTst extends JFrame {
static CefBrowser browser = null;
static CefClient client = null;
private static final long serialVersionUID = 1L;
private JPanel cardPanel, jp1, jp2, buttonPanel;
private JLabel jl1, jl2;
private JButton btn1, btn2;
private CardLayout cardLayout = new CardLayout();
public CardLayoutTst() {
setTitle("Test med CardLayout");
setSize(400, 300);
cardPanel = new JPanel();
buttonPanel = new JPanel();
cardPanel.setLayout(cardLayout);
jp1 = new JPanel();
jp2 = new JPanel();
jl1 = new JLabel("Card 1");
jl2 = new JLabel("Card 2");
jp1.add(jl1);
jp2.add(jl2);
cardPanel.add(jp1, "1");
cardPanel.add(browser.getUIComponent(), "2");
btn1 = new JButton("Show Card 1");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardLayout.show(cardPanel, "1");
}
});
btn2 = new JButton("Show Card 2");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardLayout.show(cardPanel, "2");
}
});
buttonPanel.add(btn1);
buttonPanel.add(btn2);
add(cardPanel, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.SOUTH);
addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
CefApp.getInstance().dispose();
dispose();
}
});
}
public static void main(String[] args) {
client = CefApp.getInstance().createClient();
browser = client.createBrowser("http://www.google.com", false, false);
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
CardLayoutTst frame = new CardLayoutTst();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
It is a bit late but I had just the same problem. A friend of mine solved the issue.
You have to create CefSettings, set the windowless_rendering_enabled to false and pass this settings object to the getInstance call:
CefSettings settings = new CefSettings();
settings.windowless_rendering_enabled = false;
client = CefApp.getInstance(settings).createClient();
I'm rewriting a Swing application in JavaFX, where I allow users to present multiple workspaces as either windows or tabs. However, my FX code will not display the contents moved from more than one tab into a new stage; only the contents of the currently-selected tab appear in my new stages. I've distilled my code into a small example below. Can anyone clue me in as to what's gone wrong?
package scenes;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class StageSwapper extends Application {
static public void main(String[] args) {
launch(args);
}
private TabPane tabs = new TabPane();
public void start(Stage stage) {
stage.setTitle("Stage Swapper");
BorderPane p = new BorderPane();
p.setCenter(tabs);
tabs.getTabs().addAll(new Swapee("First").createTab(), new Swapee("Second").createTab());
Scene s = new Scene(p);
stage.setScene(s);
stage.show();
launchSwap();
}
private void launchSwap() {
new Thread() {
public void run() {
try {
sleep(10000);
} catch (Exception e) {
e.printStackTrace();
}
Platform.runLater(new Runnable() {
public void run() {
for (Swapee s : Swapee.list) {
createWindow(s);
}
}
});
}
}.start();
}
public void createWindow(Swapee s) {
Stage window = new Stage();
window.setTitle("New Window");
window.setY(200);
window.setX(200);
BorderPane p = new BorderPane();
p.setCenter(s);
window.setScene(new Scene(p));
window.show();
}
}
class Swapee extends Label {
static private int count;
static ArrayList<Swapee> list = new ArrayList<>();
String name;
Swapee(String name) {
super("Swappable Item " + ++count);
this.name = name;
list.add(this);
}
Tab createTab() {
Tab t = new Tab(name);
t.setContent(this);
return t;
}
}
You haven't specified the size of the windows that you're creating. Right now they have width and length equal to 0. You may use the following approach:
BorderPane p = new BorderPane();
p.setPrefSize(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE);
p.setCenter(s);
BorderPane will be resized according to its content and the window will be resized as well.
i'm adding a JTable into a JPanel wich is using a GridLayout but i don't get the same behavior like when you add a JButton...
I want to know how can i get the same auto resizing behavior with my JTable like when you add a JButton into a JPanel that uses a GridLayout and also I want the table use the whole space of the panel.
Sorry about spelling, english is not my maternal language. Hope you guys can help me!
This is my code:
import javax.swing.*;
import java.awt.*;
class Frame extends JFrame {
private JPanel center;
private JTable table;
public Frame(){
super("Test Jtable");
this.setLayout(new BorderLayout());
this.center = new JPanel();
this.center.setLayout(new GridLayout());
this.table = new JTable(50,50);
this.table.setGridColor(Color.black);
this.table.setCellSelectionEnabled(true);
this.center.add(this.table);
this.add(this.center,BorderLayout.CENTER);
}
}
public class TestFrame {
public static void main(String... args) {
Frame f =new Frame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400,400);
f.setVisible(true);
}
}
Add your table to a scroll pane, as shown in this example.
this.center.add(new JScrollPane(this.table));
Addendum: I'd advocate something like this using pack(); other arrangements are possible, but less usable. The setPreferredScrollableViewportSize() method may be helpful, too.
import javax.swing.*;
import java.awt.*;
class Frame extends JFrame {
private JPanel center;
private JTable table;
public Frame() {
super("Test Jtable");
this.setLayout(new BorderLayout());
this.center = new JPanel();
this.center.setLayout(new GridLayout());
this.table = new JTable(50, 10);
this.table.setGridColor(Color.black);
this.table.setCellSelectionEnabled(true);
this.center.add(this.table);
this.add(new JScrollPane(this.center), BorderLayout.CENTER);
}
}
public class TestFrame {
public static void main(String... args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
Frame f = new Frame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
});
}
I have two text fields and I can drag and drop the text between them. What I want is that every time I drag the text it will replace the existing text data with the text which was dragged and dropped.
import java.awt.Container;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class DragDropText extends JFrame {
public static void main(String[] args) {
new DragDropText().setVisible(true);
}
public DragDropText() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField field1 = new JTextField("Life's a drag", 20);
JTextField field2 = new JTextField("and then you drop", 20);
field1.setDragEnabled(true);
field2.setDragEnabled(true);
Container content = getContentPane();
content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
content.add(field1);
content.add(field2);
pack();
}
}
You can achieve the effect by creating and setting a subclass of TransferHandler.
This is an example that will work for any subclass of JTextComponent. You'll have to add the appropriate checks to make it robust.
You can find more info here: http://download.oracle.com/javase/tutorial/uiswing/dnd/transferhandler.html.
import java.io.*;
import java.awt.*;
import java.awt.datatransfer.*;
import javax.swing.*;
import javax.swing.text.*;
public class DragDropText extends JFrame {
public static void main(String[] args) {
new DragDropText().setVisible(true);
}
public DragDropText() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField field1 = new JTextField("Life's a drag", 20);
JTextField field2 = new JTextField("and then you drop", 20);
field1.setDragEnabled(true);
field2.setDragEnabled(true);
field1.setTransferHandler(new CustomTransferHandler());
field2.setTransferHandler(new CustomTransferHandler());
Container content = getContentPane();
content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
content.add(field1);
content.add(field2);
pack();
}
}
class CustomTransferHandler extends TransferHandler {
public int getSourceActions(JComponent c) {
return COPY_OR_MOVE;
}
public Transferable createTransferable(JComponent c) {
return new StringSelection(((JTextComponent) c).getSelectedText());
}
public void exportDone(JComponent c, Transferable t, int action) {
if(action == MOVE)
((JTextComponent) c).replaceSelection("");
}
public boolean canImport(TransferSupport ts) {
return ts.getComponent() instanceof JTextComponent;
}
public boolean importData(TransferSupport ts) {
try {
((JTextComponent) ts.getComponent())
.setText((String) ts
.getTransferable()
.getTransferData(DataFlavor.stringFlavor));
return true;
} catch(UnsupportedFlavorException e) {
return false;
} catch(IOException e) {
return false;
}
}
}
Here's my code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;
import javax.swing.JOptionPane;
import java.util.*;
import java.applet.*;
/*<applet code=tabledemo.class height=300 width=300></applet>*/
public class tabledemo extends JApplet implements ActionListener
{
JScrollPane jsp1;
JPanel jp,jp1,jp2,jp3;
JTable jt1;
int i,j;
DefaultTableModel tm=new DefaultTableModel(3,3);
String data[][]={{"1001","Ram","BCA"},
{"1002","Sham","MCA"},
{"1003","Mohan","BCA"}};
String head[]={"Rollno","Name","Course"};
JTextField jtf1,jtf2,jtf3;
JButton b1,b2,b3,b4;
public void init()
{
jp=new JPanel();
jp.setLayout(new BorderLayout());
jp1=new JPanel();
jp1.setLayout(new BorderLayout());
jp2=new JPanel();
jp2.setLayout(new FlowLayout());
jp3=new JPanel();
jp3.setLayout(new FlowLayout());
jt1=new JTable();
jt1.setModel(tm);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
tm.setValueAt(data[i][j],i,j);
}
}
jp1.add(jt1,BorderLayout.CENTER);
jp.add(jp1,BorderLayout.CENTER);
jtf1=new JTextField(25);
jtf2=new JTextField(25);
jtf3=new JTextField(25);
jp2.add(jtf1);
jp2.add(jtf2);
jp2.add(jtf3);
b1=new JButton("Add Row");
b2=new JButton("Display Count");
b3=new JButton("Coursewise Count");
b4=new JButton("Exit");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
jp1.add(jp2,BorderLayout.SOUTH);
jp3.add(b1);
jp3.add(b2);
jp3.add(b3);
jp3.add(b4);
jp.add(jp3,BorderLayout.SOUTH);
int v,h;
v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
jsp1=new JScrollPane(jp,v,h);
Container c=getContentPane();
c.add(jsp1);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("Add Row"))
{
String [] r={jtf1.getText(),jtf2.getText(),jtf3.getText()};
tm.addRow(r);
}
else if(ae.getActionCommand().equals("Display Count"))
{
String name;
name=JOptionPane.showInputDialog("enter student name:");
jtf1.setText(name);
}
else if(ae.getActionCommand().equals("Exit"))
{
int ans=0;
ans=JOptionPane.showConfirmDialog(null,"Are you sure");
if(ans==JOptionPane.YES_OPTION)
{
dispose();
System.exit(0);
//processEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
}
}
}
}
As Daniel already said, you cannot close the browser from your applet, as it is not allowed for security reasons.
The only possibilities you have are to use the showDocument method to redirect to a new website, or you simply hide your applet by using swing methods (eg. setVisible).
You could also let your applet create a new frame which can be closed!