Home » Developer & Programmer » JDeveloper, Java & XML » pojo (4 Merged)
pojo (4 Merged) [message #552131] Mon, 23 April 2012 14:18 Go to next message
ishu
Messages: 1
Registered: April 2012
Junior Member
package project2;

import javax.swing.*;
import javax.swing.table.*;

import java.awt.*;

import java.lang.*;
import java.lang.Character.*;

import java.awt.BorderLayout;
import java.awt.Dimension;

import java.awt.Rectangle;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class POJO extends JFrame {
    private JLabel jLabel1 = new JLabel();
    private JTextField jTextField1 = new JTextField();
    private DefaultTableModel model;
    private int count = 0;
    private JTable table;
    private JLabel jLabel2 = new JLabel();
    private JButton jButton1 = new JButton();

    public POJO() {
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        this.getContentPane().setLayout(null);
        this.setSize(new Dimension(400, 300));
        jLabel1.setText("Class Name");
        jLabel1.setBounds(new Rectangle(185, 15, 100, 15));
        jTextField1.setBounds(new Rectangle(370, 15, 125, 20));

        model = new DefaultTableModel();
        model.addColumn("Type");
        model.addColumn("Field Name");


        table = new JTable(model);
        table = new JTable(model);

        JButton addButton = new JButton("Add Row");
        addButton.setBounds(400, 400, 201, 21);
        addButton.setBounds(new Rectangle(235, 305, 201, 21));
        addButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent event) {
                    String[] philosopher = { "", "" };
                    model.addRow(philosopher);
                    count++;
                }
            });
        JScrollPane s = new JScrollPane(table);
        s.setBounds(new Rectangle(85, 95, 500, 200));
        s.setBounds(new Rectangle(90, 95, 500, 200));
        jButton1.setText("Generate POJO");
        jButton1.setBounds(new Rectangle(240, 350, 195, 20));
        jButton1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    try{ jButton1_actionPerformed(e);
                    }
                    catch(Exception ae)
                    {System.out.println(ae);
                    }
                }
            });
        jLabel2.setText("Enter the fields along with their types");
        jLabel2.setBounds(new Rectangle(230, 70, 295, 20));


        this.getContentPane().add(jButton1, null);
        this.getContentPane().add(jLabel2, null);
        this.getContentPane().add(s, BorderLayout.CENTER);
        this.getContentPane().add(addButton);
        this.getContentPane().add(jTextField1, null);
        this.getContentPane().add(jLabel1, null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(700, 700);
        setVisible(true);
    }

    public static void main(String[] q) {
        new POJO().setVisible(true);

    }

    private void create() throws Exception {
        Writer output = null;
           
        File file = new File(jTextField1.getText()+".java");
           output = new BufferedWriter(new FileWriter(file));
           System.out.println("public class "+jTextField1.getText()+"{");
         output.write("public class "+jTextField1.getText()+"{\n");
         int i;
        for (  i = 0; i < count; i++) {
            String type = table.getValueAt(i, 0).toString();
            String field = table.getValueAt(i, 1).toString();
            if (type == "")
                continue;
            if (field == "")
                continue;
        System.out.println(type +" "+field+";\n");
        output.write(type +" "+field+";\n");
        
        
        }
        for (  i = 0; i < count; i++) {
            String type = table.getValueAt(i, 0).toString();
            String field = table.getValueAt(i, 1).toString();
            String small = table.getValueAt(i, 1).toString();
            if (type == "")
                continue;
            if (field == "")
                continue;
            char a = Character.toUpperCase(field.charAt(0));

            field =
                    field.replaceFirst(Character.toString(field.charAt(0)), Character.toString(a));

            System.out.println("public void set" + field + "(" + type + " " +
                               small + "){\n");
            output.write("public void set" + field + "(" + type + " " +
                               small + "){\n");
            System.out.println("this." + small + "=" + small + ";\n");
            output.write("this." + small + "=" + small + ";\n");
            System.out.println("\n");
            output.write("}\n");
            System.out.println("public " + type + " " + " get" + field +
                               "( ){\n");
            output.write("public " + type + " " + " get" + field +
                               "( ){\n");
            System.out.println("return this." + small + ";\n");
            output.write("return this." + small + ";\n");
            System.out.println("}\n");
            output.write("}\n");
        }
        System.out.println("}\n"); 
        output.write("}\n");
        output.close();
    }

    private void jButton1_actionPerformed(ActionEvent e) throws Exception{
        create();

    }
}



[mod-edit: code tags added by bb; next time please add them yourself]

[Updated on: Mon, 23 April 2012 15:11] by Moderator

Report message to a moderator

Re: pojo [message #552135 is a reply to message #552131] Mon, 23 April 2012 14:38 Go to previous messageGo to next message
BlackSwan
Messages: 21967
Registered: January 2009
Senior Member
Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/
Re: pojo (4 Merged) [message #552136 is a reply to message #552131] Mon, 23 April 2012 14:55 Go to previous messageGo to next message
Barbara Boehmer
Messages: 7861
Registered: November 2002
Location: California, USA
Senior Member
So, you posted some java code. What about it? Is there a question here? Is the title "pojo" supposed to mean something to us?
Re: pojo (4 Merged) [message #552137 is a reply to message #552136] Mon, 23 April 2012 15:05 Go to previous message
Littlefoot
Messages: 18844
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Maybe Plain Old Java Object (but it still means nothing to me)?
Previous Topic: Procedure to query an XML document
Next Topic: XML Error LPX-00283: document encoding is UTF-8-based but default input encoding is not
Goto Forum:
  


Current Time: Wed Apr 23 18:21:54 CDT 2014

Total time taken to generate the page: 0.10580 seconds