kevinharper
October 14th, 2012, 04:46 AM
First time really creating a GUI on my own and I'm excited. It took me most of the day to read tutorials and get something somewhat created. I will post the code and then a few questions. I am also fairly new to Object-oriented programming.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FrontEnd {
/**
* Generate & Display GUI.
*/
private static void GUI() {
//window
JFrame window = new JFrame("Kevin Harper's CSV Reader");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
window.setSize(1100, 650);
window.setResizable(false);
JPanel panel = new JPanel();
window.getContentPane().add(panel);
panel.setLayout(null);
//TOP (open button, save button, & search)
JButton openButton = new JButton("Open CSV File");
openButton.setBounds(1,1,150,20);
openButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
JButton saveButton = new JButton("Save Search Results");
saveButton.setBounds(157,1,200,20);
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
final JTextField searchField = new JTextField("Search ID or Description", 250);
/***
* Code for clearing searchField on focus and replacing text when
* not in focus taken from:
* http://stackoverflow.com/questions/5382436/text-box-cleared-when-receives-focus
**/
searchField.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent arg0) {
searchField.setText("Search ID or Description");
}
@Override
public void focusGained(FocusEvent arg0) {
// TODO Auto-generated method stub
searchField.setText("");
}
});
searchField.setBounds(738, 1, 250, 20);
JButton searchButton = new JButton("Search");
searchButton.setBounds(989,1,100,20);
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
//MIDDLE (data table)
String[] tableHeader = {"ID", "Description", "Color", "Qty.", "Price"};
Object[][] tableData = {
{new Integer(1), "Screw", "Yellow", new Integer(10), new Float(0.50)},
{new Integer(2), "Scres Driver", "Yellow", new Integer(4), new Float(10.50)},
{new Integer(3), "TV", "Black", new Integer(100), new Float(1000)},
{new Integer(4), "Screw", "Yellow", new Integer(10), new Float(0.50)},
{new Integer(5), "Scres Driver", "Yellow", new Integer(4), new Float(10.50)},
{new Integer(6), "TV", "Black", new Integer(100), new Float(1000)},
{new Integer(7), "Screw", "Yellow", new Integer(10), new Float(0.50)},
{new Integer(8), "Scres Driver", "Yellow", new Integer(4), new Float(10.50)},
{new Integer(9), "TV", "Black", new Integer(100), new Float(1000)},
{new Integer(10), "Screw", "Yellow", new Integer(10), new Float(0.50)},
{new Integer(11), "Scres Driver", "Yellow", new Integer(4), new Float(10.50)},
{new Integer(12), "TV", "Black", new Integer(100), new Float(1000)},
{new Integer(13), "Screw", "Yellow", new Integer(10), new Float(0.50)},
{new Integer(14), "Scres Driver", "Yellow", new Integer(4), new Float(10.50)},
{new Integer(15), "TV", "Black", new Integer(100), new Float(1000)},
{new Integer(16), "Screw", "Yellow", new Integer(10), new Float(0.50)},
{new Integer(17), "Scres Driver", "Yellow", new Integer(4), new Float(10.50)},
{new Integer(18), "TV", "Black", new Integer(100), new Float(1000)},
{new Integer(19), "Screw", "Yellow", new Integer(10), new Float(0.50)},
{new Integer(20), "Scres Driver", "Yellow", new Integer(4), new Float(10.50)},
{new Integer(21), "TV", "Black", new Integer(100), new Float(1000)},
{new Integer(22), "Screw", "Yellow", new Integer(10), new Float(0.50)},
{new Integer(23), "Scres Driver", "Yellow", new Integer(4), new Float(10.50)},
{new Integer(24), "TV", "Black", new Integer(100), new Float(1000)},
{new Integer(25), "Screw", "Yellow", new Integer(10), new Float(0.50)},
{new Integer(26), "TV", "Black", new Integer(100), new Float(1000)},
{new Integer(27), "Screw", "Yellow", new Integer(10), new Float(0.50)},
{new Integer(28), "Scres Driver", "Yellow", new Integer(4), new Float(10.50)},
{new Integer(29), "TV", "Black", new Integer(100), new Float(1000)},
{new Integer(30), "Screw", "Yellow", new Integer(10), new Float(0.50)}
};
JTable resultsTable = new JTable(tableData, tableHeader);
JScrollPane scrollPane = new JScrollPane(resultsTable);
resultsTable.setPreferredScrollableViewportSize(ne w Dimension(1000, 503));
resultsTable.setFillsViewportHeight(true);
scrollPane.setBounds(1, 40, 1090, 503);
//BOTTOM (result tracker/count, page tracker)
JLabel totals = new JLabel("Total:");
totals.setBounds(570, 545, 100, 20);
JLabel qtyTotal = new JLabel("QTY_TOTAL");
qtyTotal.setBounds(661, 545, 100, 20);
JLabel priceTotal = new JLabel("PRICE_TOTAL");
priceTotal.setBounds(881, 545, 100, 20);
JLabel resultsString = new JLabel("Viewing Records 61 - 90 of 5,000");
resultsString.setBounds(300, 575, 200, 20);
JLabel pagesString = new JLabel("Pages: << 1 | 2 | 3 | 4 | 5 >>");
pagesString.setBounds(660,575,200,20);
panel.add(openButton);
panel.add(saveButton);
panel.add(searchField);
panel.add(searchButton);
panel.add(scrollPane);
panel.add(totals);
panel.add(qtyTotal);
panel.add(priceTotal);
panel.add(resultsString);
panel.add(pagesString);
//center window
window.setLocationRelativeTo(null);
//view window
window.setVisible(true);
}
public static void main(String[] args) {
//Create & show GUI
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
GUI();
}
});
}
}
I made the open, save, and search buttons close the window just to confirm that I had created them correctly.
My next step is to have the "Open" button pop up a file system navigation window and populate the table that way.
My questions:
1) Should the table and the text at the bottom each have it's own object class? This is information that gets generated dynamically and it needs to be repainted (right?).
2) Should I have my main program here? I will read a file and save the results to an arraylist. That array list will populate my first table. The user will then have the option to do a search. The search results will then populate the table.
I'm just not sure if this class should be my start class.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FrontEnd {
/**
* Generate & Display GUI.
*/
private static void GUI() {
//window
JFrame window = new JFrame("Kevin Harper's CSV Reader");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
window.setSize(1100, 650);
window.setResizable(false);
JPanel panel = new JPanel();
window.getContentPane().add(panel);
panel.setLayout(null);
//TOP (open button, save button, & search)
JButton openButton = new JButton("Open CSV File");
openButton.setBounds(1,1,150,20);
openButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
JButton saveButton = new JButton("Save Search Results");
saveButton.setBounds(157,1,200,20);
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
final JTextField searchField = new JTextField("Search ID or Description", 250);
/***
* Code for clearing searchField on focus and replacing text when
* not in focus taken from:
* http://stackoverflow.com/questions/5382436/text-box-cleared-when-receives-focus
**/
searchField.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent arg0) {
searchField.setText("Search ID or Description");
}
@Override
public void focusGained(FocusEvent arg0) {
// TODO Auto-generated method stub
searchField.setText("");
}
});
searchField.setBounds(738, 1, 250, 20);
JButton searchButton = new JButton("Search");
searchButton.setBounds(989,1,100,20);
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
//MIDDLE (data table)
String[] tableHeader = {"ID", "Description", "Color", "Qty.", "Price"};
Object[][] tableData = {
{new Integer(1), "Screw", "Yellow", new Integer(10), new Float(0.50)},
{new Integer(2), "Scres Driver", "Yellow", new Integer(4), new Float(10.50)},
{new Integer(3), "TV", "Black", new Integer(100), new Float(1000)},
{new Integer(4), "Screw", "Yellow", new Integer(10), new Float(0.50)},
{new Integer(5), "Scres Driver", "Yellow", new Integer(4), new Float(10.50)},
{new Integer(6), "TV", "Black", new Integer(100), new Float(1000)},
{new Integer(7), "Screw", "Yellow", new Integer(10), new Float(0.50)},
{new Integer(8), "Scres Driver", "Yellow", new Integer(4), new Float(10.50)},
{new Integer(9), "TV", "Black", new Integer(100), new Float(1000)},
{new Integer(10), "Screw", "Yellow", new Integer(10), new Float(0.50)},
{new Integer(11), "Scres Driver", "Yellow", new Integer(4), new Float(10.50)},
{new Integer(12), "TV", "Black", new Integer(100), new Float(1000)},
{new Integer(13), "Screw", "Yellow", new Integer(10), new Float(0.50)},
{new Integer(14), "Scres Driver", "Yellow", new Integer(4), new Float(10.50)},
{new Integer(15), "TV", "Black", new Integer(100), new Float(1000)},
{new Integer(16), "Screw", "Yellow", new Integer(10), new Float(0.50)},
{new Integer(17), "Scres Driver", "Yellow", new Integer(4), new Float(10.50)},
{new Integer(18), "TV", "Black", new Integer(100), new Float(1000)},
{new Integer(19), "Screw", "Yellow", new Integer(10), new Float(0.50)},
{new Integer(20), "Scres Driver", "Yellow", new Integer(4), new Float(10.50)},
{new Integer(21), "TV", "Black", new Integer(100), new Float(1000)},
{new Integer(22), "Screw", "Yellow", new Integer(10), new Float(0.50)},
{new Integer(23), "Scres Driver", "Yellow", new Integer(4), new Float(10.50)},
{new Integer(24), "TV", "Black", new Integer(100), new Float(1000)},
{new Integer(25), "Screw", "Yellow", new Integer(10), new Float(0.50)},
{new Integer(26), "TV", "Black", new Integer(100), new Float(1000)},
{new Integer(27), "Screw", "Yellow", new Integer(10), new Float(0.50)},
{new Integer(28), "Scres Driver", "Yellow", new Integer(4), new Float(10.50)},
{new Integer(29), "TV", "Black", new Integer(100), new Float(1000)},
{new Integer(30), "Screw", "Yellow", new Integer(10), new Float(0.50)}
};
JTable resultsTable = new JTable(tableData, tableHeader);
JScrollPane scrollPane = new JScrollPane(resultsTable);
resultsTable.setPreferredScrollableViewportSize(ne w Dimension(1000, 503));
resultsTable.setFillsViewportHeight(true);
scrollPane.setBounds(1, 40, 1090, 503);
//BOTTOM (result tracker/count, page tracker)
JLabel totals = new JLabel("Total:");
totals.setBounds(570, 545, 100, 20);
JLabel qtyTotal = new JLabel("QTY_TOTAL");
qtyTotal.setBounds(661, 545, 100, 20);
JLabel priceTotal = new JLabel("PRICE_TOTAL");
priceTotal.setBounds(881, 545, 100, 20);
JLabel resultsString = new JLabel("Viewing Records 61 - 90 of 5,000");
resultsString.setBounds(300, 575, 200, 20);
JLabel pagesString = new JLabel("Pages: << 1 | 2 | 3 | 4 | 5 >>");
pagesString.setBounds(660,575,200,20);
panel.add(openButton);
panel.add(saveButton);
panel.add(searchField);
panel.add(searchButton);
panel.add(scrollPane);
panel.add(totals);
panel.add(qtyTotal);
panel.add(priceTotal);
panel.add(resultsString);
panel.add(pagesString);
//center window
window.setLocationRelativeTo(null);
//view window
window.setVisible(true);
}
public static void main(String[] args) {
//Create & show GUI
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
GUI();
}
});
}
}
I made the open, save, and search buttons close the window just to confirm that I had created them correctly.
My next step is to have the "Open" button pop up a file system navigation window and populate the table that way.
My questions:
1) Should the table and the text at the bottom each have it's own object class? This is information that gets generated dynamically and it needs to be repainted (right?).
2) Should I have my main program here? I will read a file and save the results to an arraylist. That array list will populate my first table. The user will then have the option to do a search. The search results will then populate the table.
I'm just not sure if this class should be my start class.