OiRc
OiRc

Reputation: 1622

JinternalFrame with jtable, inside a JFrame

I have this class and the other one to connect to database and show me a table of the database this part of program works pretty well the problem explained below ,but no:

import java.awt.Color;
import java.awt.event.*;
import java.sql.SQLException;
import javax.swing.*;
public class ManagerInterface {
public static JFrame ManagerInterface = new JFrame("Manager Interface");

public ManagerInterface() {
    StartInterfaceGUI();
}

public static JFrame getframe() {
    return ManagerInterface;
}
private void StartInterfaceGUI() {



    ManagerInterface.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ManagerInterface.setSize(1600, 900);
    new ShowEmployee();
    ManagerInterface.setVisible(true);
}
}
public static void main(String []args)
{
   new ManagerInterface();
}

and this class:

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.*;
import java.util.*;

import javax.swing.*;
import GUIManager.ManagerInterface;

public class ShowEmployee {

public static JInternalFrame frame = new JInternalFrame();
public JTable table = new JTable();
public JFrame mainframe = new JFrame();

public ShowEmployee() {

    frame.add(table);
    JScrollPane scroll = new JScrollPane(table);
    frame.getContentPane().add(scroll, BorderLayout.SOUTH);
    frame.setTitle("Employees");
    frame.setResizable(true);
    frame.setClosable(true);
    frame.setMaximizable(true);
    frame.setIconifiable(true);
    frame.setSize(650, 400);
    frame.pack();
    frame.setVisible(true);

           /* mainframe.add(frame);
    mainframe.setSize(650, 400);    //adding frame inside mainframe defined in this class
    mainframe.pack();
    mainframe.setVisible(true);*/


    //ManagerInterface.getframe().add(frame); //adding the internalframe to manager interface frame


}
 }

I use the ManagerInterface as a container for ShowEmployee, in this way:

The problem is the following:

In other words,i don't see the attributes row of the table represented by the ScrollPane,it is not visible at the inside of the frame managerInterface, I define the scrollpane in this way, defined in ShowEmployee. JScrollPane scroll = new JScrollPane (table); frame.getContentPane (.) add (scroll BorderLayout.SOUTH);

Upvotes: 0

Views: 2467

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209102

  1. As @kleopatra noted, follow java naming convention. variables start with lower case.

  2. Why in the world are you naming the JFrame ManagerInterface when the class name is ManagerInterface?

  3. Why in the world do you have two main methods? You only need it in the ManagerInterface, the launching class.

  4. Just make ShowEmployee subclass JInternalFrame. Then just add it to the JFrame (that you are going to name something else) that is in ManagerInterface

    public class ManagerInterface {
        private Frame frame;
        private ShowEmployees showEmployee;
    
        public ManagerInterface() {
            showEmployees = new ShowEmployees();
    
            frame = new JFrame("MagagerInterface");
            frame.add(new ShowEmployees());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativTo(null);
            frame.setVisible(true);
        }
    }
    
    public class ShowEmployees extends JInternalFrame {
        public ShowEmployees() {
    
        }
    }
    
  5. Adding on to 4. You should be adding JInternalFrame to JDesktopPanes and not JFrame

    JDesktopPane desktop;
    
    public ManagerInterface() {
        showEmployees = new ShowEmployees();
        desktop = new JDesktopPane();
        desktop.add(showEmployees);
    
        frame = new JFrame("MagagerInterface");
        frame.setContentPane(desktop);
        ....
    }
    
  6. Run your Swing apps from the EDT

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new ManagerInterface();
            }
        });
    }
    

    See Initial Threads

  7. The below doesn't cause a problem, but you should know that a parent can only have one parent container. So you trying to add the table to the frame and the scroll pane shouldn't be done. Just add the scroll pane

    frame.add(table);   <<---------------------Get Rid of MEEEE!
    JScrollPane scroll = new JScrollPane(table);
    frame.getContentPane().add(scroll, BorderLayout.SOUTH);
    

Here is a running example with all the above mentioned fixes.

import javax.swing.*;

public class ManagerInterface {

    public JFrame frame = new JFrame("Manager Interface");

    private ShowEmployee showEmployee;
    private JDesktopPane desktop;

    public ManagerInterface() {
        showEmployee = new ShowEmployee();
        desktop = new JDesktopPane();
        desktop.add(showEmployee);

        frame = new JFrame("MagagerInterface");
        frame.setContentPane(desktop);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ManagerInterface();
            }
        });
    }
}

class ShowEmployee extends JInternalFrame {

    String[][] data = {{"Hello", "Hello", "Hello"},
    {"Hello", "Hello", "Hello"}};
    String[] cols = {"Col 1", "Col 2", "Col 3"};

    public JTable table = new JTable(data, cols);

    public ShowEmployee() {

        JScrollPane scroll = new JScrollPane(table);
        getContentPane().add(scroll);
        setTitle("Employees");
        setResizable(true);
        setClosable(true);
        setMaximizable(true);
        setIconifiable(true);
        pack();
        setVisible(true);

    }
}

Upvotes: 3

Related Questions