I am trying to make a simple calculator to practice Graphics (i am a complete GUI noob). I am having some problems with having unneeded spaces after Polyashenkos Calulator and the text area and the space between the text area and the buttons. Also how do i keep that layout but eliminate the space and also make the bottom 3 buttons smaller. Any tips about what im doing or how i can do it better would be much appreciated. Thank you.
import javax.swing.*;
import java.awt.*;
public class calculator {
public static void main(String[] args) {
// creates the JFrame(a window with decorations)
JFrame frame = new JFrame("Calculator");
// stops the program when window is closed
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(377, 350);
// the main panel of the JFrame,
// remembet you cant add content directly to JFrame
JPanel content = new JPanel(new GridLayout(4, 0));
// panel for the text field
JPanel textarea = new JPanel(new GridLayout(4, 0));
// panel for the buttons,
// GridLayout(int rows, int cols, int horiz_gap, int vert_gap)
JPanel buttonarea = new JPanel(new GridLayout(4, 5, 2, 2));
// the panel for the bigger bottom buttons
JPanel secondbuttonarea = new JPanel(new GridLayout(1, 1, 2, 2));
// the panel for the text on top
JPanel label = new JPanel();
content.add(label);
content.add(textarea);
content.add(buttonarea);
content.add(secondbuttonarea);
JLabel words = new JLabel("Polyashenko's Calculator", JLabel.CENTER);
label.add(words);
JTextField enterhere = new JTextField("0.", JTextField.CENTER);
// will set the curser of the text bar on right side
enterhere.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
textarea.add(enterhere);
// makes a button called b1 with text in it
JButton b1 = new JButton("BkSP");
// adds the backspace button to the buttonarea panel
buttonarea.add(b1);
JButton b2 = new JButton("CE");
buttonarea.add(b2);
JButton b3 = new JButton("C");
buttonarea.add(b3);
JButton b4 = new JButton("/");
buttonarea.add(b4);
JButton b5 = new JButton("sqrt");
buttonarea.add(b5);
JButton b6 = new JButton("7");
buttonarea.add(b6);
JButton b7 = new JButton("8");
buttonarea.add(b7);
JButton b8 = new JButton("9");
buttonarea.add(b8);
JButton b9 = new JButton("*");
buttonarea.add(b9);
JButton b10 = new JButton("%");
buttonarea.add(b10);
JButton b11 = new JButton("4");
buttonarea.add(b11);
JButton b12 = new JButton("5");
buttonarea.add(b12);
JButton b13 = new JButton("6");
buttonarea.add(b13);
JButton b14 = new JButton("-");
buttonarea.add(b14);
JButton b15 = new JButton("1/x");
buttonarea.add(b15);
JButton b16 = new JButton("1");
buttonarea.add(b16);
JButton b17 = new JButton("2");
buttonarea.add(b17);
JButton b18 = new JButton("3");
buttonarea.add(b18);
JButton b19 = new JButton("+");
buttonarea.add(b19);
JButton b20 = new JButton("+/-");
buttonarea.add(b20);
JButton b21 = new JButton("0");
secondbuttonarea.add(b21);
JButton b22 = new JButton(".");
secondbuttonarea.add(b22);
JButton b23 = new JButton("=");
secondbuttonarea.add(b23);
// adds the buttonarea panel to the main panel
frame.getContentPane().add(content);
// makes the window visible, put at end of program
frame.setVisible(true);
}
}
one of lessons by Hovercraft Full Of Eels (-: forums.sun.com :-)
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.*;
import javax.swing.*;
public class SciCalc {
private static void createAndShowUI() {
SciCalcGui gui = new SciCalcGui();
SciCalcMenu menu = new SciCalcMenu(gui);
JFrame frame = new JFrame("Calculator");
frame.getContentPane().add(gui.getMainPanel());
frame.setJMenuBar(menu.getJMenuBar());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowUI();
}
});
}
private SciCalc() {
}
}
class SciCalcGui {
private static final String[][] STANDARD_BTN_TEXTS = {
{"7", "8", "9", "/"}, {"4", "5", "6", "*"},
{"1", "2", "3", "-"}, {"0", ".", "=", "+"}};
private static final String[][] SCIENTIFIC_BTN_TEXTS = {
{"sqrt", "1/x", "sin"}, {"%", "Exp", "cos"},
{"x^y", "ln", "tan"}, {"x^2", "n!", "sec"}};
private static final int GAP = 5;
private static final Font BTN_FONT = new Font(Font.DIALOG, Font.BOLD, 20);
private JPanel mainPanel = new JPanel();
private JPanel sciPanel;
private JTextField display = new JTextField();
SciCalcGui() {
display.setFont(BTN_FONT);
JPanel standardPanel = createBtnPanel(STANDARD_BTN_TEXTS, "Standard");
sciPanel = createBtnPanel(SCIENTIFIC_BTN_TEXTS, "Scientific");
mainPanel.setLayout(new BorderLayout());
mainPanel.setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
mainPanel.add(standardPanel, BorderLayout.CENTER);
mainPanel.add(sciPanel, BorderLayout.WEST);
mainPanel.add(display, BorderLayout.NORTH);
sciPanel.setVisible(false);
}
public void sciPanelSetVisible(boolean visible) {
sciPanel.setVisible(visible);
Window win = SwingUtilities.getWindowAncestor(mainPanel);
win.pack();
}
public JPanel getMainPanel() {
return mainPanel;
}
private JPanel createBtnPanel(String[][] texts, String title) {
JPanel btnPanel = new JPanel();
int rows = texts.length;
int cols = texts[0].length;
btnPanel.setLayout(new GridLayout(rows, cols, GAP, GAP));
for (int row = 0; row < texts.length; row++) {
for (int col = 0; col < texts[row].length; col++) {
JButton btn = new JButton(texts[row][col]);
btn.setFont(BTN_FONT);
btnPanel.add(btn);
}
}
btnPanel.setBorder(BorderFactory.createTitledBorder(title));
return btnPanel;
}
}
class SciCalcMenu {
private static final String STANDARD = "Standard";
private static final String SCIENTIFIC = "Scientific";
private SciCalcGui gui;
private JMenuBar menuBar = new JMenuBar();
private JMenuItem standardView;
private JMenuItem scientificView;
SciCalcMenu(SciCalcGui gui) {
this.gui = gui;
standardView = new JMenuItem(STANDARD, KeyEvent.VK_T);
scientificView = new JMenuItem(SCIENTIFIC, KeyEvent.VK_S);
ViewAction viewAction = new ViewAction();
standardView.addActionListener(viewAction);
scientificView.addActionListener(viewAction);
standardView.setEnabled(false);
JMenu viewMenu = new JMenu("View");
viewMenu.setMnemonic(KeyEvent.VK_V);
viewMenu.add(standardView);
viewMenu.add(scientificView);
menuBar.add(new JMenu("Edit"));
menuBar.add(viewMenu);
menuBar.add(new JMenu("Help"));
}
public JMenuBar getJMenuBar() {
return menuBar;
}
private class ViewAction implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals(STANDARD)) {
gui.sciPanelSetVisible(false);
standardView.setEnabled(false);
scientificView.setEnabled(true);
} else if (command.equals(SCIENTIFIC)) {
gui.sciPanelSetVisible(true);
standardView.setEnabled(true);
scientificView.setEnabled(false);
}
}
}
}
A GridLayout won't ever look very good in cases like this.
The content expands to fill the box in the grid. You only have minimal control over the spacing between rows and columns.
You may have to change layouts to make it look the way you want. GridBagLayout has all that control but is much harder to configure.
Sometimes you can nest panels with BorderLayout and GridLayout to make it look reasonable. But it is Swing and that means its usable but it becomes very hard to make it look slick.
I always like to use a FlowLayout for OK/Cancel buttons. They look best to me that way and you can push them all left, right or centered. Your calculator buttons should work well with a GridLayout but you can't easily have a tall "Enter" button or a wide "0" button.
For example, try using a vertical BoxLayout instead of a grid that is 4 high by 1 wide.
Related
As stated in the title i need to move the label for the text box to be above the box and not to the side. attached i have a picutres of what i mean. what i have vs what i want i have tried searching for it but i cannot seem to find the answer im looking for/not exactly sure what to look up. I have tried using JFrame but it made a separate window unless i need to make the entire GUI a JFrame for me to get the result i want?
Also the actionPerformed method has things but it is irrelevant to the question but displays correctly still.
import java.awt.event.\*;
import javax.swing.\*;
import java.text.DecimalFormat;
public class Project4 extends JFrame implements ActionListener {
private JTextArea taArea = new JTextArea("", 30, 20);
ButtonGroup group = new ButtonGroup();
JTextField name = new JTextField(20);
boolean ch = false;
boolean pep = false;
boolean sup = false;
boolean veg = false;
DecimalFormat df = new DecimalFormat("##.00");
double cost = 0.0;
public Project4() {
initUI();
}
public final void initUI() {
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
getContentPane().add(panel1, "North");
getContentPane().add(panel2, "West");
getContentPane().add(panel3, "Center");
getContentPane().add(panel4, "East");
panel4.setLayout(new BoxLayout(panel4, BoxLayout.Y_AXIS));
getContentPane().add(panel5, "South");
JButton button = new JButton("Place Order");
button.addActionListener(this);
panel5.add(button);
JButton button2 = new JButton("Clear");
button2.addActionListener(this);
panel5.add(button2);
panel3.add(taArea);
JCheckBox checkBox1 = new JCheckBox("Cheese Pizza") ;
checkBox1.addActionListener(this);
panel4.add(checkBox1);
JCheckBox checkBox2 = new JCheckBox("Pepperoni Pizza");
checkBox2.addActionListener(this);
panel4.add(checkBox2);
JCheckBox checkBox3 = new JCheckBox("Supreme Pizza");
checkBox3.addActionListener(this);
panel4.add(checkBox3);
JCheckBox checkBox4 = new JCheckBox("Vegetarian Pizza");
checkBox4.addActionListener(this);
panel4.add(checkBox4);
JRadioButton radioButton1 = new JRadioButton("Pick Up");
group.add(radioButton1);
radioButton1.addActionListener(this);
panel1.add(radioButton1);
JRadioButton radioButton2 = new JRadioButton("Delivery");
group.add(radioButton2);
radioButton2.addActionListener(this);
panel1.add(radioButton2);
JLabel name_label = new JLabel("Name on Order");
name.addActionListener(this);
panel5.add(name_label);
panel5.add(name);
setSize(600, 300);
setTitle("Pizza to Order");
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent action) {
}
public static void main(String[] args) {
Project4 ex = new Project4();
ex.setVisible(true);
}
}
You can use a nested JPanel with another layout in order to achieve that. I would go with BorderLayout here. You can also other layouts that allow vertical orientation. Visiting the visual guide to Layout Managers will help you spot them.
JLabel name_label = new JLabel("Name on Order");
name.addActionListener(this);
JPanel verticalNestedPanel = new JPanel(new BorderLayout());
verticalNestedPanel.add(name_label, BorderLayout.PAGE_START);
verticalNestedPanel.add(name, BorderLayout.PAGE_END);
panel5.add(verticalNestedPanel);
I am using JCombobox and just below that there is a panel which contains JTextFields. Whenever i click on the dropdown (JCombobox) some portion of JTextField disappears itself. I don't know what to do. I am unable to post a pic of the problem here because i am a new user and my reputation is below 10.
public class MainClass {
public static void main(String args[]){
Form form = new Form();
int width = 400;
int height = 400;
form.setSize(width, height);
form.setVisible(true);
form.setTitle("Create Network");
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
form.setLocation((screenSize.width-width)/2, (screenSize.height-height)/2);
}
}
This is the form class
public class Form extends JFrame {
private JLabel ipAddress, networkTopo, numNodes;
private JTextField nodes;
private JButton addIp;
private JButton removeIp;
private JButton next, back;
private JPanel jPanel, jPanel1, jPanel2, commonPanel;
private JComboBox<String> dropDown;
private String[] topologies = { "Grid", "Diagnol Grid", "Bus", "Ring",
"Star" };
public Form() {
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
setResizable(false);
this.getContentPane().setBackground(Color.WHITE);
// add(Box.createRigidArea(new Dimension(0,10)));
GridLayout commonPanelLayout = new GridLayout(0, 2);
commonPanelLayout.setVgap(10);
commonPanel = new JPanel(commonPanelLayout);
commonPanel.setVisible(true);
commonPanel.setAlignmentX(commonPanel.LEFT_ALIGNMENT);
commonPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
commonPanel.setBackground(Color.white);
numNodes = new JLabel("Number of Nodes");
commonPanel.add(numNodes);
nodes = new JTextField(20);
commonPanel.add(nodes);
networkTopo = new JLabel("Network Topology");
commonPanel.add(networkTopo);
dropDown = new JComboBox<String>(topologies);
dropDown.setBackground(Color.WHITE);
commonPanel.add(dropDown);
add(commonPanel);
add(Box.createRigidArea(new Dimension(0, 10)));
ipAddress = new JLabel("IP Addresses");
ipAddress.setAlignmentX(ipAddress.LEFT_ALIGNMENT);
ipAddress.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
add(ipAddress);
add(Box.createRigidArea(new Dimension(0, 10)));
jPanel = new JPanel(new FlowLayout());
jPanel.setVisible(true);
jPanel.setAlignmentX(jPanel.LEFT_ALIGNMENT);
jPanel.setBackground(Color.WHITE);
// jPanel1.setAutoscrolls(true);
add(jPanel);
GridLayout layout = new GridLayout(0, 1);
jPanel1 = new JPanel();
layout.setVgap(10);
// jPanel1.setBackground(Color.WHITE);
jPanel1.setVisible(true);
JScrollPane scroll = new JScrollPane();
scroll.setViewportView(jPanel1);
scroll.setAutoscrolls(true);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setPreferredSize(new Dimension(300, 150));
jPanel1.setPreferredSize(new Dimension(scroll.getPreferredSize().width,
Integer.MAX_VALUE));
jPanel.add(scroll);
jPanel2 = new JPanel(new GridLayout(0, 1, 10, 10));
jPanel2.setBackground(Color.WHITE);
jPanel2.setVisible(true);
jPanel.add(jPanel2);
addIp = new JButton("Add");
jPanel2.add(addIp);
removeIp = new JButton("Remove");
jPanel2.add(removeIp);
JPanel savePanel = new JPanel(new FlowLayout());
savePanel.setVisible(true);
savePanel.setAlignmentX(savePanel.LEFT_ALIGNMENT);
savePanel.setBackground(Color.white);
back = new JButton("Back");
back.setAlignmentX(next.LEFT_ALIGNMENT);
back.setEnabled(false);
savePanel.add(back);
next = new JButton("Next");
next.setAlignmentX(next.LEFT_ALIGNMENT);
savePanel.add(next);
add(savePanel);
AddIPEvent addIPEvent = new AddIPEvent();
addIp.addActionListener(addIPEvent);
RemoveIPEvent removeIPEvent = new RemoveIPEvent();
removeIp.addActionListener(removeIPEvent);
}
public class AddIPEvent implements ActionListener {
public void actionPerformed(ActionEvent e) {
JPanel subPanel = new JPanel(new FlowLayout());
// subPanel.setBackground(Color.WHITE);
subPanel.setVisible(true);
JCheckBox jCheckBox = new JCheckBox();
// jCheckBox.setBackground(Color.WHITE);
subPanel.add(jCheckBox);
JTextField ip = new JTextField(12);
subPanel.add(ip);
JTextField nodesInIp = new JTextField(6);
nodesInIp.setVisible(false);
subPanel.add(nodesInIp);
jPanel1.add(subPanel);
jPanel1.repaint();
jPanel1.revalidate();
}
}
public class RemoveIPEvent implements ActionListener {
public void removeIP(Container container) {
for (Component c : container.getComponents()) {
if (c instanceof JCheckBox) {
if (((JCheckBox) c).isSelected()) {
jPanel1.remove(container);
jPanel.revalidate();
jPanel1.repaint();
}
} else if (c instanceof Container) {
removeIP((Container) c);
}
}
}
public void actionPerformed(ActionEvent e) {
removeIP(jPanel1);
}
}
}
After Clicking on the Add button and then clicking on the JComboBox will make portion of JTextField dissapear. Could someone pls point out the mistake?
Whenever i click on the dropdown (JCombobox) some portion of JTextField disappears itself.
//jPanel1.setPreferredSize(new Dimension(scroll.getPreferredSize().width, Integer.MAX_VALUE));
Don't set the preferred size of components. The layout manager will determine the preferred size as components are added/removed. Then scrollbars will appear as required.
jPanel1.repaint();
jPanel1.revalidate();
The order should be:
jPanel1.revalidate();
jPanel1.repaint();
The revalidate() first invokes the layout manager before it is repainted.
//form.setSize(width, height);
form.pack();
Again, don't try to set the size. Let the layout managers do their jobs. The pack() will size the frame based on the sizes of the components added to the frame.
As practice, I've been trying to make a simple tic tac toe game to see how layouts work in java. Now that I have the base code, with all of the rules and variable checks, I cannot find out how to get the buttons to line up the way I want. I wanted to make a 3x3 grid of buttons, but whenever I try a tutorial online or find someone with a similar problem, it always leads to the buttons not showing up at all. The following code gets the buttons on the screen, but doesn't arrange them.
package game;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JFrame{
//JPanel
JPanel pnlMainBoard = new JPanel();
//Buttons
JButton btnTest = new JButton("Test");
JButton btnAI = new JButton("A1");
JButton btnBI = new JButton("B1");
JButton btnCI = new JButton("C1");
JButton btnAII = new JButton("A2");
JButton btnBII = new JButton("B2");
JButton btnCII = new JButton("C2");
JButton btnAIII = new JButton("A3");
JButton btnBIII = new JButton("B3");
JButton btnCIII = new JButton("C3");
public Main(){
//Layout
//pnlMainBoard.setLayout(null);
//Game set bounds
btnTest.setBounds(60,400,220,30);
//JPanel bounds
pnlMainBoard.setBounds(800,800,200,100);
//Add buttons to frame
pnlMainBoard.add(btnTest);
pnlMainBoard.add(btnAI);
pnlMainBoard.add(btnBI);
pnlMainBoard.add(btnCI);
pnlMainBoard.add(btnAII);
pnlMainBoard.add(btnBII);
pnlMainBoard.add(btnCII);
pnlMainBoard.add(btnAIII);
pnlMainBoard.add(btnBIII);
pnlMainBoard.add(btnCIII);
add(pnlMainBoard);
//JFrame Properties
setSize(400,400);
setTitle("Ultimate Tic Tac Toe");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
So far this is the only code I've created that successfully puts the buttons on the screen, and when I change it the buttons disappear. How do I make it so I can set the location of the buttons where I want them on the window?
For a 3x3 grid check out the swing grid layout that you can set the JPanel to use like this:
GridLayout grid = new GridLayout(3,3);
JPanel.setLayout(grid);
Where jpanel is the name of your jpanel in your program...
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class ThreeByThreeWithButtonLayout {
private JComponent ui = null;
ThreeByThreeWithButtonLayout() {
initUI();
}
public void initUI() {
if (ui != null) {
return;
}
int gap = 10;
ui = new JPanel(new BorderLayout(4, 4));
ui.setBorder(new EmptyBorder(4, 4, 4, 4));
JButton testButton = new JButton("Test");
JPanel buttonConstrain = new JPanel(
new FlowLayout(FlowLayout.CENTER, gap, gap));
buttonConstrain.add(testButton);
ui.add(buttonConstrain, BorderLayout.PAGE_START);
JPanel gridPanel = new JPanel(new GridLayout(0, 3, 5, 5));
gridPanel.setBorder(new EmptyBorder(gap, gap, gap, gap));
ui.add(gridPanel, BorderLayout.CENTER);
String[] buttonRows = {"A", "B", "C"};
for (int ii = 1; ii < 4; ii++) {
for (String buttonRow : buttonRows) {
JButton b = new JButton(buttonRow + ii);
b.setFont(b.getFont().deriveFont(32f));
gridPanel.add(b);
}
}
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
ThreeByThreeWithButtonLayout o = new ThreeByThreeWithButtonLayout();
JFrame f = new JFrame("3x3 + Button");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
GridLayout makes some rows and columns to your frame/panel like this :
GridLayout grid = new GridLayout(3,3);
Jpanel.setLayout(grid);
I'm having trouble getting a JPanel inside a BorderLayout to work.
I defined the layout of the Panel as a Grid Layout, and then added a bunch of buttons I had made before hand to the JPanel. However, when I run the program, the JFrame loads but nothing within the frame loads. Here's the code:
import java.awt.*;
import javax.swing.*;
public class Phone extends JFrame {
private JTextField PhoneText;
private JPanel ButtonPanel;
private JButton bttn1;
private JButton bttn2;
private JButton bttn3;
private JButton bttn4;
private JButton bttn5;
private JButton bttn6;
private JButton bttn7;
private JButton bttn8;
private JButton bttn9;
private JButton bttn10;
private JButton bttn11;
private JButton bttn12;
public Phone(){
setTitle("Phone - Agustin Ferreira");
Container ContentPane = getContentPane();
ContentPane.setLayout(new BorderLayout());
setSize(300, 400);
setVisible(true);
setBackground(Color.DARK_GRAY);
PhoneText = new JTextField("(317)188-8566");
bttn1 = new JButton ("1");
bttn2 = new JButton ("2");
bttn3 = new JButton ("3");
bttn4 = new JButton ("4");
bttn5 = new JButton ("5");
bttn6 = new JButton ("6");
bttn7 = new JButton ("7");
bttn8 = new JButton ("8");
bttn9 = new JButton ("9");
bttn10 = new JButton ("*");
bttn11 = new JButton ("0");
bttn12 = new JButton ("#");
ButtonPanel = new JPanel(new GridLayout(4,3,0,0));
ButtonPanel.add(bttn1);
ButtonPanel.add(bttn2);
ButtonPanel.add(bttn3);
ButtonPanel.add(bttn4);
ButtonPanel.add(bttn5);
ButtonPanel.add(bttn6);
ButtonPanel.add(bttn7);
ButtonPanel.add(bttn8);
ButtonPanel.add(bttn9);
ButtonPanel.add(bttn10);
ButtonPanel.add(bttn11);
ButtonPanel.add(bttn12);
ContentPane.add(PhoneText, BorderLayout.NORTH);
ContentPane.add(ButtonPanel, BorderLayout.CENTER);
}
}
Additionally, I have another class that calls the Phone class. Here's the code for that, just in case:
package ProgrammingAssignment11;
import javax.swing.JFrame;
public class GUI_Driver {
public static void main(String[] args) {
Phone Nokia;
Nokia = new Phone();
Nokia.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
Any Help?
Much appreciated, M3tal T1ger
Your main problem:
You should only call setVisible(true) after adding components to your GUI. You don't do this and so the GUI gets drawn without its components.
Also:
You should avoid setting the sizes or preferred sizes of anything. Instead let the components and layout managers size themselves.
And don't forget to call pack() after adding all components and before making the GUI visible.
Learn and follow Java naming conventions, including giving all variables and methods names that begin with a lower case letter, and all classes with names that start with an upper-case letter.
For example:
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
#SuppressWarnings("serial")
public class Phone2 extends JPanel {
private static final String[][] BTN_TEXTS = {
{"1", "2", "3"},
{"4", "5", "6"},
{"7", "8", "9"},
{"*", "0", "#"}
};
private static final float BTN_POINTS = 48f;
private static final float TEXT_POINTS = 24f;
private static final int DISPLAY_COLUMNS = 12;
private JButton[][] buttons = new JButton[BTN_TEXTS.length][BTN_TEXTS[0].length];
private JTextField display = new JTextField(DISPLAY_COLUMNS);
public Phone2() {
display.setFocusable(false);
display.setFont(display.getFont().deriveFont(TEXT_POINTS));
GridLayout gridLayout = new GridLayout(BTN_TEXTS.length, BTN_TEXTS[0].length);
JPanel btnPanel = new JPanel(gridLayout);
for (int i = 0; i < BTN_TEXTS.length; i++) {
for (int j = 0; j < BTN_TEXTS[i].length; j++) {
String text = BTN_TEXTS[i][j];
JButton btn = new JButton(new BtnAction(text));
btn.setFont(btn.getFont().deriveFont(Font.BOLD, BTN_POINTS));
btnPanel.add(btn);
buttons[i][j] = btn;
}
}
setLayout(new BorderLayout());
add(display, BorderLayout.NORTH);
add(btnPanel, BorderLayout.CENTER);
}
private class BtnAction extends AbstractAction {
public BtnAction(String name) {
super(name);
}
#Override
public void actionPerformed(ActionEvent evt) {
String text = evt.getActionCommand();
display.setText(display.getText() + text);
}
}
private static void createAndShowGui() {
Phone2 mainPanel = new Phone2();
JFrame frame = new JFrame("Phone");
frame.setDefaultCloseOperation(JFrame.EXIT_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();
}
});
}
}
This is driving me insane. I'm trying to change the panels when you click on 3 different buttons and it works, but for one panel - only once.
If you click addPerson - the Person panel shows,
If you then click addCD - the CD panel shows; (same for view store)
If you then click on addPerson - it doesn't work. It throws a nullpointer exception.
Even if you click on addCD/viewstore and THEN add Person it shows but it just won't show a second time.
In a test file, I created a GUI with an add and remove: if I clicked add it threw the null pointer exception but if I just added it already and compiled, it was fine...
/* PERSON PANEL */
public JPanel create_PersonPnl()
{
personPnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
personPnl.setBackground(Color.WHITE);
personPnl.setPreferredSize(minPnl);
/* VERTICAL BOX for Person boxes */
Box personBox = Box.createVerticalBox();
personBox.setBorder(new TitledBorder(new LineBorder(Color.DARK_GRAY), "Person"));
/* Horizontal Box for Name Lbl & TF */
Box nameBox = Box.createHorizontalBox();
nameBox.add(Box.createHorizontalStrut(10));
nameLbl = new JLabel("Name: ");
nameBox.add(nameLbl);
nameBox.add(Box.createHorizontalStrut(5));
nameTF = new JTextField();
nameBox.add(nameTF);
nameBox.add(Box.createHorizontalStrut(10));
personBox.add(nameBox);
personBox.add(Box.createVerticalStrut(10));
Box limitBox = Box.createHorizontalBox();
limitBox.add(Box.createHorizontalStrut(10));
limitLbl = new JLabel("CD Limit: ");
limitBox.add(limitLbl);
limitTF = new JFormattedTextField();
limitBox.add(limitTF);
limitBox.add(Box.createHorizontalStrut(10));
personBox.add(limitBox);
personBox.add(Box.createVerticalStrut(10));
personBox.add(addPersonBtn = new JButton("Add Person"));
personBox.add(Box.createVerticalStrut(10));
personList = new JList(temp);
personList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
scrollp = new JScrollPane(personList);
scrollp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollp.setSize(100, 45);
personBox.add(scrollp);
personBox.add(Box.createVerticalStrut(10));
Box optionsBox = Box.createHorizontalBox();
editPrsnBtn = new JButton("Edit");
editPrsnBtn.addActionListener(this);
optionsBox.add(editPrsnBtn);
removePrsnBtn = new JButton("Remove");
removePrsnBtn.addActionListener(this);
optionsBox.add(removePrsnBtn);
personBox.add(optionsBox);
personPnl.add(personBox);
return personPnl;
}
This is what is in my ActionPerformed method.
if(e.getSource() == addPersonBtn)
{
changePnl.removeAll();
changePnl.add(create_PersonPnl());
changePnl.revalidate();
System.out.println("PersonPnl added");
}
if(e.getSource() == addCDBtn)
{
changePnl.removeAll();
changePnl.add(create_CDPnl());
changePnl.revalidate();
}
if(e.getSource() == viewStoreBtn)
{
changePnl.removeAll();
changePnl.add(create_StorePnl());
changePnl.revalidate();
}
Only code for ChangePnl.
changePnl = new JPanel();
changePnl.setBackground(Color.WHITE);
defaultPnl = new JPanel();
defaultPnl.setBackground(Color.WHITE);
defaultPnl.add(new JLabel("Welcome to the CD Store"));
defaultPnl.add(new JLabel("Click an option from the left"));
changePnl.add(defaultPnl);
The S.O.P was to debug to see what was being run.. and that only prints once and that's it unless I take out .add(create_PersonPnl()); so I've narrowed it but I don't have a clue since it works the first time.
Thank in advance!
Separate test file to prove it's create_PersonPnl()
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
public class Test implements ActionListener
{
JButton add, remove;
JButton addPersonBtn, editPrsnBtn, removePrsnBtn;
JFrame frame;
JFormattedTextField limitTF;
JLabel nameLbl, limitLbl;
JList personList;
JPanel TotalGUI, welcomePnl, mainPnl, imagePnl, changePnl, defaultPnl, cdPnl, storePnl;
JPanel personPnl;
JScrollPane scrollp;
JTextField nameTF;
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
Dimension minPnl = new Dimension(300, 400);
/* Test for JList */
String[] temp = {"1", "2", "3", "4", "1", "2", "3", "4","1", "2", "3", "4","1", "2", "3", "4" };
public Test()
{
frame = new JFrame("CD Store");
frame.setExtendedState(JFrame.NORMAL);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.pack(); //sets size based on components size in TotalGUI
//frame.setJMenuBar(create_MenuBar());
//frame.setMinimumSize(minDim);
frame.setSize(725, 550);
//set frame location (central to screen)
int fw = frame.getSize().width;
int fh = frame.getSize().height;
int fx = (dim.width-fw)/2;
int fy = (dim.height-fh)/2;
frame.getContentPane().add(create_Content_Pane());
frame.setVisible(true);
//moves the frame to the centre
frame.setLocation(fx, fy);
}
public JPanel create_Content_Pane()
{
JPanel TotalGUI = new JPanel();
TotalGUI.add(remove = new JButton("Remove"));
remove.addActionListener(this);
TotalGUI.add(add = new JButton("Add")); <- this crashes when selected
add.addActionListener(this);
//TotalGUI.add(create_PersonPnl()); <- works just fine
return TotalGUI;
}
public JPanel create_PersonPnl()
{
personPnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
personPnl.setBackground(Color.WHITE);
personPnl.setPreferredSize(minPnl);
/* VERTICAL BOX for Person boxes */
Box personBox = Box.createVerticalBox();
personBox.setBorder(new TitledBorder(new LineBorder(Color.DARK_GRAY), "Person"));
/* Horizontal Box for Name Lbl & TF */
Box nameBox = Box.createHorizontalBox();
nameBox.add(Box.createHorizontalStrut(10));
nameLbl = new JLabel("Name: ");
nameBox.add(nameLbl);
nameBox.add(Box.createHorizontalStrut(5));
nameTF = new JTextField();
nameBox.add(nameTF);
nameBox.add(Box.createHorizontalStrut(10));
personBox.add(nameBox);
personBox.add(Box.createVerticalStrut(10));
Box limitBox = Box.createHorizontalBox();
limitBox.add(Box.createHorizontalStrut(10));
limitLbl = new JLabel("CD Limit: ");
limitBox.add(limitLbl);
limitTF = new JFormattedTextField();
limitBox.add(limitTF);
limitBox.add(Box.createHorizontalStrut(10));
personBox.add(limitBox);
personBox.add(Box.createVerticalStrut(10));
personBox.add(addPersonBtn = new JButton("Add Person"));
personBox.add(Box.createVerticalStrut(10));
personList = new JList(temp);
personList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
scrollp = new JScrollPane(personList);
scrollp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollp.setSize(100, 45);
personBox.add(scrollp);
personBox.add(Box.createVerticalStrut(10));
Box optionsBox = Box.createHorizontalBox();
editPrsnBtn = new JButton("Edit");
editPrsnBtn.addActionListener(this);
optionsBox.add(editPrsnBtn);
removePrsnBtn = new JButton("Remove");
removePrsnBtn.addActionListener(this);
optionsBox.add(removePrsnBtn);
personBox.add(optionsBox);
personPnl.add(personBox);
return personPnl;
}
public static void main(String[] args)
{
new Test();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == remove)
{
TotalGUI.removeAll();
TotalGUI.revalidate();
}
if(e.getSource() == add)
{
TotalGUI.add(create_PersonPnl());
TotalGUI.revalidate();
}
}
}
In your method
public JPanel create_Content_Pane()
{
JPanel TotalGUI = new JPanel();
TotalGUI.add(remove = new JButton("Remove"));
remove.addActionListener(this);
TotalGUI.add(add = new JButton("Add")); <- this crashes when selected
add.addActionListener(this);
//TotalGUI.add(create_PersonPnl()); <- works just fine
return TotalGUI;
}
The NullPointerException occurs because your private field TotalGUI is null... Remove the JPanel declaration in front of TotalGUI = new JPanel(); That will solve the null pointer problem. This probably solves only your problem in the test scenario... To solve the problem in your original scenario it would be nice to have the complete source code of the class..