Reputation: 117
I'm progressing through java and have started a little 'catch-all' project to being piecing it all together, and I get hung up on an issue almost out of the gate.
The application basically, at the moment, is simply supposed to open a GUI composed of three tabs, each with a different content; one for designing a table top campaign, one for running it, and one for database management.
I believed I had gotten all the components added correctly, but when I run it to test I get an empty, unsized application pane. Basically just the resize buttons and a line. I can resize it, but although it shows the menu bar, we get no panes, no tabs, no nuthin. I've been kajiggering one thing at a time to get them to display and even rebuilt it from the gui builder in netbeans and remodelled it based on that generated code (I have some custom components to include and want to learn to manually code GUIs).
Can anyone spot what I'm doing wrong here?
package com.bmagus.tabletop.gui;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class GMGui extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public GMGui() {
initComponents();
lookAndFeel();
}
//Component variables
private JCheckBox dbSearchMod1;
private JComboBox pcSelector;
private JComboBox npcSelector;
private JMenu fileMenu;
private JMenu editMenu;
private JMenu helpMenu;
private JMenuBar menuBar;
private JMenuItem fileExit;
private JPanel designPanel;
private JPanel runPanel;
private JPanel dbPanel;
private JScrollPane designNotesScroll;
private JScrollPane runNotesScroll;
private JTabbedPane gmGuiTabs;
private JTextArea designNotes;
private JTextArea runNotes;
private JTextField dbSearchField;
private void setGbc(GridBagConstraints localGbc, int x, int y, int height, int width, double xweight, double yweight){
localGbc.gridx=x;
localGbc.gridy=y;
localGbc.gridheight=height;
localGbc.gridwidth=width;
localGbc.fill = GridBagConstraints.BOTH;
localGbc.weightx=xweight;
localGbc.weighty=yweight;
localGbc.insets = new Insets(20, 20, 20, 20);
}
private void lookAndFeel()
{
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(GMGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(GMGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(GMGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(GMGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
}
private void initComponents() {
GridBagConstraints gbc = new GridBagConstraints();
GridBagLayout guiLayout = new GridBagLayout();
gmGuiTabs = new JTabbedPane();
designPanel = new JPanel();
designNotesScroll = new JScrollPane();
designNotes = new JTextArea();
runPanel = new JPanel();
runNotesScroll = new JScrollPane();
runNotes = new JTextArea();
pcSelector = new JComboBox();
npcSelector = new JComboBox();
dbPanel = new JPanel();
dbSearchField = new JTextField();
dbSearchMod1 = new JCheckBox();
menuBar = new JMenuBar();
fileMenu = new JMenu();
fileExit = new JMenuItem();
editMenu = new JMenu();
helpMenu = new JMenu();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//Campaign Design Tab
designPanel.setLayout(guiLayout);
designNotes.setColumns(20);
designNotes.setRows(5);
designNotesScroll.setViewportView(designNotes);
setGbc(gbc, 0,0,5,3,1.0d,1.0d);
gmGuiTabs.addTab("Campaign Design", designPanel);
//Run Game tab
runPanel.setLayout(guiLayout);
runNotes.setColumns(20);
runNotes.setRows(5);
runNotesScroll.setViewportView(runNotes);
setGbc(gbc, 0,0,5,3,1.0d,1.0d);//Todo
runPanel.add(runNotesScroll, gbc);
pcSelector.setModel(new DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
setGbc(gbc,6,0,1,1,0.0d,0.0d);//Todo
runPanel.add(pcSelector, gbc);
npcSelector.setModel(new DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
setGbc(gbc,0,1,1,1,0d,0d);//todo
runPanel.add(npcSelector, gbc);
gmGuiTabs.addTab("Run Game", runPanel);
//Database Tab
dbPanel.setLayout(guiLayout);
dbSearchField.setText("jTextField1");
setGbc(gbc,0,0,3,1,0d,0d);//Todo
dbPanel.add(dbSearchField, gbc);
dbSearchMod1.setText("jCheckBox1");
setGbc(gbc,1,0,1,1,0d,0d);//Todo
dbPanel.add(dbSearchMod1, gbc);
gmGuiTabs.addTab("Database", dbPanel);
//MenuBar Elements
fileMenu.setText("File");
fileExit.setText("jMenuItem1");
fileMenu.add(fileExit);
menuBar.add(fileMenu);
editMenu.setText("Edit");
menuBar.add(editMenu);
helpMenu.setText("Help");
menuBar.add(helpMenu);
setJMenuBar(menuBar);
}
}
I don't think it's in the main method, but here's what I've got calling and running the GUI.
package com.bmagus.tabletop;
import java.awt.EventQueue;
import com.bmagus.tabletop.gui.GMGui;
public class Game {
public static void main(String[] args){
EventQueue.invokeLater(new Runnable() {
@Override
public void run(){
GMGui gmgui = new GMGui();
gmgui.setVisible(true);
}
});
}
}
Upvotes: 2
Views: 93
Reputation: 608
You don't add anything to your frame. Therefore you get an empty one. So call at the end of your initComponents
method the add
method to add your components which you created before to the frame:
private void initComponents()
{
/* ... */
setJMenuBar(menuBar);
add(gmGuiTabs);
pack();
}
This will show for example your tabbed pane.
Upvotes: 2