swing horizontal scroll bar problem - swing

public TaskGraphComponent(ProjectFrame proFrame,TaskGraphModel model, painter) {
this.proFrame = proFrame;
_painter = painter;
setModel(model);
_mouseHandler = new GraphMouseHandler(this);
_verticalScroll = new JScrollBar(JScrollBar.VERTICAL);
this.add(_verticalScroll, BorderLayout.EAST);
_verticalScroll.addAdjustmentListener(this);
_verticalScroll.setVisible(true);
_horizontalScroll = new JScrollBar(JScrollBar.HORIZONTAL);
add(_horizontalScroll, BorderLayout.SOUTH);
_horizontalScroll.addAdjustmentListener(this);
_horizontalScroll.setVisible(true);
setBorder(BorderFactory.createEmptyBorder(0,0,15,0));
this.addMouseMotionListener(_mouseHandler);
this.addMouseListener(_mouseHandler);
this.addMouseWheelListener(_mouseHandler);
this.addKeyListener(_mouseHandler);
this.addComponentListener(this);
this.setFocusable(false);
ToolTipManager.sharedInstance().setDismissDelay(8000);
ToolTipManager.sharedInstance().setReshowDelay(3000);
}
I'm extending from JComponent here vertical scroll bar working fine but horizontal
scroll is not showing.
Here TaskGraphComponent is jcomponent

..not using any layout..
That is the problem. Use layouts.

I tested this code please run it and let me know. I change the code from the above link:
http://www.java2s.com/Code/Java/Swing-JFC/AquickdemonstrationofJScrollBarbothverticalandhorizontal.htm
import java.awt.BorderLayout;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollBar;
public class SwingScrollBarExample extends JComponent implements AdjustmentListener {
JLabel label;
public SwingScrollBarExample() {
label = new JLabel();
setLayout(new BorderLayout());
JScrollBar _verticalScroll = new JScrollBar(JScrollBar.VERTICAL);
this.add(_verticalScroll);
_verticalScroll.addAdjustmentListener(this);
_verticalScroll.setVisible(true);
JScrollBar _horizontalScroll = new JScrollBar(JScrollBar.HORIZONTAL);
this.add(_horizontalScroll);
_horizontalScroll.addAdjustmentListener(this);
_horizontalScroll.setVisible(true);
this.add(_verticalScroll, BorderLayout.EAST);
this.add(_horizontalScroll, BorderLayout.SOUTH);
}
public void adjustmentValueChanged(AdjustmentEvent e) {
label.setText(" New Value is " + e.getValue() + " ");
repaint();
}
public static void main(String s[]) {
JFrame frame = new JFrame("Scroll Bar Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new SwingScrollBarExample());
frame.setSize(200, 200);
frame.setVisible(true);
}
}

Related

Unable to Switch JPanels after loading jcef browser window

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();

How to visualize my R plot in my JFrame with JavaGD?

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 :)

How to fit a JTable into a JPanel and make it auto resizeable?

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);
}
});
}

how to display the contents of a tab in the workspace of Jtabbedpane?

i'v put a jpanel on the tab of Jtabbedpane which is at the left side ,nw when the tab is clicked i want to display labels and textfields in the workspace of the tabbedpane,plz hlp.
When you say 'workspace of the tabbedpane' I think you are referring to your JPanel?
You must attach the JLabels and JTextfields to the JPanel using JPanel.add() before you add the JPanel to the JTabbedPane.
I think you should do exactly as #7SpecialGems says (+1 for that).
Here is an example showing how exactly it can look like in code.
import java.awt.Color;
import java.awt.HeadlessException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class JTabbedPaneSample
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run()
{
createGUI();
}
});
}
private static void createGUI() throws HeadlessException
{
JPanel oi1 = new JPanel();
oi1.setBackground(Color.RED);
oi1.add(new JLabel("Label 1"));
JPanel oi2 = new JPanel();
oi2.setBackground(Color.PINK);
oi2.add(new JLabel("Label 2"));
oi2.add(new JTextField("TextField 1"));
JTabbedPane tp = new JTabbedPane();
tp.addTab("Oi1", oi1);
tp.addTab("Oi2", oi2);
JPanel contentPane = new JPanel();
contentPane.add(tp);
JFrame f = new JFrame();
f.setContentPane(contentPane);
f.setSize(800, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Good luck, Boro.

my program exits from close button of window but not from my exit button.help me i have used System.exit(0).tell me another way

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!