Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

setVisible(true) not working properly in web application  RSS feed

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a code which is executing correctly(gragh is displayed) when I run as a standalone through Command Prompt. But when I place the same code in a servlet, I am not getting the graph displayed.

The code is as follows,

import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class semilog extends JPanel {
double[] data;
public semilog(){
}
public semilog(double[] data){
this.data=data;
}
final int PAD = 20;


protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
int w=getWidth();
int h = getHeight();

// Draw ordinate.
g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));

// Draw abcissa.
g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));

//some other code

}



public void draw(double[] data) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new semilog(data));
f.setSize(400,400);
f.setLocation(200,200);
f.repaint(1);
f.setVisible(true);
System.out.println(f.isVisible());
System.out.println(f.isShowing());

}

}


When I call this code from a servlet, f.isVisible() and f.isShowing() are returning true but no graph is displayed. But when I run through Command Prompt, it is working.

Can anyone help me with this?
 
Sheriff
Posts: 21603
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't run Swing code in a servlet. Well, perhaps you can, but the actual displaying of the Swing components won't work. You'll need an applet for that, or use a different way for showing the graph.

The good news is, an image can work:
Of course you can use "image/jpeg" and "JPEG" instead of PNG if you prefer JPG images.

Moving to Servlets.
 
chandana nannapaneni
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou Rob. Thanks a lot. The code is working.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!