Sunday 5 July 2015

Filled Under:

Opening Image In JText Pane Using File Chooser

JText pane is a swing component which is used when user want to show a custom styled text and images in it and one big advantage if jtextpane is that it can be used to display a text from specific URL.

CONSTRUCTORS OF JTEXTPANE:

JEditorPane(URL)
JEditorPane(String)


These two constructor are used to create editor pane that has a text of  specified URL.

setPage(URL)
setPage(String)

Used to load a page on jtext pane of specified URL.

CODING:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class myjframe1 extends JFrame implements ActionListener

{

JMenuBar menuBar = new JMenuBar();  

JMenu file = new JMenu();
JMenuItem open = new JMenuItem();
JTextPane textpane =new JTextPane();

public myjframe1()

{

setTitle("JTextPane");

setSize(300, 200);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);      
textpane = new JTextPane();
textpane.setPreferredSize( new Dimension( 800, 800 ) );
menuBar.add(file);
file.setText("File");
file.add(open);
setJMenuBar(menuBar);
open.setText("OPEN");
setLayout(new FlowLayout(FlowLayout.CENTER));
JScrollPane js = new JScrollPane(textpane,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
add(js);
open.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == open)

try

{
JFileChooser open = new JFileChooser();
int option = open.showOpenDialog(this);
textpane.insertIcon( new ImageIcon (open.getSelectedFile().getPath()));
}
catch(Exception ae)
{
System.out.println(ae);
}
}
}
class text
{
public static void main(String args[])
{
myjframe1 x= new myjframe1();
}
}

OUTPUT:


opening image in jtextpane java

0 comments:

Post a Comment