Reputation: 1622
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:
in ManagerInterface I call a JFrame
the class ShowEmployee is represented by a JInternalFrame on which add a JTable.
The problem is the following:
if I define a frame(in this case mainframe) inside ShowEmployee and I add the internalframe i see this:
however, if I add the JInternalFrame to the frame ManagerInterface I see this:
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
Reputation: 209102
As @kleopatra noted, follow java naming convention. variables start with lower case.
Why in the world are you naming the JFrame
ManagerInterface
when the class name is ManagerInterface
?
Why in the world do you have two main
methods? You only need it in the ManagerInterface
, the launching class.
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() {
}
}
Adding on to 4. You should be adding JInternalFrame
to JDesktopPane
s and not JFrame
JDesktopPane desktop;
public ManagerInterface() {
showEmployees = new ShowEmployees();
desktop = new JDesktopPane();
desktop.add(showEmployees);
frame = new JFrame("MagagerInterface");
frame.setContentPane(desktop);
....
}
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
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