0

I'm having a very hard time with borders, I've looked through tutorials and examples and each seems to use a different style, I'm just trying to organise the content into separated borders. The content for the bottom panal isn't finished yet but I'm just trying to get it to work as is before adding more.

The compiler says there are problems on two lines java.lang.NullPointerException:

package question2;

import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

/**
 * @author Matt Headley
  This frame shows a data set and its statistics.
  */
 public class ComputerStoreGUI extends JFrame
 {  



   private JCheckBox item1;
   private JCheckBox item2;
   private JCheckBox item3;
   private JCheckBox item4;
   private JCheckBox item5;
   private TitledBorder border;
   private TitledBorder border2;
   private static JPanel content;
   private static JPanel top;
   private static JPanel bottom;
    private JTextField parts;

public ComputerStoreGUI()
 {       
  JCheckBox item1 = new JCheckBox("Install Hard Drive - $25.00");
  JCheckBox item2 = new JCheckBox("Install RAM - $15.00");
  JCheckBox item3 = new JCheckBox("Remove Virus - $50.00");
  JCheckBox item4 = new JCheckBox("Format Hard Drive - $80.00");
  JCheckBox item5 = new JCheckBox("Quote Hourly Labour - $10.00");
  item1.setHorizontalAlignment(JCheckBox.LEFT);
  item2.setHorizontalAlignment(JCheckBox.LEFT);
  item3.setHorizontalAlignment(JCheckBox.LEFT);
  item4.setHorizontalAlignment(JCheckBox.LEFT);
  item5.setHorizontalAlignment(JCheckBox.LEFT);
  JTextField cost = new JTextField(10);


  top = new JPanel(new FlowLayout(FlowLayout.LEFT));
  top.add(item1);
  top.add(item2);
  top.add(item3);
  top.add(item4);
  top.add(item5);
  bottom.add(cost); //here ????????
  cost.setHorizontalAlignment(JTextField.CENTER);


}



public static void main(String[] args)
{       
JFrame frame = new ComputerStoreGUI(); // and here ???????

content = new JPanel();
content.setBorder(BorderFactory.createEmptyBorder(10, 10, 10,  10));

top = new JPanel();
top.setBorder(BorderFactory.createTitledBorder("Standard Services"));

bottom = new JPanel();
bottom.setBorder(BorderFactory.createTitledBorder("Hourly Service"));

    frame.setSize(250, 400);
    frame.setTitle("LU Computer Store");
    frame.setContentPane(content);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    content.add(top);
    content.add(bottom);
    frame.setVisible(true);
}

}

the end goal is to look something like this: enter image description here

Matt
  • 59
  • 6

1 Answers1

0

First, I dont see a reson to extend JFrame in your example. So without inheritance you can use composition like:

JFrmae frame = new JFrame();

Read more: Why do we need to extend JFrame in a swing application?

The compiler says there are problems on two lines java.lang.NullPointerException:

Reason is, you have declared the JPanel bottom but without initializing you are trying to use it, from this line: bottom.add(cost);

Do something like:

bottom = new JPanel(new FlowLayout(FlowLayout.LEFT));
bottom.add(cost);

EDIT:

comment: It runs now, but the panels are tiny

Because you need to set layout for the parent JPanel, you can use BoxLayout, to add child panels one below another, like below:

content = new JPanel();
content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));

In case if you want to add space between those two child panels, use as below:

content.add(top);
content.add(Box.createRigidArea(new Dimension(0,10)));
content.add(bottom);
Blasanka
  • 21,001
  • 12
  • 102
  • 104