0

I have two actions here.one is for viewing the student information and next one is viewing the teacher information.

I would like to add the two tabs named as student and teacher,but dont know where to add the jpanels in the customized layout in java.

kindly help me to suggest the solution or point out the format to add.

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

public class Searchdb extends JFrame implements ActionListener {

//Initializing Components
    JLabel lb,lbd,lb1, lb2, lb3, lb5;
    JTextField tf1, tf2,tf3,tf5,tfd;
    JButton btn;

    //Creating Constructor for initializing JFrame components
    Searchdb() {
        //Providing Title
        super("Fetching Roll Information");




        lb5 = new JLabel("Roll Number:");
        lb5.setBounds(20, 20, 100, 20);
        tf5 = new JTextField(20);
        tf5.setBounds(130, 20, 200, 20);

        lbd = new JLabel("Date:");
        lbd.setBounds(20, 50, 100, 20);
        tfd = new JTextField(20);
        tfd.setBounds(130, 50, 200, 20);


        btn = new JButton("Submit");
        btn.setBounds(50, 50, 100, 20);
        btn.addActionListener(this);

        lb = new JLabel("Fetching Student Information From Database");
        lb.setBounds(30, 80, 450, 30);
        lb.setForeground(Color.black);
        lb.setFont(new Font("Serif", Font.PLAIN, 12));
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 500);

        lb1 = new JLabel("Name:");
        lb1.setBounds(20, 120, 100, 20);
        tf1 = new JTextField(50);
        tf1.setBounds(130, 120, 200, 20);
        lb2 = new JLabel("Fathername:");
        lb2.setBounds(20, 150, 100, 20);
        tf2 = new JTextField(100);
        tf2.setBounds(130, 150, 200, 20);
        lb3 = new JLabel("State:");
        lb3.setBounds(20, 180, 100, 20);
        tf3 = new JTextField(50);
        tf3.setBounds(130, 180, 200, 20);

        setLayout(null);

        //Add components to the JFrame
        add(lb5);
        add(tf5);
        add(lbd);
        add(tfd);
        add(btn);

        add(lb);
        add(lb1);
        add(tf1);
        add(lb2);
        add(tf2);
        add(lb3);
        add(tf3);


        //Set TextField Editable False
        tf1.setEditable(false);
        tf2.setEditable(false);
        tf3.setEditable(false);

    }

    public void actionPerformed(ActionEvent e) {
        //Create DataBase Coonection and Fetching Records

        //  First  panel dispalyed only the student below information.

        try {
            String str = tf5.getText();

            Datestri = tfd.getText();//Getting the unable to convert String to Date error

            System.out.println(str);
            System.out.println(stri);

            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con = DriverManager.getConnection("jdbc:oracle:thin:@//host:port/servicename","username","password");
            PreparedStatement st = con.prepareStatement("select Name,Fathername,State from student_db where roll_number=? and medium=?");
            System.out.println(st);
            st.setString(1, str);
            st.setDate(2, stri);



            //Excuting Query
            ResultSet rs = st.executeQuery();
            System.out.println(rs);

            if (rs.next()) {
                String s = rs.getString(1);
                String s1 = rs.getString(2);
                String s2 = rs.getString(3);


                //Sets Records in TextFields.
                tf1.setText(s);
                tf2.setText(s1);
                tf3.setText(s2);

            } else {
                JOptionPane.showMessageDialog(null, "Student not Found");
            }

            //Create Exception Handler
        } catch (Exception ex) {

            System.out.println(ex);
        }
    }

    public void actionPerformed(ActionEvent e) {
        //Create DataBase Coonection and Fetching Records

    //  Second panel dispalyed only the teacher information.
        //Teacher information should be retrieved from the db

        }

//Running Constructor

    public static void main(String args[]) {
        new Searchdb();
    }
}

Thanks for any help in advance.

sathya
  • 199
  • 5
  • 26
  • 1
    Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Jan 03 '17 at 22:38
  • i think layout manager is not best practice , that's why tried the custom layout and getting confusion every time which layout manger to select and use?@AndrewThompson – sathya Jan 04 '17 at 16:08
  • Provide ASCII art or a simple drawing of the GUI at minimum size, and if resizable, with more width and height (to show how the extra space is assigned). Id the student/teacher containers have different layouts, show twice as many ASCII art/drawings (one for each). – Andrew Thompson Jan 04 '17 at 22:19
  • Great I will try sure @Andrew Thompson – sathya Jan 05 '17 at 02:40

1 Answers1

2

Add this to your construcotr and then you can customize it as you wish:

JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setBounds(10, 211, 464, 240);
getContentPane().add(tabbedPane);

JPanel panel = new JPanel();
tabbedPane.addTab("New tab", null, panel, null);
panel.setLayout(null);

JLabel lblNewLabel_1 = new JLabel("New label");
lblNewLabel_1.setBounds(43, 35, 46, 14);
panel.add(lblNewLabel_1);

JPanel panel_1 = new JPanel();
tabbedPane.addTab("New tab", null, panel_1, null);
panel_1.setLayout(null);

JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.setBounds(50, 149, 46, 14);
panel_1.add(lblNewLabel);

Also i think you should use an other layout and not absolute for this form, if you resize the window the components are stay in same place same dimensions.

ddarellis
  • 3,912
  • 3
  • 25
  • 53