I have a class that contains the main GUI window that my program will display
/**
* GUI program to run a coffee/bagel shoppe
* #author Nick Gilbert
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CoffeeShop extends JPanel {
private final int WINDOW_WIDTH = 400; // Window width
private final int WINDOW_HEIGHT = 300; // Window height
private JFrame mainFrame;
public CoffeeShop()
{
//Setting up mainframe configurations
mainFrame = new JFrame();
mainFrame.setTitle("Order Entry Screen!");
mainFrame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setLayout(new BorderLayout());
//Piecing together GUI window
mainFrame.add(new TitleRegister(), BorderLayout.NORTH);
mainFrame.setVisible(true);
}
public static void main(String[] args) {
new CoffeeShop();
}
}
As you can see, I'm trying to add to mainFrame a JPanel that is actually a class I wrote which extends JPanel
/**
* Sets title at top of register
* #author Nick Gilbert
*/
import javax.swing.*;
import java.awt.*;
import java.awt.Event.*;
public class TitleRegister extends JPanel {
private JPanel titlePanel;
private JLabel titleLabel;
public TitleRegister() {
titlePanel = new JPanel();
titleLabel = new JLabel("Order Entry Screen", SwingConstants.CENTER);
titlePanel.add(titleLabel);
titlePanel.setVisible(true);
}
}
Yet when I do this the instance of TitleRegister does not show up. I have setVisible set to true for everything so it should be showing up.
You've not actually added anything to the TitleRegister pane...
Without knowing more, you could simply getaway with...
public class TitleRegister extends JPanel {
private JLabel titleLabel;
public TitleRegister() {
titleLabel = new JLabel("Order Entry Screen", SwingConstants.CENTER);
add(titleLabel);
}
}
In fact, you could simply get away with adding the JLabel to the mainFrame.
Notes:
There's no need to CoffeeShop to extend JPanel, you're not adding anything to, your constructor builds a JFrame and adds the UI to that...
Rely on pack over setSize, it will produce a more reliable output, ensuring that the content area has the space it needs to be properly displayed.
Make sure you are creating and modifying your UI's only from the context of the Event Dispatching Thread. See Initial Threads for more details...
Related
I have been practicing my code with Java Swing and have gotten decent on controlling where to place some items, such as labels and or buttons, but I was wondering if you can do the same with classes? I have just a simple class with enough code to put a button in it and that's it, that I am trying to create an instance of the class and then control for to put on the left and right side but when I do, all it does is create two separate windows with the button in the middle and that's it. Am I doing something wrong, or can you not do classes the same way?
The code:
import java.awt.Color;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Fun extends JFrame
{
private final int WIDTH = 500;
private final int HEIGHT = 400;
public Fun()
{
setTitle("Fun Management");
setSize(WIDTH, HEIGHT);
BuildPanel west = new BuildPanel(); /// BuildPanel is the name of the class that has just a button in it.
BuildPanel east = new BuildPanel(); ///
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(west, BorderLayout.WEST); /// I am doing the same thing with the instances as I would with buttons or labesl
add(east, BorderLayout.EAST);
setVisible(true);
}
public static void main(String[] args)
{
new Fun();
}
}
I took your code and created the following GUI.
Oracle has a rad tutorial, Creating a GUI With Swing, that will show you how to create Swing GUIs. Skip the Netbeans section.
Always start your Swing application with a call to the SwingUtilities invokeLater method. This method ensures that your Swing components are created and executed on the Event Dispatch Thread.
Use Swing components. Don't extend a Swing component unless you want to override one or more of the component methods.
The JFrame methods must be called in a specific order. This is the order I recommend for most Swing applications. Use the JFrame pack method and let the components size the JFrame.
I created a BuildPanel class to build a JPanel. There are good reasons to do this, but be careful. You have to manage each instance of the class you create. As an example, what if you want the text of the two buttons to be different? What if you want to assign two different ActionListener classes, one to each button?
Here's the complete runnable code. I made the BuildPanel class an inner class so I can post the code as one block.
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TwoPanelExample implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new TwoPanelExample());
}
#Override
public void run() {
JFrame frame = new JFrame("Fun Management");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BuildPanel west = new BuildPanel();
BuildPanel east = new BuildPanel();
frame.add(west.getPanel(), BorderLayout.WEST);
frame.add(east.getPanel(), BorderLayout.EAST);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public class BuildPanel {
private final JPanel panel;
public BuildPanel() {
this.panel = createMainPanel();
}
private JPanel createMainPanel() {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(5, 30, 5, 30));
JButton button = new JButton("Click Me");
panel.add(button);
return panel;
}
public JPanel getPanel() {
return panel;
}
}
}
As of late I've been developing a (very) small GUI application in Java. I'm extremely new to Swing and Java in general, but up until now I have been able to get everything to work the way I want it to. However, after cleaning up my code, when I run the program nothing but the border of the window appears. What am I doing wrong and how can I fix my code? Thanks ahead of time!
For the sake of saving space I've made Pastebin links to all of my classes (besides Main).
Main Class
package me.n3rdfall.ezserver.main;
public class Main {
public static GUI g = new GUI();
public static void main(String[] args) {
g.showWindow(800, 500);
}
}
GUI Class
http://pastebin.com/gDMipdp1
ButtonListener Class
http://pastebin.com/4XXm70AD
EDIT: It appears that calling removeAll() directly on 'frame' actually removed essential things other than what I had added. By calling removeAll() on getContentPane(), the issue was resolved.
Quick hack: Remove the removeAll() functions.
public void homePage() {
// frame.removeAll();
// mainpanel.removeAll();
// topbar.removeAll();
I'm not sure what you're trying to achieve, but that will at least show some items. If I were you I would rebuild this GUI by extending JFrame. It will make your code a little easier to read.
I also think what you are trying to achieve with the buttons is to switch layouts, you can do this in an easier way by using CardLayout
Example (has nothing to do with your code, but to demonstrate):
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Example extends JFrame implements ActionListener {
private JButton leftButton;
private JButton rightButton;
private CardLayout cardLayout = new CardLayout();
JPanel cards = new JPanel(cardLayout);
final static String LEFTPANEL = "LEFTPANEL";
final static String RIGHTPANEL = "RIGHTPANEL";
JPanel card1;
JPanel card2;
public Example() {
JPanel topPanel = new JPanel();
addButtons(topPanel);
add(topPanel, BorderLayout.NORTH);
add(cards, BorderLayout.CENTER);
//Initiates the card panels
initCards();
setTitle("My Window");
setSize(300, 300);
setLocationRelativeTo(null);
setVisible(true);
}
private void initCards() {
card1 = new JPanel();
card2 = new JPanel();
card1.setBackground(Color.black);
card2.setBackground(Color.red);
cards.add(card1, LEFTPANEL);
cards.add(card2, RIGHTPANEL);
}
private void addButtons(Container con) {
leftButton = new JButton("Left Button");
leftButton.addActionListener(this);
rightButton = new JButton("Right Button");
rightButton.addActionListener(this);
con.add(leftButton, BorderLayout.WEST);
con.add(rightButton, BorderLayout.EAST);
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(leftButton)) {
//Change cardlayout
cardLayout.show(cards, LEFTPANEL);
} else if(e.getSource().equals(rightButton)) {
//Change cardlayout
cardLayout.show(cards, RIGHTPANEL);
}
}
public static void main(String[] args) {
new Example();
}
}
I am attempting to learn more about creating more dynamic GUI's. I am hoping to add different panels with different content and as you press buttons on one main panel, it changes the adjacent panels. I have added two panels and some buttons and when I test the program, it displays correctly. The problem is when I add a JTextField (or JTextArea) the panels are blank and there are no buttons. The strange thing is I haven't added the JTextField to either panel. I have only created a global variable. If I comment it out, the program runs correctly. Am I missing something very simple?
Here is the gameWindow class that has the JTextField
package rpgcreator;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
class gameWindow extends JPanel {
JPanel startWindowPanel;
JPanel settingsPanel;
JPanel characterPanel;
JPanel scenarioPanel;
JPanel mapPanel;
JButton CharacterButton = new JButton("Create your character");
JButton StoryButton = new JButton("Choose your Story line");
JButton MapButton = new JButton("Choose your World");
//JTextField nameField = new JTextField(15); //comment or uncomment to see issue
public gameWindow() {
setLayout(new GridLayout(0,2,5,0));
startWindowPanel = new JPanel(new FlowLayout());
settingsPanel = new JPanel(new GridLayout(2,1));
startWindowPanel.setBackground(Color.blue);
settingsPanel.setBackground(Color.black);
startWindowPanel.add(MapButton);
startWindowPanel.add(StoryButton);
startWindowPanel.add(CharacterButton);
add(startWindowPanel);
add(settingsPanel);
}
}
Here is main
package rpgcreator;
import javax.swing.JFrame;
public class RPGCreator extends JFrame{
private static void mainWindow(){
RPGCreator mainwindow = new RPGCreator();
mainwindow.setSize(1200, 800);
mainwindow.setResizable(false);
mainwindow.setLocationRelativeTo(null);
mainwindow.setTitle("RPG Creator");
mainwindow.setVisible(true);
mainwindow.add(new gameWindow());
mainwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO code application logic here
mainWindow();
}
}
setVisible should go at the end. You're currently setting visible to true, and then adding a panel.
mainwindow.setVisible(true);
mainwindow.add(new gameWindow());
Put setVisible at the end after setDeaultCLoseOperation
I'm not entirely sure why it does it, maybe someone else can explain.
What I do know, is I usually call pack() which seems to make your problem go away.
private static void mainWindow(){
final RPGCreator mainwindow = new RPGCreator();
mainwindow.setMinimumSize(new Dimension(1200, 800));
mainwindow.setResizable(false);
mainwindow.setTitle("RPG Creator");
mainwindow.setVisible(true);
mainwindow.add(new gameWindow());
mainwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainwindow.pack(); //This usually goes after you've added all of your components
mainwindow.setLocationRelativeTo(null);
}
Some notes:
I had to change to mainwindow.setMinimumSize(new Dimension(1200, 800)); to avoid the frame looking squashed. Although I would usually let the layout manager deal with the sizes of things.
Call setLocationRelativeTo(null) after you call pack() so that it has the desired effect. Again not sure why, but I've learnt that through some hardship.
In trying to run my basic GUI application from Main I have somehow managed to first make the GUI show up (but it was smaller than what I set the size to within the code and not showing any components) then magically (after adding pack() and setlocationrelativeto(null)) it does not pop up at all. I am using Netbeans (if that helps), in Main it gives me a tooltip that my GUI is "never used" so it runs and outputs "Finished building" rather than continuing to run and showing the GUI. I have provided 2 sets of code (1) main method and (2) GUI class. Please let me know if I'm being confusing as of course it makes sense in my head but may be communicated badly. I have not included the complete code but if it is necessary please let me know and I will do so.
package logTime;
public class LogInTime {
public static void main(String[] args) {
try{
LogAppFrame app = new LogAppFrame(); //IDE gives tooltip that app is unused
}
catch(Exception e){
System.err.println("\n\nError Occurred: "); //am going to print message later
}
}
}
The actual GUI code - does not include imports or actionlisteners:
public void LogAppFrame(){
frame = new JFrame("Time Log Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cl = new CardLayout();
frame.getContentPane().setLayout(cl);
//frame.setLayout(cl);
frame.setSize(new Dimension(375,385));
logNewFrame = new JPanel();
logNewFrame.setLayout(new GridLayout(5,1));
logNewFrame.setBorder(new EmptyBorder(20,20,20,20));
frame.getContentPane().add(logNewFrame, "logNewFrame");
historyFrame = new JPanel();
historyFrame.setLayout(new GridLayout(2,1)); //given 0 for rows to add numerous rows
historyFrame.setBorder(new EmptyBorder(20,20,20,20));
frame.getContentPane().add(historyFrame, "historyFrame");
.
.
.
//added lots of components but will not include code as there is no error within this portion of code - i used to have both Main and LogAppFrame class all together and my GUI worked and showed components but I felt it may be best practice not to do it this way and cardlayout wasnt working
.
.
.
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Adding SSCE below:
package logTime;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.event.*;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class LogAppFrame{
private static JFrame frame;
private static CardLayout cl;
//menu option components
/**
* Help option: How To Use/Read Me - explains how to use it 8 Hour Day -
* shows the arrival and leave time based on lunch type
*/
/**
* Log New Date option: shows screen to input new values into date
*/
/**
* View Past Dates option: shows all past dates since forever - may add
* month tabs later
*/
/**
* Edit Past Date option: doesnt exist yet but will be added to View Past
* menu option as side button to edit any old date
*/
private static JMenuBar menuBar = new JMenuBar();
private static JMenu help;
private static JMenuItem logNewDate;
private static JMenuItem viewPastDates;
private static JMenuItem workDay;
private static JMenuItem about;
//Log New Date components
/**
* 4 labels, 1 button, 1 calendar, 2 dropdowns, 2 textfields, 5x2 gridlayout
*/
private static JLabel dateToday;
private static JLabel timeInToday;
private static JLabel timeOutToday;
private static JLabel lunchTypeToday;
private static JLabel timeColon1;
private static JLabel timeColon2;
private static JButton saveButton;
private static JComboBox month;
private static JComboBox day;
private static JComboBox year;
private static JComboBox amPm1;
private static JComboBox amPm2;
private static JComboBox hrTimeIn;
private static JComboBox hrTimeOut;
private static JComboBox minTimeIn;
private static JComboBox minTimeOut;
private static JPanel dateTodayPanel;
private static JPanel timeInPanel;
private static JPanel timeOutPanel;
private static JPanel lunchTypePanel;
private static JPanel saveButtonPanel;
private static JComboBox lunchType;
//View Past Dates components
/**
* 4x*infinitiy* gridlayout or have a flowlayout, 4 labels
*/
private static JLabel pastDates;
private static JLabel pastTimeIns;
private static JLabel pastTimeOuts;
private static JLabel pastLunchTypes;
private static JPanel headers; //holds header labels
private static JPanel oldLogs; //will hold all past log panels
//Frames to hold the logNew and viewOld views
private static JPanel logNewFrame;
private static JPanel historyFrame;
public void LogAppFrame(){
frame = new JFrame("Time Log Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cl = new CardLayout();
frame.getContentPane().setLayout(cl);
//frame.setLayout(cl);
frame.setSize(new Dimension(375,385));
logNewFrame = new JPanel();
logNewFrame.setLayout(new GridLayout(5,1));
logNewFrame.setBorder(new EmptyBorder(20,20,20,20));
frame.getContentPane().add(logNewFrame, "logNewFrame");
historyFrame = new JPanel();
historyFrame.setLayout(new GridLayout(2,1)); //given 0 for rows to add numerous rows
historyFrame.setBorder(new EmptyBorder(20,20,20,20));
frame.getContentPane().add(historyFrame, "historyFrame");
//Menu components
menuBar = new JMenuBar();
help = new JMenu("Help");
logNewDate = new JMenuItem("Log New Date");
viewPastDates = new JMenuItem("View Past Dates");
workDay = new JMenuItem("8 Hour Day");
about = new JMenuItem("How To ...");
help.add(workDay);
help.add(about);
menuBar.add(logNewDate);
menuBar.add(viewPastDates);
menuBar.add(help);
frame.setJMenuBar(menuBar);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
You have written void before the thing that ought to be a constructor. So it is mistaken as a method by the compiler which never gets called. Unfortunately the compiler generates a no-op constructor for you in such a case. Just remove the void keyword.
And by the way, remove all the nasty static keywords from the fields. That hurts.
If you want to write simple gui applications you should check WindowBuilder. It's drag and drop gui for java that actually works.
I'm having trouble adding a JPanel class I created to my container class. To be honest, I'm not sure if this is even the best method to go about creating my menu. I've asked professors, and TA's and they all tell me different things.
EDIT: To clarify, the problem is that the main menu made in the JPanel class isn't shown. I think I added it properly within the container class, but I'm not entirely sure.
This is my JPanel class that I'm trying to add to the container:
package.model;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
#SuppressWarnings("serial")
public class MainMenu extends JPanel {
private static MainMenu instance;
private JPanel mainMenu;
private JButton btnSinglePlayer, btnMultiPlayer, btnScoreBoard, btnQuit;
private MainMenu() {
mainMenu();
singlePlayer();
multiPlayer();
scoreBoard();
quit();
}
public static MainMenu getMenuInstance() {
if (instance == null) {
instance = new MainMenu();
}
return instance;
}
public void construct() {
mainMenu();
singlePlayer();
multiPlayer();
scoreBoard();
quit();
}
private JPanel mainMenu() {
mainMenu = new JPanel();
mainMenu.setLayout(null);
return mainMenu;
}
private JButton singlePlayer() {
btnSinglePlayer = new JButton("SinglePlayer");
btnSinglePlayer.setBounds(365, 210, 170, 55);
mainMenu.add(btnSinglePlayer);
return btnSinglePlayer;
}
private JButton multiPlayer() {
btnMultiPlayer = new JButton("MultiPlayer");
btnMultiPlayer.setBounds(365, 300, 170, 55);
mainMenu.add(btnMultiPlayer);
return btnMultiPlayer;
}
private JButton scoreBoard() {
btnScoreBoard = new JButton("ScoreBoards");
btnScoreBoard.setBounds(365, 411, 170, 55);
mainMenu.add(btnScoreBoard);
return btnScoreBoard;
}
private JButton quit() {
btnQuit = new JButton("Quit");
btnQuit.setBounds(365, 500, 170, 55);
mainMenu.add(btnQuit);
return btnQuit;
}
}
And here is my container class:
package view;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import model.MainMenu;
#SuppressWarnings("serial")
public class MainFrame extends JFrame {
private static JFrame mainContainer = new JFrame("Checkers");
private JPanel card = new JPanel(new CardLayout());
private CardLayout cardLayout = (CardLayout) (card.getLayout());
private JPanel homeCard, singlePlayerCard, multiPlayerCard, scoreBoardCard;
private JPanel mainMenu = MainMenu.getMenuInstance();
public MainFrame() {
}
private void addComponentToPane(Container pane) {
//mainMenu.construct();
homeCard = new JPanel();
singlePlayerCard = new JPanel();
multiPlayerCard = new JPanel();
scoreBoardCard = new JPanel();
homeCard.add(mainMenu);
card.add(homeCard, "homeCard");
cardLayout.show(card, "homeCard");
pane.add(card);
}
public static void createAndShowGUI() {
MainFrame frame = new MainFrame();
frame.addComponentToPane(mainContainer.getContentPane());
mainContainer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainContainer.pack();
mainContainer.setSize(920, 650);
mainContainer.setVisible(true);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
mainContainer.setLocation(dim.width / 2 - mainContainer.getSize().width
/ 2, dim.height / 2 - mainContainer.getSize().height / 2);
}
}
The reason I want to have a JPanel class, and I'll have more than one btw, is because I want that class to launch my game thread. And I don't wish to launch the game thread through the view. If this is a bad design please let me know.
I do btw, have a launcher class, which I think is irrelevant to post. I know I did it right though, the error which I and abut 3-4 TA's at my university can't seem to find.
JFrame has a few side-effects that exist outside of the Java program space, like the frame decorators that the operating system provides (usually edge handles, close buttons, and os driven drop down menus to hide / show / etc.)
As such, typically you cannot use classical composition and inheritance for driving JFrames at the same time.
If you inherit, you can call the class's own getContentPane() to get the container which then can be used such as
getContentPane().add(jpanel);
If you perfer composition, then you basically do the same thing, but with your member variable holding the JFrame.
frame.getContentPane().add(jpanel);
Note that unlike many containers, JFrame only supports adding one item. So, if you want to add multiple items, you need to add a JPanel, and the add the interior items to the JPanel.
private JPanel mainMenu = MainMenu.getMenuInstance(); in MainFrame gets the MainMenu instance variable from the MainMenu Class.
The constructor or construct() method in MainMenu add components to the mainMenu JPanel in MainMenu which is never referred to again.
Also, it is bad practice to set a JPanel's layout to null as you did in MainMenu's mainMenu() method. It sets the size of the JPanel component to 0x0 pixels and you won't see the components inside of the JPanel when you add them.
I could mention another thing or two about refactoring your code properly into methods and ask questions about how the rest of the project works, but this post is 8 years old at the time of this writing. I'm writing these statements to point people in the right direction if they are still curious about why the code doesn't work.