0

I am developing my new application in Swing and I want to reuse JChempaint in this application. I have the jar file of JChempaint applet (which is developed in Swing using JApplet).

Basically, I want to add jar file to JPanel in my new application. In anyway, is this possible? JChempaint being open source, I also have the source code.

How can I add the JChempaint applet to a panel?


Following are the details after trying to implement the suggestions------ I started with my project and tried to develop a skeleton to embed the JChemPaint window in it. Following is the code of my layout:

package LearnSwingPkg;

import java.awt.BorderLayout;

class SplitPane extends JFrame  {

private JPanel panel1;
private JPanel panel2;
private JScrollPane panel3;
private JScrollPane panel4;

protected JSplitPane split;

public SplitPane(){

    super("Learn Swing");
    JFrame.setDefaultLookAndFeelDecorated(true);
    setSize(900, 700);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocation(0,0);

    setTitle( "Split Pane Application" );

    JPanel topPanel = new JPanel();
    topPanel.setLayout( new BorderLayout() );
    getContentPane().add( topPanel );

    // Create the panels
    createPanel1();
    createPanel2();
    createPanel3();
    createPanel4();

    JSplitPane spLeft = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true,panel1, panel3);
    JSplitPane spRight = new JSplitPane(JSplitPane.VERTICAL_SPLIT,true, panel2, panel4);

    split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true,spLeft, spRight);
    split.setOneTouchExpandable(true);

    getContentPane().add(split, BorderLayout.CENTER);



}
//top left
public void createPanel1(){
    panel1 = new JPanel();
    panel1.setLayout( new BorderLayout() );
    panel1.add((new TextArea("Panel1")));

    panel1.setPreferredSize( new Dimension( 450, 400 ) );
    panel1.setMaximumSize(new Dimension(450, 400));
}


//top right
public void createPanel2(){
    panel2 = new JPanel();
    panel2.setLayout( new BorderLayout() );
  panel2.add((new TextArea("Panel2")));
    panel2.setPreferredSize( new Dimension( 450, 400 ) );
    panel2.setMaximumSize(new Dimension(450, 400));

}

//bottom left
public void createPanel3(){
    Label label_prop = new Label("Properties:", Label.LEFT);

   String[] columnNames = {"Properties",
            "",
          };
    Object[][] data = {
            {"", "",}, {"", ""}, {"", ""},{"", ""},
            {"", "",}, {"", ""}, {"", ""},{"", ""},
            {"", "",}, {"", ""}, {"", ""},{"", ""} 
            };


    JTable table = new JTable(data, columnNames);
    table.setBackground(getBackground());
    table.setBackground(Color.LIGHT_GRAY);
    table.setRowHeight(20);
    table.setBorder(BasicBorders.getMenuBarBorder());

    panel3 = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZO
    panel3.add(label_prop);
    panel3.setPreferredSize( new Dimension( 20, 20 ) );
    panel3.setMinimumSize( new Dimension( 20, 20 ) );

}
//bottom right
public void createPanel4(){

     panel4 = new JScrollPane();
        //panel4.setLayout( new FlowLayout() );
     String[] columnNames = {"Activities",
                "",
              };
        Object[][] data = {
                    {"", "",}, {"", ""}, {"", ""},{"", ""},
                    {"", "",}, {"", ""}, {"", ""},{"", ""},
                    {"", "",}, {"", ""}, {"", ""},{"", ""} 
                    };


        JTable table = new JTable(data, columnNames);
        table.setBackground(getBackground());
        table.setBackground(Color.LIGHT_GRAY);
        table.setRowHeight(20);
        table.setBorder(BasicBorders.getMenuBarBorder());
        panel4 = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    panel4.setPreferredSize( new Dimension( 20, 20 ) );
    panel4.setMinimumSize( new Dimension( 20, 20 ) );


}

public static void main( String args[] ){
    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {}
    // Create an instance of the test application
    SplitPane mainFrame = new SplitPane();
    mainFrame.setVisible( true );
    mainFrame.setBackground(Color.blue);
    }
}

For time being, I have tried to insert an empty table,in the above code. Later, it will be populated with relevant data.

This gives me a Frame having four blocks, the upper left will have JCHemPaint window, Lower two blocks will have a table.

Now, in order to add JChemPaint in Panel 1 I edited the code in this file.I changed the method createPanel1 :

//top left
public void createPanel1(){
    panel1 = new JPanel();
    panel1.setLayout( new BorderLayout() );
    JChemPaint.showInstance(filename, null, null, debug);
    panel1.setPreferredSize( new Dimension( 450, 400 ) );
    panel1.setMaximumSize(new Dimension(450, 400));
}

This outputs me the JChemPaint window only.

I am unable to place it in to the panel 1 if my framework. How can I do this? Thank you!

pas
  • 109
  • 1
  • 13
  • 4
    A "jar", which is a zip file that contains Java classes and resources, is not a Swing component and thus cannot be treated as such. So the brief answer to your direct question, `"can I add a Jar to a JPanel?"` is no, you cannot. But if the jar file contains Swing classes that extend from Swing components such as from JPanel or JComponent, and the documentation or source code will be able to tell you this, then yes, you can use objects of these classes in your own Swing GUI. – Hovercraft Full Of Eels Nov 21 '14 at 00:06
  • 1
    Thank you so much. This will definitely help me. – pas Nov 21 '14 at 18:45

1 Answers1

3

As suggested here, JChemPaint is a standard Java application. See showInstance() for an example of constructing a JChemPaintPanel and adding it to a JFrame.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks @trashgod. I will look into this example and get back to you in case I have any problems. – pas Nov 21 '14 at 18:48
  • It looks like a hybrid applet/application; some related examples are cited [here](http://stackoverflow.com/a/12449949/230513). – trashgod Nov 22 '14 at 11:54