الأربعاء، 11 مارس 2009

Create or Embed media player in Java program under windows

I wanted to create my own media player using java which has the codec for many media types.

NOTE: that works for windows OS.

Tools:
I used netbeans 6.5 but any IDE can do the job.
JDK (1.6.0_07) installed.
I use JMF as a library for the program but it's not required to install it.

Required Files:
jmf.properties
libfobs4jmf.a
fobs4jmf.ja
jmf.jar
fobs4jmf.dll


Steps:
1. Create a new project.
2. Add Libraries (fobs4jmf.jar, jmf.jar) to the project.
3. Create new JFrame class. make it like the picture.

NOTE: the panel in the middle is JPanle and name mediaPanel in code

4. Add class mediaPanel to your source package.
5. For The JMenuItem action (or the button that will open the file chooser) write this piece of code:

JFileChooser fc = new JFileChooser();
URL mediaURL;
if (mediaPanel instanceof MediaPanel)
((MediaPanel)mediaPanel).stopPlayer();

if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
try {
mediaURL = fc.getSelectedFile().toURI().toURL();
if ( mediaURL != null ) // Only display if there is a valid URL
{
mediaPanel = new MediaPanel(mediaURL);
showMedia();
}
} catch (MalformedURLException ex) {
Logger.getLogger(MediaPlayer.class.getName()).log(Level.SEVERE, null, ex);
}
}


6. Add a private method showMedia() and implement it as following:
THE TARGET IS TO ADD THE PANEL TO THE FRAME AGAIN SO WE CAN VIEW THE CHANGES
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(mediaPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(mediaPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);


7. Add the following files to your dist folder (beside the jar file)
"fobs4jmf.dll, jmf.properties, libfobs4jmf.a"

8. Run the program and browse for a file to play :)
NOTE: JMF doesn't support long named files.




References for more information:
Geertjan's Blog


Website counter

الأحد، 4 مايو 2008

Print Multi line header and footer in JTable

In this post I'll explain how to print the default JTable with a multi line header and multi line footer.

Here's the link to the class MyTablePrintable.

Here's how to use it:


JTable jTable2 = new JTable();

try {
PrinterJob job = PrinterJob.getPrinterJob();
MessageFormat[] header = new MessageFormat[3];
header[0] = new MessageFormat("");
header[1] = new MessageFormat("line 1");
header[2] = new MessageFormat("line 2");

MessageFormat[] footer = new MessageFormat[2];
footer[0] = new MessageFormat("footer 1");
footer[1] = new MessageFormat("footer 2");
job.setPrintable(new MyTablePrintable(jTable2, PrintMode.FIT_WIDTH, header, footer));
job.print();
} catch (PrinterException ex) {
Logger.getLogger(Export.class.getName()).log(Level.SEVERE, null, ex);
}


note that if u want to setup the printer and the printing dialog manipulate the job object.
note also that the 1st header line doesn't appear in the printing so i put an empty line. (bad solution i know, but it's to continue working ;) )