0

I am using container to display buttons and a combo box. Later to display contents from a database i am using g.drawString but it doesn't display anything in the applet. Here is my code

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;


public class Test extends JApplet implements ItemListener,ActionListener
{
    private String[] description = { "4.EarthWork", "5.Concrete", 
    "6.ReinforcedConcrete" };
    private JTextField t = new JTextField(15);

    private JComboBox c = new JComboBox();

    private JButton view = new JButton("VIEW");

    private JButton update = new JButton("UPDATE");

    private int count = 0;

    Container cp;

    ResultSet rs,rs2,rs3,rs4,con,rcon;

    Statement st;

    String msg = "";

    String str1,str2;

    public void init() 
    {

        try
        {
            String database="Proj.accdb";
            Connection 
            c=DriverManager.getConnection("jdbc:ucanaccess://W:\\1.Intern 
            Project\\Proj.accdb");
            st=c.createStatement();
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
        for (int i = 0; i < 3; i++)
        c.addItem(description[count++]);
        t.setEditable(true);
        t.setVisible(false);
        view.addActionListener(this);
        update.addActionListener(this);
        c.addItemListener(this);
        cp = getContentPane();
        cp.setLayout(new FlowLayout());
        cp.add(t);
        cp.add(c);
        cp.add(view);
        cp.add(update);

    }

    public void itemStateChanged(ItemEvent ie)
    {
        str2=(String)ie.getItem();
        System.out.println(str2);
        repaint();
    }

    public void actionPerformed(ActionEvent ae)
    {
        str1=ae.getActionCommand();
        System.out.println(str1);   

        repaint();


    }

    public void paint(Graphics g)
    {
            myMethod(g);
    }

    public void myMethod(Graphics g)
    {
        int y=200;
        if(str2=="4.EarthWork" && str1=="View")
        {
            try
            {
                 rs = st.executeQuery("select * from items where Itemid=1");

                while(rs.next())
                {
                    int i11 = rs.getInt("Itemid");
                    int id = rs.getInt("Id");
                    String name = rs.getString("IName");
                    String desc = rs.getString("Description");
                    String unit = rs.getString("Unit");
                    int price = rs.getInt("Price");
                    String s11 = String.valueOf(i11);
                    String s12 = String.valueOf(id);
                    String s13 = String.valueOf(price);

                    if(id==101)
                    {
                        g.drawString(s12,10,y);
                        g.drawString(name,20,y);
                        g.drawString(desc,30,y);
                        try
                        {
                            rs2 = st.executeQuery("select * from sitems where 
                             id = 101");
                        }

                        catch(Exception e)
                        {
                            System.out.println(e);
                        }

                        while(rs2.next())
                        {
                             y = (y+5);
                            String sname = rs2.getString("SName");
                            String unt = rs2.getString("Unit");
                            int p = rs2.getInt("Price");
                            String sp = String.valueOf(p);
                            g.drawString(sname,30,y);
                            g.drawString(unt,40,y);
                            g.drawString(sp,50,y);

                        }
                    }
                    if(id==102)
                    {
                        y = (y+5);
                        g.drawString(s12,10,y);
                        g.drawString(name,20,y);
                        g.drawString(desc,30,y);
                        try 
                        {
                            rs3 = st.executeQuery("select * from sitems where 
                            id=102");    
                        }

                        catch(Exception e)
                        {
                            System.out.println(e);
                        }

                        while(rs3.next())
                        {
                            y = (y+5);
                            String snm = rs3.getString("SName");
                            String ut = rs3.getString("Unit");
                            int pp = rs3.getInt("Price");
                            String spp = String.valueOf(pp);
                            g.drawString(snm,30,y);
                            g.drawString(ut,40,y);
                            g.drawString(spp,50,y);
                        }

                    }
                    if(id==103)
                    {
                        y = (y+5);
                        g.drawString(s12,10,y);
                        g.drawString(name,20,y);
                        g.drawString(desc,30,y);
                        try
                        {
                            rs4 = st.executeQuery("select * from sitems where 
                             id=103");   
                        }

                        catch(Exception e)
                        {
                            System.out.println(e);
                        }

                        while(rs4.next())
                        {
                            y = (y+5);
                            String snm1 = rs4.getString("SName");
                            String ut1 = rs4.getString("Unit");
                            int pp1 = rs4.getInt("Price");
                            String spp1 = String.valueOf(pp1);
                            g.drawString(snm1,30,y);
                            g.drawString(ut1,40,y);
                            g.drawString(spp1,50,y);
                        }
                    }
                    else
                    {
                        y = (y+5);
                        g.drawString(s12,10,y);
                        g.drawString(name,20,y);
                        g.drawString(desc,30,y);
                        g.drawString(unit,40,y);
                        g.drawString(s13,50,y);
                    }
                }
            }
            catch(Exception e)
            {  
                System.out.println(e);
            }
        }   

        if(str2=="5.Concrete" && str1=="View")
        { 
            try
            {
                con = st.executeQuery("select * from items where Itemid=2");

                while(con.next())
                {
                    y = (y+5);
                    int i111 = con.getInt("Itemid");
                    int i1dd1 = con.getInt("Id");
                    String name1 = con.getString("IName");
                    String desc1 = con.getString("Description");                     
                    String unit1 = con.getString("Unit");
                    int price1 = con.getInt("Price");
                    String s111 = String.valueOf(i111);
                    String s121 = String.valueOf(i1dd1);
                    String s131 = String.valueOf(price1);
                    g.drawString(s121,10,y);
                    g.drawString(name1,20,y);
                    g.drawString(desc1,30,y);
                    g.drawString(unit1,40,y);
                    g.drawString(s131,50,y);
                }
            }

            catch(Exception e)
            {
                System.out.println(e);
            }
        }   

        if(str2=="6.ReinforcedConcrete" && str1=="View")
        {
            try
            {
                rcon = st.executeQuery("select * from items where Itemid=2");



                while(rcon.next())
                {
                    y = (y+5);
                    int zxc = rcon.getInt("Itemid");
                    int id56 = rcon.getInt("Id");
                    String name5 = rcon.getString("IName");
                    String desc5 = rcon.getString("Description");
                    String unit5 = rcon.getString("Unit");
                    int price5 = rcon.getInt("Price");
                    String s115 = String.valueOf(zxc);
                    String s125 = String.valueOf(id56);
                    String s135 = String.valueOf(price5);
                    g.drawString(s125,10,y);
                    g.drawString(name5,20,y);
                    g.drawString(desc5,30,y);
                    g.drawString(unit5,40,y);
                    g.drawString(s135,50,y);
                }
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
        } 


    }



    public static void main(String[] args) 
    {
        run(new Test(), 800, 600);
    }

    public static void run(JApplet applet, int width, int height)
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(applet);
        frame.setSize(width, height);
        applet.init();
        applet.start();
        frame.setVisible(true);
    }
}

should i use g.drawstring or use a jtextfield instead and how to add that textfield to the applet. I am not getting any errors while compiling.

Charan2705
  • 19
  • 2
  • Applets are dead. They are no longer supported by browsers, Oracle or the community. `if(str2=="5.Concrete" && str1=="View")` - See [How to compare Strings in Java](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). Painting should run as fast as possible, doing database operations in a paint pass is a really, really bad idea – MadProgrammer Jun 15 '18 at 05:13
  • Top level containers aren't double buffered, so you should avoid painting directly to them (not to mention all the issues with overlapping components and independent painting) it's just a mess best avoided. Start by taking a look at [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) and [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) for more details about how painting works in Swing and how you should work with – MadProgrammer Jun 15 '18 at 05:15
  • You have a number of serious issue, more then is viably fixable within the constraints of the forum. You need to fix the immediate issue of comparing `String`s. I also don't know why you'd want to "paint" the results like this, instead of using a `JList` or `JTable` – MadProgrammer Jun 15 '18 at 05:18
  • And don't mix Applet's with frames, it just not required to be done – MadProgrammer Jun 15 '18 at 05:20

0 Answers0