JinternalFrame with jtable, inside a JFrame - java

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.
I add the JInternalFrame to the frame of managerInterface class, that is defined by the line ManagerInterface.getframe.add.(frame), inserted in ShowEmployee.
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);

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 JDesktopPanes 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);
}
}

Related

GUI doesn't show up on a Mac?

I bought a Mac, I I download netbeans for my java.
package gui;
import javax.swing.*;
import java.awt.*;
public class Gui extends JFrame {
public void Gui(){
setTitle("Gui");
setSize(640,320);
setVisible(true);
}
public static void main(String[] args) {
new Gui();
}
}
It is very easy code and I didn't find any problem with it, but somehow the GUI is not showing up.
is GUI no suppose to show up on a Mac?
Somehow, the program didn't go through the Gui method, I tried
System.out.println("Hello");
didn't show up.
You think you're using a constructor but you are not! The constructor is what makes the app become a JFrame. This line:
public void Gui() {
should be:
public Gui() {
Also, nice to add a setMinimumSize(new Dimension(640,320));
I think the problem is you have a empty container, but I make an example for you:
import javax.swing.*;
public class Main {
private static JPanel panel1;
private static JButton button;
public static void main(String[] args) {
JFrame frame = new JFrame( "Main");
panel1 = new JPanel();
button = new JButton("Button");
panel1.add(button);
frame.setContentPane(panel1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setTitle("Gui");
frame.setSize(640,320);
frame.setVisible(true);
}
}

Calling different screen thru button from a screen

Hello I would like to ask how can I call my Main menu screen from MainScreen? and kindly explain a little more details about Listener.
below is my prepared code:
public class MainScreen {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
JLabel WelcomeNote = new JLabel("Welcome");
panel.add(WelcomeNote);
JButton Start = new JButton("Start");
panel.add(Start);
//Insert action for Start button here
}
}
public class MainMenu {
public static void main(String[] args){
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
JLabel menuLbl = new JLabel("Main Menu");
panel.add(menuLbl);
}
}
What is wrong?
You cannot have two main methods in a single file in Java.
Program
Here is a demo program to change windows.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class First extends JFrame
{
JLabel jlb = new JLabel("Label in First Window");
JButton jb = new JButton("Next Window");
First()
{
super("First Windows");
//Set this frame
this.setSize(350,250);
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Setting size of components
jlb.setBounds(10,10,200,40);
jb.setBounds(10,120,150,40);
add(jlb);
add(jb);
jb.addActionListener((e)->{
this.setVisible(false);
new Second();
});
setVisible(true);
}
}
class Second extends JFrame implements ActionListener
{
JLabel jlb = new JLabel("Label in Second Window");
JButton jb = new JButton("Prev. Window");
Second()
{
super("Second Window");
this.setSize(350,250);
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Setting size of components
jlb.setBounds(10,10,200,40);
jb.setBounds(10,120,150,40);
add(jlb);
add(jb);
jb.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
this.setVisible(false);
new First();
}
}
class StartHere
{
public static void main(String[] args) {
Runnable r = ()->{
new First();
};
r.run();
}
}
Understanding the above program.
The StartHere class has a main method. It is just used for calling the first window you like. I could even call Second using new Second().
First and Second are similar codes.
Both of them have buttons. On each button (or JButton) I have added a method named addActionListner(this). This method fires up an ActionEvent which as you can see in Second class is captured by actionPerformed method. This method is declared in Functional Interface, ActionListener. The 'this' passed in Second class is you telling where the actionPerformed method is present in your code. The parameter is an ActionListener. Hence, you have to implement ActionListener for the class where you define actionPerformed.
Bonus
The First class doesn't seem to follow the norms described above. I passed a strange syntax. It is a new feature included in Java 8.
See this Oracle tutorial about Lambda Expressions.

Run new GUI window from an event of another class

I have 2 classes. Both implements runnable to create the GUI. The first one is the main, and the second one is the secondary class.
I want within the actionlistener of the main class to startup the secondary class.
Here is the code (the two classes are separated files):
public class Main implements Runnable
{
private JTextField txt1, txt2;
private JLabel lbl1, lbl2;
public void run()
{
JFrame frame = new JFrame("Secondary");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = frame.getContentPane();
JPanel background = new JPanel();
background.setLayout(new BoxLayout(background, BoxLayout.LINE_AXIS));
.........
// Horizontally adding the textbox and button in a Box
Box box = new Box(BoxLayout.Y_AXIS);
......
background.add(box);
pane.add(background);
frame.pack();
frame.setVisible(true);
}
private class SListener implements ActionListener
{
public void actionPerformed(ActionEvent a)
{
Secondary s = new Secondary();
}
}
public static void main (String[] args)
{
Main gui = new Main();
SwingUtilities.invokeLater(gui);
}
}
public class Secondary implements Runnable
{
private JTextField txt1, txt2;
private JLabel lbl1, lbl2;
public Secondary()
{
Secondary gui = new Secondary();
SwingUtilities.invokeLater(gui);
}
public void run()
{
JFrame frame = new JFrame("Secondary");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = frame.getContentPane();
JPanel background = new JPanel();
background.setLayout(new BoxLayout(background, BoxLayout.LINE_AXIS));
.........
// Horizontally adding the textbox and button in a Box
Box box = new Box(BoxLayout.Y_AXIS);
......
background.add(box);
pane.add(background);
frame.pack();
frame.setVisible(true);
}
}
I want to keep the code in two files, I don't want to mixed the two classes in one file.
As you can see from the code, in the Secondary class, in it's constructor I create an Instance of the Secondary class and I run the gui so that when the Instance of this class is created in the Main class, to run the gui.
Unfortunately this technique is not working.
Any ideas?
Thanks
The following line are complety wrong:
public Secondary(){
Secondary gui = new Secondary();
SwingUtilities.invokeLater(gui);
}
Each time you call new Secondary() somewhere in your code, the above code will be triggered, which in turn calls new Secondary() again, and again, and again, ... and your program is blocked.
You probably want to replace it either by
public Secondary(){
SwingUtilities.invokeLater(this);
}
which will avoid the loop, but this is weird behaviour for a constructor.
It makes much more sense to switch to an empty constructor (or delete it all together)
public Secondary(){
}
and rewrite your listener to
public void actionPerformed(ActionEvent a){
Secondary s = new Secondary();
SwingUtilities.invokeLater( s );
}
I would recommend that you completely re-design your program. I find that it is most helpful to gear my GUI's towards creation of JPanels, not top level windows such as JFrame, which can then be placed into JFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, wherever needed. I find that this greatly increase the flexibility of my GUI coding, and is exactly what I suggest that you do. So...
Your first class creates a JPanel that is then placed into a JFrame.
In the first class's ActionListener, create an instance of the 2nd class, place it into a JDialog (not a JFrame), and then display it.
For example,
import java.awt.Component;
import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class TwoWindowEg {
public TwoWindowEg() {
// TODO Auto-generated constructor stub
}
private static void createAndShowGui() {
GuiPanel1 mainPanel = new GuiPanel1();
JFrame frame = new JFrame("Main GUI");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class GuiPanel1 extends JPanel {
private static final int PREF_W = 800;
private static final int PREF_H = 650;
private GuiPanel2 guiPanel2 = new GuiPanel2(); // our second class!
private JDialog dialog = null; // our JDialog
public GuiPanel1() {
setBorder(BorderFactory.createTitledBorder("GUI Panel 1"));
add(new JButton(new LaunchNewWindowAction("Launch New Window")));
add(new JButton(new DisposeAction("Exit", KeyEvent.VK_X)));
}
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private class LaunchNewWindowAction extends AbstractAction {
public LaunchNewWindowAction(String name) {
super(name);
}
#Override
public void actionPerformed(ActionEvent e) {
if (dialog == null) {
// get the Window that holds this JPanel
Window win = SwingUtilities.getWindowAncestor(GuiPanel1.this);
dialog = new JDialog(win, "Second Window", ModalityType.APPLICATION_MODAL);
dialog.add(guiPanel2);
dialog.pack();
}
dialog.setVisible(true);
}
}
}
class GuiPanel2 extends JPanel {
public GuiPanel2() {
setBorder(BorderFactory.createTitledBorder("GUI Panel 1"));
add(new JLabel("The second JPanel/Class"));
add(new JButton(new DisposeAction("Exit", KeyEvent.VK_X)));
}
}
class DisposeAction extends AbstractAction {
public DisposeAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
#Override
public void actionPerformed(ActionEvent e) {
Component comp = (Component) e.getSource();
Window win = SwingUtilities.getWindowAncestor(comp);
win.dispose();
}
}
Alternatively, you could swap JPanel "views" using a CardLayout, but either way, you will want to avoid showing two JFrames. Please have a look at The Use of Multiple JFrames, Good/Bad Practice?.

Custom component creation in intellij fails

I just want to set custom list model for ComboBox. The commented code also didn't worked. I completely don't know why!
I'm working in Intellij Community Edition under JDK 1.8
import jssc.SerialPortList;
import javax.swing.*;
import java.awt.*;
public class sampleForm extends JFrame {
private JComboBox comboBox1;
private JPanel panel1;
/*public sampleForm() {
super("title");
String[] portNames = SerialPortList.getPortNames();
setLayout(new FlowLayout());
comboBox1 = new JComboBox(portNames);
add(comboBox1);
}*/
public static void main(String[] args) {
JFrame frame = new JFrame("sampleForm");
frame.setContentPane(new sampleForm().panel1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
private void createUIComponents() {
// TODO: place custom component creation code here
String[] portNames = SerialPortList.getPortNames();
comboBox1 = new JComboBox(port);
}
}
Your class is already a JFrame, but you create another JFrame instance in the main method. So whatever you added in the sampleForm constructor has no affect visually because the sampleForm instance isn't the frame your showing. You're showing the new JFrame instance. So instead, do
public sampleForm() {
super("title");
String[] portNames = SerialPortList.getPortNames();
comboBox1 = new JComboBox(portNames);
panel1 = new JPanel(); // default FlowLayout
panel.add(comboBox1);
setContentPane(panel1);
}
public static void main(String[] args) {
sampleForm frame = new sampleForm("sampleForm");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
Also, see Initial Threads. Swing apps should be run on the Event Dispatch Thread.

JButton changes size

I have a JFrame, and whenever I switch from one JFrame using a JButton it starts out normally, but whenever I create a new instance of the first JFrame, the JButton is in an incorrect location and is the wrong size.
Example on startup
and when another one is created
Code:
public class Menu extends JFrame implements Runnable {
private static final long serialVersionUID = 1L;
public static int Number_of_Participants = 0;
protected JPanel window = new JPanel();
double p;
private JButton Participants;
private Rectangle rParticipants;
protected int Button_width = 240;
protected int Button_height = 48;
boolean running = false;
Thread thread;
JFrame frame = new JFrame();
public Menu() {
window.setBackground(Color.BLUE);
frame.setSize(new Dimension(800, 600));
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.getContentPane().add(window);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Image image = null;
try {
image = ImageIO.read(new File("res/BG.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
generateFiles();
drawButtons();
startMenu();
frame.repaint();
}
public void drawButtons() {
rParticipants = new Rectangle(520, 12, Button_width, Button_height);
Participants = new JButton("A");
Participants.setBounds(rParticipants);
window.add(Participants);
Participants.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
new Participant(Number_of_Participants);
}
});
}
}
Participant.java extends Menu.java
int Participant_ID;
public Participant(int Participant_ID) {
super();
this.Participant_ID = Participant_ID;
}
makes a JButton that goes back to Menu.java
As mentioned in the comment, your problem is most likely related to the call to setVisible(true). This should always be the LAST call in the constructor. Particularly, it should only be called AFTER all components have been added to the frame.
Apart from that, from the code that you posted, it seems like you want to switch through a seqence of frames, starting with a "main" menu, and then going through one frame for each "Participant". This intention could already be considered as questionable, because closing and disposing a JFrame just in order to create a new one does not seem to be very elegant. Most likely, a more elegant solution would be possible with a CardLayout : http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html
However, some general hints:
Create the GUI on the Event Dispatch Thread
Don't extend JFrame. Instead, create a JFrame and fill it as needed
Don't implement Runnable with your top level class
Obey the standardJavaNamingConventions!
Don't try to do manual layouts with setBounds
This code is still not "beautiful", but at least shows how the goal of switching through several frames might be achieved, taking into account these points
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class MenuExample
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
JPanel mainMenuPanel = new MainMenuPanel();
createAndShowFrame(mainMenuPanel);
}
});
}
static void createAndShowFrame(JPanel panel)
{
JFrame frame = new JFrame();
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(800, 600));
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
static JButton createNextParticipantButton(
final JComponent container, final int nextID)
{
JButton nextParticipantButton = new JButton("New Participant");
nextParticipantButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
Window window =
SwingUtilities.getWindowAncestor(container);
window.dispose();
ParticipantPanel participantPanel =
new ParticipantPanel(nextID);
createAndShowFrame(participantPanel);
}
});
return nextParticipantButton;
}
}
class MainMenuPanel extends JPanel
{
public MainMenuPanel()
{
setBackground(Color.BLUE);
add(MenuExample.createNextParticipantButton(this, 0));
}
}
class ParticipantPanel extends JPanel
{
private final int participantID;
public ParticipantPanel(int participantID)
{
this.participantID = participantID;
add(new JLabel("Add the contents for participant "+participantID));
add(MenuExample.createNextParticipantButton(this, participantID+1));
}
}

Categories

Resources