943,534 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3252
  • Java RSS
Feb 21st, 2009
0

add Image to Jwindow in BorderLayout.CENTER

Expand Post »
Hi,
How to add image to Jwindow. Here is my code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.io.*;
class Project4 extends JWindow
{

Container container;
JTextField jt;
JButton jb;
public Project4(String st, final Image image)
{

container = getContentPane();
container.setLayout(new BorderLayout());
//JTextArea jtp = new JTextArea(st,10,10);

//container.add(sp);
container.add(new JButton("RGUKT Examinations"),BorderLayout.NORTH);
container.add(new JButton("Close"),BorderLayout.SOUTH);
container.add(new JButton(""),BorderLayout.EAST);
container.add(new JButton(""),BorderLayout.WEST);
JTextArea textArea = new JTextArea()
{

{setOpaque(false);}

public void paintComponent (Graphics g)
{
g.drawImage(image, 0, 0, (int)getSize().getWidth(), (int)getSize().getHeight(), this);
//g.drawImage(grayImage, 0, 0, (int)getSize().getWidth(), (int)getSize().getHeight(), this);
super.paintComponent(g);
}
};
JScrollPane sp=new JScrollPane(textArea);
sp.createVerticalScrollBar();
container.add(textArea);

}
public static void main(String[] args) throws IOException
{
FileInputStream fl1= new FileInputStream("Project2.java");
DataInputStream in = new DataInputStream(fl1);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine, st="";
while ((strLine = br.readLine()) != null)
{
st+=strLine;
st+="\n";
}
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.createImage("bg.jpg");
Project4 demo = new Project4(st,image);
//demo.setDefaultCloseOperation(JWindow.EXIT_ON_CLOSE);
demo.setSize(1200,800);
demo.setVisible(true);
demo.setAlwaysOnTop(true);


}

}


I got Image in center in border Layout. but i didn't get Scrollbar.
Please please help me.
Thanks in advance
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Narayan15 is offline Offline
20 posts
since Nov 2008
Feb 21st, 2009
0

Re: add Image to Jwindow in BorderLayout.CENTER

You need to add the scroll pane to your container instead of the text area. Change this
Java Syntax (Toggle Plain Text)
  1. container.add(textArea);
to this
Java Syntax (Toggle Plain Text)
  1. container.add(sp);
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Relative Path
Next Thread in Java Forum Timeline: How do i sort in descending?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC