Java Swing won’t display - eclipse

I’m getting an error for regarding a nullPointerException and my JavaSwing project won’t display as a result. I think I need to initialise the employeeInfoPanel to the JFrams on line 55 but I dont know how to do this. There could be other errors after that. Any info/tips would be greatly appreciated!
package employee;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class taxSystemDriver1 extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel homePanel, employeeInfoPanel, taxDetailsPanel, textInputPanel;
private JPanel ppsPanel, surnamePanel, firstnamePanel, dobPanel, salaryPanel, maritialStatusPanel,noIncomesPanel, noDependentChildrenPanel, noBIKPanel;
private JPanel buttonPanel, buttonPanelTop, buttonPanelBottom;
private JButton calculatorButton, recordsButton, addEmployeeButton, modifyEmployeeButton, deleteEmployeeButton, saveButton, backButton;
private JLabel comboLabel1, ppsLabel, surnameLabel, firstnameLabel, dobLabel, salaryLabel, maritialStatusLabel, noIncomesLabel, noDependentChildrenLabel, noBIKLabel;
private JLabel birthDayLabel, birthMonthLabel, birthYearLabel;
private JComboBox birthDayComboBox, birthMonthComboBox, birthYearComboBox, incomeComboBox;
private JRadioButton childrenRadioButton, maritialStatusRadioButton;
private JButton addEmployee, deleteEmployee, modifyEmployee, displayFirst, displayNext, displayLast, displayTaxDetails;
private JTextField ppsTF, surnameTF, firstnameTF, dobTF, salaryTF, maritialStatusTF, noIncomesTF, noDependentChildrenTF, noBIKTF;
private JTextArea statementTextArea;
private JScrollPane statementPane;
public taxSystemDriver1 (String title) {
super(title);
initComponents();
}
private void initComponents() {
setSize(300, 500);
setLocation(100, 100);
//construct components
//**************************************************************************
// HomePanel North
//**************************************************************************
//*********************************************
// EmployeeInfo Panel West
//*********************************************
// PPS NUMBER (TextField)
ppsPanel = new JPanel();
ppsLabel = new JLabel ("PPS Number: ");
ppsLabel.setHorizontalAlignment(SwingConstants.LEFT);
ppsTF = new JTextField (5);
ppsTF.setEditable(true);
ppsPanel.add(ppsLabel);
ppsPanel.add(ppsTF);
employeeInfoPanel.add(ppsPanel);
// SURNAME (TextField)
surnamePanel = new JPanel();
surnameLabel = new JLabel ("Employee Surname: ");
surnameLabel.setHorizontalAlignment(SwingConstants.LEFT);
surnameTF = new JTextField (5);
surnameTF.setEditable(true);
surnamePanel.add(surnameLabel);
surnamePanel.add(surnameTF);
employeeInfoPanel.add(surnamePanel);
// FIRST NAME (TextField)
firstnamePanel = new JPanel();
firstnameLabel = new JLabel ("Employee First Name: ");
firstnameLabel.setHorizontalAlignment(SwingConstants.LEFT);
firstnameTF = new JTextField (5);
firstnameTF.setEditable(true);
firstnamePanel.add(firstnameLabel);
firstnamePanel.add(firstnameTF);
employeeInfoPanel.add(firstnamePanel);
// DOB (ComboBox)
dobPanel = new JPanel();
dobPanel.add(new JLabel ("Employee Date of Birth: "));
String[] days = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15",
"16","17","18","19","20","21","22","23","24","25","26","27","28",
"29","30","31"};
birthDayComboBox = new JComboBox(days);
dobPanel.add(birthDayComboBox);
String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
"Oct", "Nov", "Dec"};
birthMonthComboBox = new JComboBox(months);
dobPanel.add(birthMonthComboBox);
String[] years = {"1996", "1995", "1994", "1993", "1992", "1991", "1990", "1989", "1988", "1987", "1986", "1985", "1984",
"1983", "1982", "1981", "1980", "1979", "1978", "1977", "1976", "1975", "1974", "1973", "1972", "1971", "1970",
"1969", "1968", "1967", "1966", "1965", "1964", "1963", "1962", "1961", "1960", "1959", "1958", "1957", "1956",
"1955", "1954", "1953", "1952", "1951", "1950", "1949", "1948", "1947", "1946", "1945", "1944", "1943", "1942",
"1941", "1940"};
birthYearComboBox = new JComboBox (years);
dobPanel.add(birthYearComboBox);
employeeInfoPanel.add(dobPanel);
//*********************************************
// TaxDetails Panel East
//*********************************************
// SALARY (TextField)
salaryPanel = new JPanel();
salaryLabel = new JLabel ("Employee yearly salary: €");
salaryLabel.setHorizontalAlignment(SwingConstants.LEFT);
salaryTF = new JTextField (5);
salaryTF.setEditable(true);
salaryPanel.add(salaryLabel);
salaryPanel.add(salaryTF);
taxDetailsPanel.add(salaryPanel);
// MARITIAL STATUS (Radio Button)
final ButtonGroup maritialStatus = new ButtonGroup();
maritialStatusPanel = new JPanel();
maritialStatusPanel.add(new JLabel("Relationship Status: "));
maritialStatusRadioButton = new JRadioButton ("Single", true);
maritialStatusRadioButton.setActionCommand("Single");
maritialStatus.add(maritialStatusRadioButton);
maritialStatusPanel.add(maritialStatusRadioButton);
maritialStatusRadioButton = new JRadioButton ("In unmarried Relationship");
maritialStatusRadioButton.setActionCommand("In unmarried Relationship");
maritialStatus.add(maritialStatusRadioButton);
maritialStatusPanel.add(maritialStatusRadioButton);
maritialStatusRadioButton = new JRadioButton ("Married");
maritialStatusRadioButton.setActionCommand("Married");
maritialStatus.add(maritialStatusRadioButton);
maritialStatusPanel.add(maritialStatusRadioButton);
taxDetailsPanel.add(maritialStatusRadioButton);
// NUMBER OF INCOMES IN HOUSEHOLD (ComboBox)
noIncomesPanel = new JPanel();
noIncomesPanel.add(new JLabel("Number of Incomes in the Household"));
String[] income = {"1", "2"};
incomeComboBox = new JComboBox(income);
noIncomesPanel.add(incomeComboBox);
taxDetailsPanel.add(incomeComboBox);
// NUMBER OF INDEPENDENT CHILDREN (Radio Button)
final ButtonGroup childrenNumber = new ButtonGroup();
noDependentChildrenPanel = new JPanel();
noDependentChildrenPanel.add(new JLabel("Number of Dependent Children: "));
childrenRadioButton = new JRadioButton ("0", true);
childrenRadioButton.setActionCommand("0");
childrenNumber.add(childrenRadioButton);
noDependentChildrenPanel.add(childrenRadioButton);
childrenRadioButton = new JRadioButton ("1");
childrenRadioButton.setActionCommand("1");
childrenNumber.add(childrenRadioButton);
noDependentChildrenPanel.add(childrenRadioButton);
childrenRadioButton = new JRadioButton ("2");
childrenRadioButton.setActionCommand("2");
childrenNumber.add(childrenRadioButton);
noDependentChildrenPanel.add(childrenRadioButton);
childrenRadioButton = new JRadioButton ("3");
childrenRadioButton.setActionCommand("3");
childrenNumber.add(childrenRadioButton);
noDependentChildrenPanel.add(childrenRadioButton);
childrenRadioButton = new JRadioButton ("4");
childrenRadioButton.setActionCommand("4");
childrenNumber.add(childrenRadioButton);
noDependentChildrenPanel.add(childrenRadioButton);
childrenRadioButton = new JRadioButton ("5");
childrenRadioButton.setActionCommand("5");
childrenNumber.add(childrenRadioButton);
noDependentChildrenPanel.add(childrenRadioButton);
taxDetailsPanel.add(childrenRadioButton);
// AMOUNT OF BENEFIT IN KIND (TextField)
noBIKPanel = new JPanel();
noBIKLabel = new JLabel ("Amount of benefits in kind : €");
noBIKLabel.setHorizontalAlignment(SwingConstants.LEFT);
noBIKTF = new JTextField (5);
noBIKTF.setEditable(true);
noBIKPanel.add(noBIKLabel);
noBIKPanel.add(noBIKTF);
taxDetailsPanel.add(noBIKPanel);
Container content = getContentPane();
content.setLayout(new BorderLayout());
content.add(homePanel, BorderLayout.NORTH);
homePanel.setLayout(new BorderLayout());
homePanel.add(employeeInfoPanel, BorderLayout.WEST);
homePanel.add(taxDetailsPanel, BorderLayout.EAST);
//*****************************************************
// ButtonGroup Panel Center
//*****************************************************
addEmployeeButton = new JButton("Add Employee");
modifyEmployeeButton = new JButton("Modify Employee");
deleteEmployeeButton = new JButton("Delete Employee");
displayFirst = new JButton("Display First Employee");
displayNext = new JButton("Display Next Employee");
displayLast = new JButton("Display Last Employee");
displayTaxDetails = new JButton("Display Employee Tax Details");
content.add(buttonPanel, BorderLayout.CENTER);
buttonPanel.setLayout(new BorderLayout());
buttonPanel.add(buttonPanelTop, BorderLayout.NORTH);
buttonPanel.add(buttonPanelBottom, BorderLayout.SOUTH);
buttonPanelTop.setLayout(new FlowLayout());
buttonPanelTop.add(addEmployeeButton);
buttonPanelTop.add(modifyEmployeeButton);
buttonPanelTop.add(deleteEmployeeButton);
buttonPanelBottom.setLayout(new FlowLayout());
buttonPanelBottom.add(displayFirst);
buttonPanelBottom.add(displayLast);
buttonPanelBottom.add(displayNext);
buttonPanelBottom.add(displayTaxDetails);
}
/*employeeInfoPanel = new JPanel();
employeeInfoPanel.setLayout(new GridLayout(7, 1));
public static void main(String[] args)
{
JFrame taxFrame = new taxSystemDriver1("Tax and Revenue System");
taxFrame.setVisible(true);

Your are calling a method on an object that does not exist (employeeInfoPanel is an attribute which is never constructed or assigned before the #add(...) method call).
To initialize your employeeInfoPanel, a simple default construction:
employeeInfoPanel = new JPanel();

Related

SWT CTabFolder doesn't display when running from eclipse

I am creating an app using WindowBuilder in eclipse 4.4.2. I've added CTabFolders and corresponding CTabItems. The tabs are displayed properly when Click the preview button(Quickly test/preview the window without compiling or running it) in windowbuilder.
Screen Shot===> http://i.imgur.com/Sx9TQrL.jpg
But the real problem is when I run the app,the tabs are not displayed.
Screen Shot===> http://i.imgur.com/OP7WY8W.jpg
Code
public class Dashboard {
public static void main(String[] args) {
Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setImage(SWTResourceManager.getImage(Dashboard.class,
"/com/sun/java/swing/plaf/windows/icons/Computer.gif"));
shell.setBackground(SWTResourceManager
.getColor(SWT.COLOR_WIDGET_BORDER));
shell.setSize(1080, 700);
shell.setLayout(new GridLayout(2, false));
/**
* Base Layout
* **/
Composite content = new Composite(shell, SWT.BORDER);
content.setBackground(SWTResourceManager
.getColor(SWT.COLOR_WIDGET_BACKGROUND));
content.setLayout(new StackLayout());
// content.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false,
// false));
content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final CTabFolder tabFolder = new CTabFolder(content, SWT.CLOSE);
tabFolder.setTabHeight(28);
tabFolder.setSimple(false);
tabFolder.setSelectionForeground(SWTResourceManager
.getColor(SWT.COLOR_BLACK));
tabFolder.setBackground(SWTResourceManager
.getColor(SWT.COLOR_WIDGET_BACKGROUND));
tabFolder.setSelectionBackground(Display.getCurrent().getSystemColor(
SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));
CTabItem homeTab = new CTabItem(tabFolder, SWT.NONE);
homeTab.setText("Home");
Composite composite_4 = new Composite(tabFolder, SWT.NONE);
homeTab.setControl(composite_4);
Label lblHome = new Label(composite_4, SWT.NONE);
lblHome.setBounds(185, 105, 55, 15);
lblHome.setText("Home");
final CTabItem empTab = new CTabItem(tabFolder, SWT.CLOSE);
empTab.setImage(SWTResourceManager.getImage(
Dashboard.class, "/resource/icons/employee.png"));
empTab.setText("Employees");
Composite composite_5 = new Composite(tabFolder, SWT.NONE);
empTab.setControl(composite_5);
Label lblEmployees = new Label(composite_5, SWT.NONE);
lblEmployees.setBounds(155, 102, 55, 15);
lblEmployees.setText("Employees");
CTabItem workTab = new CTabItem(tabFolder, SWT.CLOSE);
workTab.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/worksite.png"));
workTab.setText("Work Site");
Composite composite_6 = new Composite(tabFolder, SWT.NONE);
workTab.setControl(composite_6);
Label lblWorkSite = new Label(composite_6, SWT.NONE);
lblWorkSite.setBounds(222, 123, 55, 15);
lblWorkSite.setText("Work site");
CTabItem paymentTab = new CTabItem(tabFolder, SWT.NONE);
paymentTab.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/payments.png"));
paymentTab.setText("Payments");
Composite composite_7 = new Composite(tabFolder, SWT.NONE);
paymentTab.setControl(composite_7);
Label lblPayments = new Label(composite_7, SWT.NONE);
lblPayments.setBounds(185, 176, 55, 15);
lblPayments.setText("Payments");
CTabItem toolsTab = new CTabItem(tabFolder, SWT.NONE);
toolsTab.setImage(SWTResourceManager.getImage(Dashboard.class, "/resource/icons/tools.png"));
toolsTab.setShowClose(true);
toolsTab.setText("Tools");
Composite composite_8 = new Composite(tabFolder, SWT.NONE);
toolsTab.setControl(composite_8);
Label lblTools = new Label(composite_8, SWT.NONE);
lblTools.setBounds(264, 125, 55, 15);
lblTools.setText("Tools");
/**
* MenuBar
* **/
Menu menu = new Menu(shell, SWT.BAR);
shell.setMenuBar(menu);
MenuItem mntmFile = new MenuItem(menu, SWT.CASCADE);
mntmFile.setImage(null);
mntmFile.setText("File");
Menu menu_1 = new Menu(mntmFile);
mntmFile.setMenu(menu_1);
MenuItem mntmExit = new MenuItem(menu_1, SWT.NONE);
mntmExit.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
shell.dispose();
}
});
mntmExit.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/close.png"));
mntmExit.setText("Exit");
MenuItem mntmHelp = new MenuItem(menu, SWT.CASCADE);
mntmHelp.setText("Help");
Menu menu_2 = new Menu(mntmHelp);
mntmHelp.setMenu(menu_2);
MenuItem mntmAbout = new MenuItem(menu_2, SWT.NONE);
mntmAbout.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
About abdialog = new About(shell, 0);
abdialog.open();
}
});
mntmAbout.setText("About");
Composite sidebar = new Composite(shell, SWT.NONE);
sidebar.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1,
1));
sidebar.setBackground(SWTResourceManager.getColor(112, 128, 144));
sidebar.setLayout(new GridLayout(1, false));
ExpandBar expandBar = new ExpandBar(sidebar, SWT.NONE);
expandBar.setBackground(SWTResourceManager.getColor(112, 128, 144));
expandBar.setTouchEnabled(true);
GridData gd_expandBar = new GridData(SWT.CENTER, SWT.CENTER, false,
false, 1, 1);
gd_expandBar.heightHint = 619;
expandBar.setLayoutData(gd_expandBar);
ExpandItem xpndtmEmployeeDetails = new ExpandItem(expandBar, SWT.NONE,
0);
xpndtmEmployeeDetails.setExpanded(true);
xpndtmEmployeeDetails.setImage(SWTResourceManager.getImage(
Dashboard.class, "/resource/icons/employee.png"));
xpndtmEmployeeDetails.setText("Employee Details");
Composite expandbarComposite = new Composite(expandBar, SWT.NONE);
xpndtmEmployeeDetails.setControl(expandbarComposite);
xpndtmEmployeeDetails.setHeight(100);
expandbarComposite.setLayout(new FormLayout());
expandbarComposite.setBackground(SWTResourceManager
.getColor(SWT.COLOR_WIDGET_BACKGROUND));
Button btnAddNew = new Button(expandbarComposite, SWT.NONE);
btnAddNew.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/ic_add.png"));
FormData fd_btnAddNew = new FormData();
fd_btnAddNew.right = new FormAttachment(100, -10);
fd_btnAddNew.left = new FormAttachment(0, 38);
btnAddNew.setLayoutData(fd_btnAddNew);
btnAddNew.setText("Add New");
Button btnEdit = new Button(expandbarComposite, SWT.FLAT | SWT.CENTER);
btnEdit.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
}
});
btnEdit.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/edit.png"));
fd_btnAddNew.bottom = new FormAttachment(btnEdit, -6);
FormData fd_btnEdit = new FormData();
fd_btnEdit.left = new FormAttachment(btnAddNew, 0, SWT.LEFT);
fd_btnEdit.right = new FormAttachment(btnAddNew, 0, SWT.RIGHT);
fd_btnEdit.bottom = new FormAttachment(100);
btnEdit.setLayoutData(fd_btnEdit);
btnEdit.setText("Edit");
/**
* View Employees Button
* **/
Button btnViewEmployees = new Button(expandbarComposite, SWT.NONE);
btnViewEmployees.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
tabFolder.setSelection(empTab);
}
});
FormData fd_btnViewEmployees = new FormData();
fd_btnViewEmployees.right = new FormAttachment(btnAddNew, 0, SWT.RIGHT);
fd_btnViewEmployees.top = new FormAttachment(0, 6);
fd_btnViewEmployees.left = new FormAttachment(btnAddNew, 0, SWT.LEFT);
btnViewEmployees.setLayoutData(fd_btnViewEmployees);
btnViewEmployees.setText("View Employees");
ExpandItem xpndtmWorkSiteDetails = new ExpandItem(expandBar, SWT.NONE);
xpndtmWorkSiteDetails.setImage(SWTResourceManager.getImage(
Dashboard.class, "/resource/icons/worksite.png"));
xpndtmWorkSiteDetails.setText("Work Site Details");
Composite composite = new Composite(expandBar, SWT.NONE);
xpndtmWorkSiteDetails.setControl(composite);
xpndtmWorkSiteDetails.setHeight(100);
composite.setLayout(new FormLayout());
Button btnAddNew_1 = new Button(composite, SWT.NONE);
btnAddNew_1.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/ic_add.png"));
FormData fd_btnAddNew_1 = new FormData();
fd_btnAddNew_1.right = new FormAttachment(100, -10);
fd_btnAddNew_1.top = new FormAttachment(0, 37);
btnAddNew_1.setLayoutData(fd_btnAddNew_1);
btnAddNew_1.setText("Add New");
Button btnEdit_1 = new Button(composite, SWT.NONE);
fd_btnAddNew_1.bottom = new FormAttachment(btnEdit_1, -6);
fd_btnAddNew_1.left = new FormAttachment(btnEdit_1, 0, SWT.LEFT);
btnEdit_1.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/edit.png"));
FormData fd_btnEdit_1 = new FormData();
fd_btnEdit_1.left = new FormAttachment(0, 40);
fd_btnEdit_1.right = new FormAttachment(100, -10);
fd_btnEdit_1.bottom = new FormAttachment(100);
btnEdit_1.setLayoutData(fd_btnEdit_1);
btnEdit_1.setText("Edit");
Button btnViewWorks = new Button(composite, SWT.NONE);
FormData fd_btnViewWorks = new FormData();
fd_btnViewWorks.top = new FormAttachment(0, 6);
fd_btnViewWorks.left = new FormAttachment(btnAddNew_1, 0, SWT.LEFT);
fd_btnViewWorks.right = new FormAttachment(100, -10);
btnViewWorks.setLayoutData(fd_btnViewWorks);
btnViewWorks.setText("View Works");
ExpandItem xpndtmPaymentDetails = new ExpandItem(expandBar, SWT.NONE);
xpndtmPaymentDetails.setImage(SWTResourceManager.getImage(
Dashboard.class, "/resource/icons/payments.png"));
xpndtmPaymentDetails.setText("Payment Details");
Composite composite_1 = new Composite(expandBar, SWT.NONE);
xpndtmPaymentDetails.setControl(composite_1);
xpndtmPaymentDetails.setHeight(100);
composite_1.setLayout(new FormLayout());
Button btnAddNew_2 = new Button(composite_1, SWT.NONE);
btnAddNew_2.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/ic_add.png"));
FormData fd_btnAddNew_2 = new FormData();
fd_btnAddNew_2.left = new FormAttachment(0, 44);
btnAddNew_2.setLayoutData(fd_btnAddNew_2);
btnAddNew_2.setText("Add New");
Button btnEditPayments = new Button(composite_1, SWT.NONE);
fd_btnAddNew_2.bottom = new FormAttachment(btnEditPayments, -6);
fd_btnAddNew_2.right = new FormAttachment(btnEditPayments, 0, SWT.RIGHT);
btnEditPayments.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/edit.png"));
FormData fd_btnEditPayments = new FormData();
fd_btnEditPayments.left = new FormAttachment(0, 44);
fd_btnEditPayments.right = new FormAttachment(100, -10);
fd_btnEditPayments.bottom = new FormAttachment(100);
btnEditPayments.setLayoutData(fd_btnEditPayments);
btnEditPayments.setText("Edit Payments");
Button btnViewPayments = new Button(composite_1, SWT.NONE);
FormData fd_btnViewPayments = new FormData();
fd_btnViewPayments.right = new FormAttachment(btnAddNew_2, 0, SWT.RIGHT);
fd_btnViewPayments.top = new FormAttachment(0, 6);
fd_btnViewPayments.left = new FormAttachment(btnAddNew_2, 0, SWT.LEFT);
btnViewPayments.setLayoutData(fd_btnViewPayments);
btnViewPayments.setText("View Payments");
ExpandItem xpndtmOurTools = new ExpandItem(expandBar, SWT.NONE);
xpndtmOurTools.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/tools.png"));
xpndtmOurTools.setText("Tools Available");
Composite composite_3 = new Composite(expandBar, SWT.NONE);
xpndtmOurTools.setControl(composite_3);
xpndtmOurTools.setHeight(80);
composite_3.setLayout(new FormLayout());
Button btnViewTools = new Button(composite_3, SWT.NONE);
FormData fd_btnViewTools = new FormData();
fd_btnViewTools.top = new FormAttachment(0, 10);
fd_btnViewTools.right = new FormAttachment(100, -10);
fd_btnViewTools.left = new FormAttachment(100, -131);
btnViewTools.setLayoutData(fd_btnViewTools);
btnViewTools.setText("View Tools");
Button btnAddNew_3 = new Button(composite_3, SWT.NONE);
btnAddNew_3.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
System.out.println("Add tools");
}
});
FormData fd_btnAddNew_3 = new FormData();
fd_btnAddNew_3.right = new FormAttachment(btnViewTools, 0, SWT.RIGHT);
fd_btnAddNew_3.top = new FormAttachment(btnViewTools, 6);
fd_btnAddNew_3.left = new FormAttachment(btnViewTools, 0, SWT.LEFT);
btnAddNew_3.setLayoutData(fd_btnAddNew_3);
btnAddNew_3.setText("Add New");
ExpandItem xpndtmReports = new ExpandItem(expandBar, SWT.NONE);
xpndtmReports.setExpanded(true);
xpndtmReports.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/reports.png"));
xpndtmReports.setText("Reports");
xpndtmReports.setHeight(80);
Composite composite_2 = new Composite(expandBar, SWT.NONE);
xpndtmReports.setControl(composite_2);
composite_2.setLayout(new FormLayout());
Button btnGenerateReport = new Button(composite_2, SWT.NONE);
btnGenerateReport.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/gen_report.png"));
FormData fd_btnGenerateReport = new FormData();
fd_btnGenerateReport.top = new FormAttachment(0, 10);
fd_btnGenerateReport.left = new FormAttachment(0, 10);
fd_btnGenerateReport.right = new FormAttachment(100, -28);
btnGenerateReport.setLayoutData(fd_btnGenerateReport);
btnGenerateReport.setText("Generate Report");
xpndtmReports.setHeight(100);
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}
Can anyone suggest a solution. Thanks in advance.
And Finally I've figured the problem in the code. The composite in the base layout was assigned with the StackLayout as follows.
Composite content = new Composite(shell, SWT.BORDER);
content.setLayout(new StackLayout());
content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
When I changed the StackLayout to GridLayout, I got a working solution.
Composite content = new Composite(shell, SWT.BORDER);
content.setLayout(new GridLayout(1, false));
content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

I need help, as I've tried to make the quantity work

Can someone help me on my problem regarding on the Quantity as it will not display in my bill(JTextArea) and i've already fixed my printwriter problem
Here is the code below:
`
So,please i need everyone's help regarding on the Quantity issue
package gb.dhaneBonz.ChezDanielle.Main;
//Chez Danielle Bistro System
//importations and the heading of this System.
import java.awt.*;
import java.awt.event.*;
import java.io.FileWriter;
import java.io.PrintWriter;
import javax.swing.*;
import javax.swing.event.*;
//The menuChoicesItems and menuChoicesPrices have worked together
//with the bill(JList) in order to display it
public class ChezDanielle extends JFrame implements ActionListener
{
static String[] menuChoicesItems =
{"Andouillette (pork offal sausage) 120.00",
"Pot au feu (Lyonnais pot roast) 80.75",
"Reinsdyrsteik (Reindeer roast) 250.30",
"Bordeaux Superieur Wine 250.00",
"Crispy Pata 380.00",
"Cabernet Sauvignon Wine 180.40",
"Pain Perdu(French Toast)65.00",
"French Fries(1 bucket) 55.50",
"Chicharon 34.00",
"Coffee(Cappuccino) 40.90",
"Latte Coffee 35.00",
"Iced Tea(Bottomless) 40.00",
"Leche Flan 55.75",
"Maja Blanca 250.00",
"Bistek Tagalog 87.00",
"Buko Pie 67.00",
"Coq au Vin(Chicken with red wine soup) 180.00",
"Ice Cream Box 230,00",
"Baileys Whisky 380.00",
"Durian Shake(Bottomless) 60.00",
"Creme Brulee 230.00",};
static double[] menuChoicesPrices = {120.00, 80.75, 250.30, 250.00, 380.00,
180.40, 65.00, 55.50, 34.00,
40.00, 35.00, 40.00, 55.75, 250.00, 87.00, 67.00, 180.00, 230.00, 380.00, 60.00, 230.00};
private JList menuChoices;
private JTextArea bill;
private Container pane;
public ChezDanielle()
{
super("ChezDanielle Bistro Simplice");
setResizable(false);
setIconImage(Toolkit.getDefaultToolkit().getImage(ChezDanielle.class.getResource("/Images/Icon.png")));
//Colours and Layouts of this programme.
pane = getContentPane();
pane.setBackground(new Color(255, 20, 147));
pane.setLayout(new BorderLayout(5, 5));
//Label placement is on the NORTH and its parameters below.
JLabel menuChoicesJLabel = new JLabel("Le Menu");
menuChoicesJLabel.setForeground(Color.WHITE);
menuChoicesJLabel.setHorizontalAlignment(SwingConstants.LEFT);
pane.add(menuChoicesJLabel,BorderLayout.NORTH);
menuChoicesJLabel.setFont(new Font("Segoe UI", Font.BOLD, 20));
//Menu is on the WEST and its parameters below.
menuChoices = new JList(menuChoicesItems);
menuChoices.setForeground(Color.BLACK);
menuChoices.setBackground(Color.WHITE);
JScrollPane scrollPane = new JScrollPane (menuChoices);
pane.add(scrollPane,BorderLayout.WEST);
menuChoices.setFont(new Font("Segoe UI", Font.BOLD, 14));
//Receipt area is on the EAST and its parameters below.
bill = new JTextArea();
pane.add(bill,BorderLayout.EAST);
bill.setFont(new Font("Segoe UI", Font.PLAIN, 14));
//Button is on the SOUTH and its parameters below in order to
//make it work.
JButton button = new JButton("Please Select and Order");
button.setFont(new Font("Segoe UI", Font.PLAIN, 11));
setSize(747, 484);
pane.add(button,BorderLayout.SOUTH);
button.addActionListener(this);
setSize(747, 484);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
//Method for quantity
private void quantity()
{
final double[] quantity = {0,1,2,3,10,20};
Double.parseDouble(JOptionPane.showInputDialog(null,"Enter Quantity: "+quantity));
}
//Method codes to display the order and the total cost.
private void displayBill()
{
int[] listArray = menuChoices.getSelectedIndices();
double salesTax = 0.065;
double quantity = 0;
double tax;
double subTotal = 0;
double total;
//Receipt area's extra parameters.
bill.setEditable(false);
bill.setText("");
//Total and initialisation of the programme.
for (int index = 0; index < listArray.length; index++)
subTotal = subTotal
+ menuChoicesPrices,quantity[listArray[index]];
tax = salesTax * subTotal;
total = subTotal + tax;
//This Displays the costs of the order.
bill.append("---------------------Chez Danielle--------------------------\n");
bill.append("---------------------Our Branches:--------------------------\n");
bill.append("------Paris Davao London Edinburgh Oslo Marseille-----------\n");
bill.append("----Website: www.chezdanielle.co.uk | Phone No: 440-9087----\n\n");
for (int index = 0; index < listArray.length; index++)
{
bill.append(menuChoicesItems,quantity[listArray[index]]+ "\n");
}
bill.append("\n");
bill.append("SUB TOTAL\t\tPHP "
+ String.format("%.2f", subTotal) + "\n");
bill.append("TAX \t\tPHP "
+ String.format("%.2f", tax) + "\n");
bill.append("TOTAL \t\tPHP "
+ String.format("%.2f", total) + "\n\n");
bill.append("Bonjour, Merci - Have a Nice Day\n\n");
//Resets the receipt array in order to pave way for the new order(Loop).
menuChoices.clearSelection();
repaint();
}
public void actionPerformed(ActionEvent event)
{
if (event.getActionCommand().equals("Please Select and Order"))
quantity();
outFile();
displayBill();
}
public static void main(String[] args)
{
ChezDanielle alc = new ChezDanielle();
}
//Method for the TextFile
private void outFile()
{
try{
PrintWriter printW=new PrintWriter(new FileWriter("Receipt.txt",true));
printW.println(""+bill.getText());
printW.close();
JOptionPane.showMessageDialog(null, new JTextArea("Thank You for Choosing Chez Danielle!"));
}catch (Exception e) {
JOptionPane.showInternalMessageDialog(null, e.getMessage());
}
}
}
Any Ideas on how to make it work?
It's already fixed and its okay already
package gb.dhaneBonz.ChezDanielle.Main;
//Chez Danielle Bistro System
//importations and the heading of this System.
import java.awt.*;
import java.awt.event.*;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.Arrays;
import javax.swing.*;
import javax.swing.event.*;
//The menuChoicesItems and menuChoicesPrices have worked together due to array methods.
//with the bill(JList) in order to display it
public class ChezDanielle extends JFrame implements ActionListener
{
static String[] menuChoicesItems =
{"Andouillette (pork offal sausage) 120.00",
"Pot au feu (Lyonnais pot roast) 80.75",
"Reinsdyrsteik (Reindeer roast) 250.30",
"Bordeaux Superieur Wine 250.00",
"Crispy Pata 380.00",
"Cabernet Sauvignon Wine 180.40",
"Pain Perdu(French Toast)65.00",
"French Fries(1 bucket) 55.50",
"Chicharon 34.00",
"Coffee(Cappuccino) 40.90",
"Latte Coffee 35.00",
"Iced Tea(Bottomless) 40.00",
"Leche Flan 55.75",
"Maja Blanca 250.00",
"Bistek Tagalog 87.00",
"Buko Pie 67.00",
"Coq au Vin(Chicken with red wine soup) 180.00",
"Ice Cream Box 230,00",
"Baileys Whisky 380.00",
"Durian Shake(Bottomless) 60.00",
"Creme Brulee 230.00",};
static double[] menuChoicesPrices = {120.00, 80.75, 250.30, 250.00, 380.00,
180.40, 65.00, 55.50, 34.00,
40.90, 35.00, 40.00, 55.75, 250.00, 87.00, 67.00, 180.00, 230.00, 380.00, 60.00, 230.00};
private JList menuChoices;
private JTextArea bill;
private Container pane;
public ChezDanielle()
{
super("ChezDanielle Bistro Simplice");
setResizable(false);
setIconImage(Toolkit.getDefaultToolkit().getImage(ChezDanielle.class.getResource("/Images/Icon.png")));
//Colours and Layouts of this programme.
pane = getContentPane();
pane.setBackground(new Color(255, 20, 147));
pane.setLayout(new BorderLayout(5, 5));
// the Label placement is on the NORTH and its parameters below.
JLabel menuChoicesJLabel = new JLabel("Le Menu");
menuChoicesJLabel.setForeground(Color.WHITE);
menuChoicesJLabel.setHorizontalAlignment(SwingConstants.LEFT);
pane.add(menuChoicesJLabel,BorderLayout.NORTH);
menuChoicesJLabel.setFont(new Font("Segoe UI", Font.BOLD, 20));
// the menuChoicesItems is on the WEST and its parameters below.
menuChoices = new JList(menuChoicesItems);
menuChoices.setForeground(Color.BLACK);
menuChoices.setBackground(Color.WHITE);
JScrollPane scrollPane = new JScrollPane (menuChoices);
pane.add(scrollPane,BorderLayout.WEST);
menuChoices.setFont(new Font("Segoe UI", Font.BOLD, 14));
//I've placed the receipt area is on the EAST and its parameters below.
bill = new JTextArea();
pane.add(bill,BorderLayout.EAST);
bill.setFont(new Font("Segoe UI", Font.PLAIN, 14));
//Button is on the SOUTH and its parameters below in order to
//make it work.
JButton button = new JButton("Please Select and Order");
button.setFont(new Font("Segoe UI", Font.PLAIN, 11));
setSize(747, 484);
pane.add(button,BorderLayout.SOUTH);
button.addActionListener(this);
setSize(747, 484);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
//Method codes for quantity
private void quantity()
{
}
//Method codes to display the order and the total cost.
private void displayBill()
{
JOptionPane.showMessageDialog(null, new JTextArea("Thank You for Choosing Chez Danielle!"));
int[] quantity = new int[0];
int choiceLength = menuChoices.getSelectedIndices().length;
for(int i = 0; i < choiceLength; i++){
// JOptionPane.showMessageDialog(null, menuChoices.getSelectedValues());
int qty = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Quantity of "+menuChoices.getSelectedValues()[i]));
quantity = Arrays.copyOf(quantity, quantity.length + 1);
quantity[quantity.length - 1] = qty;
}
int[] listArray = menuChoices.getSelectedIndices();
double salesTax = 0.065;
//int quantity ;
double tax;
double subTotal = 0;
double total;
double itemTotal;
//Receipt area's extra parameters.
bill.setEditable(false);
bill.setText("");
//Total and initialisation of the programme.
for (int index = 0; index < listArray.length; index++)
{
itemTotal = menuChoicesPrices[listArray[index]] * quantity[index];
subTotal = subTotal + itemTotal;
}
tax = salesTax * subTotal;
total = subTotal + tax ;
//This Displays the costs of the order.
bill.append("---------------------Chez Danielle--------------------------\n");
bill.append("---------------------Our Branches:--------------------------\n");
bill.append("------Paris Davao London Edinburgh Oslo Marseille-----------\n");
bill.append("----Website: www.chezdanielle.co.uk | Phone No: 440-9087----\n\n");
for (int index = 0; index < listArray.length; index++)
{
bill.append(menuChoicesItems[listArray[index]]+ " x "+ quantity[index]+" /Order"+"\n");
}
bill.append("\n");
bill.append("SUB TOTAL\t\tPHP "
+ String.format("%.2f", subTotal) + "\n");
bill.append("TAX \t\tPHP "
+ String.format("%.2f", tax) + "\n");
bill.append("TOTAL \t\tPHP "
+ String.format("%.2f", total) + "\n\n");
bill.append("Bonjour, Merci - Have a Nice Day\n\n");
//Resets the receipt array in order to pave way for the new order(Loop).
menuChoices.clearSelection();
repaint();
}
public void actionPerformed(ActionEvent event)
{
if (event.getActionCommand().equals("Please Select and Order"))
quantity();
outFile();
displayBill();
}
public static void main(String[] args)
{
ChezDanielle alc = new ChezDanielle();
}
//Method for the TextFile
private void outFile()
{
try{
PrintWriter printW=new PrintWriter(new FileWriter("Receipt.txt",true));
printW.println(""+bill.getText());
printW.close();
}catch (Exception e) {
JOptionPane.showInternalMessageDialog(null, e.getMessage());
}
}
}

JTextArea, JMenuBar, JMenu, JMenuItem not showing up

I am quite new to Java so I need some help. I am trying to make a Notepad application.
The problem is that none of my menus or textfield is showing up. I cannot figure out what the problem is. Please help.
public class NotePad extends JFrame implements ActionListener {
private JTextArea txtArea;
private JMenuBar mnuBar;
private JMenu mnyFile, mnyFormat, mnyEdit, mnyHelp;
private JMenuItem openFile, saveFile, exit, textWrap, noTextWrap, clear, abtNotepad;
public NotePad() {
setTitle("NOTEPAD");
setSize(700, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
//Tekstboks
txtArea = new JTextArea();
//MenyBar
mnuBar = new JMenuBar();
//Meny
mnyFile = new JMenu("File");
mnyFormat = new JMenu("Format");
mnyEdit = new JMenu("Edit");
mnyHelp = new JMenu("Help");
//UnderMeny
openFile = new JMenuItem("Open");
saveFile = new JMenuItem("Save");
exit = new JMenuItem("Exit");
textWrap = new JMenuItem("Text Wrap");
noTextWrap = new JMenuItem("No Text Wrap");
clear = new JMenuItem("Clear");
abtNotepad = new JMenuItem("About Notepad");
add(txtArea);
add(mnuBar);
add(mnyFile);
add(mnyFormat);
add(mnyEdit);
add(mnyHelp);
add(openFile);
add(saveFile);
add(exit);
add(textWrap);
add(noTextWrap);
add(clear);
add(abtNotepad);
setJMenuBar(mnuBar);
setVisible(true);
}
public static void main(String[] args) {
new NotePad();
}
public void actionPerformed(ActionEvent e) {
}
}
Your constructor should look something like:
public NotePad() {
setTitle("NOTEPAD");
setSize(700, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
txtArea = new JTextArea();
mnuBar = new JMenuBar();
mnyFile = new JMenu("File");
mnyFormat = new JMenu("Format");
mnyEdit = new JMenu("Edit");
mnyHelp = new JMenu("Help");
openFile = new JMenuItem("Open");
saveFile = new JMenuItem("Save");
exit = new JMenuItem("Exit");
textWrap = new JMenuItem("Text Wrap");
noTextWrap = new JMenuItem("No Text Wrap");
clear = new JMenuItem("Clear");
abtNotepad = new JMenuItem("About Notepad");
mnuBar.add(mnyFile);
mnuBar.add(mnyFormat);
mnuBar.add(mnyEdit);
mnuBar.add(mnyHelp);
mnyFile.add(openFile);
mnyFile.add(saveFile);
mnyFile.add(exit);
mnyFormat.add(textWrap);
mnyFormat.add(noTextWrap);
mnyEdit.add(clear);
mnyHelp.add(abtNotepad);
setJMenuBar(mnuBar);
add(txtArea);
setVisible(true);
}
Otherwise you are overriding each component you add to BorderLayout.

Trying to set the size/location of a button in a tab for SWT

This should be pretty simple but this is the first time I've worked with SWT. This is what I have so far.
public class TabsTest {
private Shell shell;
private CTabFolder folder;
public TabsTest(Display display){
shell = new Shell(display);
shell.setText("TabsTest");
shell.setLayout(new FillLayout());
CTabFolder folder = new CTabFolder(shell, SWT.CLOSE | SWT.BOTTOM);
folder.setUnselectedCloseVisible(false);
folder.setSimple(false);
initUI(folder);
shell.pack();
shell.setBounds(500, 500, 400, 500);
shell.open ();
while(!shell.isDisposed()){
if(!display.readAndDispatch())
display.sleep();
}
}
public void initUI(CTabFolder folder){
CTabItem NFL = new CTabItem(folder, SWT.NONE);
NFL.setText("NFL Bets");
Button okButton = new Button(folder, SWT.PUSH);
okButton.setText("OK");
okButton.setSize(10,10);
NFL.setControl(okButton);
CTabItem NBA = new CTabItem(folder,SWT.NONE);
NBA.setText("NBA Bets");
CTabItem CFB = new CTabItem(folder,SWT.NONE);
CFB.setText("CFB Bets");
folder.setSize(800,500);
}
public static void main (String [] args) {
Display display = new Display();
new TabsTest(display);
display.dispose();
}
}
What this currently gives me is this....
How would I make this a small button in the bottom right corner? Or just in general make it smaller and move it somewhere.
Since you are using a FillLayout the control takes up the entire space available. What you need is a different kind of a layout. I will suggest you to read this article, it will be a good start.
I generally prefer GridLayout as it is quite easy to use and it fulfills most needs.
Edited: Modifying your code to use GridLayout
public class TabsTest {
private Shell shell;
private CTabFolder folder;
public TabsTest(Display display) {
shell = new Shell(display);
shell.setText("TabsTest");
shell.setLayout(new GridLayout());
CTabFolder folder = new CTabFolder(shell, SWT.CLOSE | SWT.BOTTOM);
folder.setUnselectedCloseVisible(false);
folder.setSimple(false);
folder.setLayoutData(new GridData(GridData.FILL_BOTH));
initUI(folder);
shell.pack();
shell.setBounds(500, 500, 400, 500);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
public void initUI(CTabFolder folder) {
CTabItem NFL = new CTabItem(folder, SWT.NONE);
NFL.setText("NFL Bets");
Composite nflParent = new Composite(folder, SWT.NONE);
nflParent.setBackground(folder.getDisplay().getSystemColor(SWT.COLOR_BLUE));
nflParent.setLayout(new GridLayout());
Button okButton = new Button(nflParent, SWT.PUSH);
okButton.setText("OK");
GridData gd = new GridData();
gd.verticalAlignment = GridData.END;
gd.horizontalAlignment = GridData.END;
gd.grabExcessHorizontalSpace = true;
gd.grabExcessVerticalSpace = true;
okButton.setLayoutData(gd);
NFL.setControl(nflParent);
CTabItem NBA = new CTabItem(folder, SWT.NONE);
NBA.setText("NBA Bets");
CTabItem CFB = new CTabItem(folder, SWT.NONE);
CFB.setText("CFB Bets");
folder.setSize(800, 500);
}
public static void main(String[] args) {
Display display = new Display();
new TabsTest(display);
display.dispose();
}
}

The barchart(jfreechart) is displayed as small icon on a composite in a view of Eclipse RCP plugin

The barchart is displayed as small icon on a composite of a view in Eclipse RCP plugin. The chart does not cover the entire composite which should be the actual case. what additional setting needs to be made in code to display the graph on entire composite
Following is the code for displaying the bargraph
final CategoryDataset dataset = createDataset();
final JFreeChart chart = createChart(dataset);
if(flag == false){
frame.dispose();
}
frame = new ChartComposite(barchartComposite,SWT.NONE,chart,true);
frame.setLayoutData(new GridData(GridData.FILL_BOTH));
frame.setChart(chart);
frame.forceRedraw();
frame.pack();
frame.setVisible(true);
flag= false;
The method createDataset() generates the data for the barchart and method createChart(dataset) generates the barchart.
THE COMPLETE SOURCE CODE FOR DISPLAY OF VIEW
public class BarChartDisplay extends ViewPart {
Text searchfield = null;
String path = SelectDataBase.path;
public static int error=0;
public static int info=0;
public static int critical=0;
public static int warning=0;
ChartComposite frame;
boolean flag=true;
public BarChartDisplay() {
}
#Override
public void createPartControl(Composite parent) {
//Composite A:
final Composite mainComposite = new Composite(parent, SWT.NONE);
GridData mainLayoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
mainLayoutData.horizontalSpan = 1;
mainComposite.setLayoutData(mainLayoutData);
GridLayout outerLayout = new GridLayout();
outerLayout.marginTop = 30;
outerLayout.marginLeft = 20;
outerLayout.marginRight = 20;
mainComposite.setLayout(new GridLayout(1, false));
//Composite B:
final Composite selectComposite = new Composite(mainComposite, SWT.NONE);
selectComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
selectComposite.setLayout(new GridLayout(4, false));
//Composite C:
final Composite barchartComposite = new Composite(mainComposite, SWT.NONE);
barchartComposite.setLayout(new GridLayout(1, false));
barchartComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final CalendarCombo ccombo = new CalendarCombo(selectComposite, SWT.READ_ONLY | SWT.FLAT);
GridData layoutDataCal = new GridData(150, 40);
ccombo.computeSize(SWT.DEFAULT, SWT.DEFAULT);
ccombo.showCalendar();
ccombo.setLayoutData(layoutDataCal);
org.eclipse.swt.widgets.Button button = new org.eclipse.swt.widgets.Button(selectComposite, SWT.PUSH);
button.setText("Go");
button.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
switch (e.type) {
case SWT.Selection:
error = 0;
info = 0;
warning = 0;
critical = 0;
DB db = new DB();
Connection conn = null;
conn = db.ConnTable(path);
Statement statement;
try {
statement = conn.createStatement();
String query = null;
String textfielddata = ccombo.getDateAsString();
System.out.println(textfielddata);
query = "select priority from log where creation_date = '"+ textfielddata +"'";
System.out.println(query);
ResultSet rs = statement.executeQuery(query);
while (rs.next()) {
int prioritydata = rs.getInt("priority");
if (prioritydata == 1)
error++;
else if (prioritydata == 2)
info++;
else if (prioritydata == 3)
warning++;
else if (prioritydata == 4)
critical++;
}
} catch (SQLException er) {
er.printStackTrace();
}
final CategoryDataset dataset = createDataset();
final JFreeChart chart = createChart(dataset);
if(flag == false){
frame.dispose();
}
frame = new ChartComposite(barchartComposite,SWT.BORDER,chart,true);
frame.setLayoutData(new GridData(GridData.FILL_BOTH));
frame.setChart(chart);
frame.forceRedraw();
frame.pack();
frame.setVisible(true);
flag= false;
break;
}
}
});
}
/**
* Returns a sample dataset.
*
* #return The dataset.
*/
private CategoryDataset createDataset() {
// row keys...
final String series1 = "First";
// column keys...
final String category1 = "error";
final String category2 = "info";
final String category3 = "warning";
final String category4 = "critical";
// create the dataset...
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(error, series1, category1);
dataset.addValue(info, series1, category2);
dataset.addValue(warning, series1, category3);
dataset.addValue(critical, series1, category4);
return dataset;
}
/**
* Creates a sample chart.
*
* #param dataset the dataset.
*
* #return The chart.
*/
private JFreeChart createChart(final CategoryDataset dataset) {
// create the chart...
final JFreeChart chart = ChartFactory.createBarChart(
"Priority BarChart", // chart title
"priority", // domain axis label
"Value", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips?
false // URLs?
);
// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
// set the background color for the chart...
chart.setBackgroundPaint(Color.white);
// get a reference to the plot for further customisation...
final CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
// set the range axis to display integers only...
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
// disable bar outlines...
final BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setDrawBarOutline(false);
// set up gradient paints for series...
final GradientPaint gp0 = new GradientPaint(
0.0f, 0.0f, Color.blue,
0.0f, 0.0f, Color.lightGray
);
renderer.setSeriesPaint(0, gp0);
final CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(
CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)
);
// OPTIONAL CUSTOMISATION COMPLETED.
return chart;
}
#Override
public void setFocus() {
}
}
You have to modify the parent composite named barchartComposite.
parent.setLayout(new GridLayout(1, false));
parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final CategoryDataset dataset = createDataset();
final JFreeChart chart = createChart(dataset);
Composite barchartComposite = new Composite(parent, SWT.NONE);
barchartComposite.setLayout(new GridLayout(1, false));
barchartComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
ChartComposite frame = new ChartComposite(barchartComposite, SWT.BORDER,chart,true);
frame.setLayoutData(new GridData(GridData.FILL_BOTH));
You have to make sure that barchartcomposite grabs the wohle space of the parent composite. This can be achieved with GridLayout and GridData.
You can find a very useful tutorial about all SWT layouts here:
Understanding Layouts in SWT