I'm new to this Java programming, so my code probably looks quite basic and any explanations need to be simple. There are a couple of display things I can't get to work in the code that follows
The Initial display is just an input Text field and a button labelled "Filter". This displays fine. When I click on the button, it should display a Scrollable Table in the lower half of the display, however no data is displayed, but if I then click elsewhere, then the display appears, where it should be. If I change the filter and click button nothing happens, but again click elsewhere other than the Filter button and display appears.
Also my scroll Pane has no scroll bars when too much data to display is output, I thought these should be automatic?
---------Here is the code --------------
package com.bliss;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class LifeStageRef extends JPanel {
private JPanel pnlFilter = new JPanel();
private JPanel pnlTable = new JPanel();
private JLabel lblFilter ;
private JTextField txtFilter = new JTextField();
private JButton jButton1 = new JButton();
private Controller ctrlMain;
public LifeStageRef(Controller ctrlIn) {
super();
ctrlMain = ctrlIn;
try {
jbInit();
} catch (Exception e) {
Logging.handle(e);
e.printStackTrace();
}
}
public LifeStageRef(boolean b) {
super(b);
}
public LifeStageRef(LayoutManager layoutManager) {
super(layoutManager);
}
public LifeStageRef(LayoutManager layoutManager, boolean b) {
super(layoutManager, b);
}
private void jbInit() throws Exception {
this.setLayout(new GridLayout(2,0));
this.setBackground(new Color(247, 255, 214));
this.setBorder(BorderFactory.createLineBorder(Colo r.black, 1));
this.setSize(new Dimension(433, 296));
pnlFilter.setLayout(new GridLayout(2,0));
pnlFilter.setOpaque(false);
JPanel pnlTxtFltr = new JPanel(new FlowLayout());
lblFilter = new JLabel("Filter : ");
txtFilter = new JTextField();
txtFilter.setEnabled(true);
txtFilter.setColumns(20);
pnlTxtFltr.add(lblFilter);
pnlTxtFltr.add(txtFilter);
pnlTxtFltr.setOpaque(false);
pnlFilter.add(pnlTxtFltr, 0);
JPanel pnlbtnFltr = new JPanel(new FlowLayout());
pnlbtnFltr.setOpaque(false);
pnlbtnFltr.add(jButton1);
pnlFilter.add(pnlbtnFltr, 1);
jButton1.setText("Filter");
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
jButton1_actionPerformed(e);
} catch (Exception ex) {
Logging.handle(ex);
}
}
});
this.add(pnlFilter, 0);
}
private void jButton1_actionPerformed(ActionEvent e) {
ArrayList<tablerowData> stratdata = new ArrayList<tablerowData>();
try {
Logging.log(GConst.LOG_VERBOSE,BuildData.class.get Name(),
"Looking for file of Loaded Strategies");
InputStream in = new FileInputStream(System.getProperty("user.dir", "") +
System.getProperty("file.separator") +
Utils.getPropertyVal("properties") +
System.getProperty("file.separator") +
GConst.LOADSTRAT);
int valid = 0;
Workbook Xref;
if (Utils.getExtension(GConst.LOADSTRAT).compareToIgn oreCase("xls")==0) {
Xref = new HSSFWorkbook(in);
valid = 1 ;
}
else {if (Utils.getExtension(GConst.LOADSTRAT).compareToIgn oreCase("xlsx")==0) {
Xref = new XSSFWorkbook(in);
valid = 1 ;
}
else {
// Error as extension not recognised
Logging.log(GConst.LOG_INFO, BuildData.class.getName(),
"Error - Strategies not loaded as file extension for " + GConst.LOADSTRAT + " not recognised") ;
Xref = new HSSFWorkbook() ;
valid = 0 ;
}
}
if (valid == 1) {
Sheet Sheet = Xref.getSheet(GConst.LOADED_STRAT_PAGE);
int lastrow = Sheet.getPhysicalNumberOfRows();
if (lastrow > 0 ) {
String fileFilter = txtFilter.getText();
for (Row row : Sheet ) {
// load data from each row into an array
Cell cellrow = row.getCell(GConst.LOADED_STRAT_REF_CELL);
if (cellrow != null) {
Logging.log(GConst.LOG_VERBOSE,BuildData.class.get Name(),
"Loading data from Row - " + row.getRowNum() + " Column - " + cellrow.getColumnIndex() );
String cell0 = String.valueOf((long)cellrow.getNumericCellValue() );
cellrow = row.getCell(GConst.LOADED_STRAT_STRATEGY_CELL);
String cell1 = "" ;
if (cellrow == null) {
cell1 = "" ;
}
else {
cell1 = cellrow.getStringCellValue() ;
}
cellrow = row.getCell(GConst.LOADED_STRAT_FILENAME_CELL);
String cell2 = "" ;
if (cellrow == null) {
cell2 = "" ;
}
else {
cell2 = cellrow.getStringCellValue() ;
}
cellrow = row.getCell(GConst.LOADED_STRAT_DATE_CELL);
String cell3 = "" ;
if (cellrow == null) {
cell3 = "" ;
}
else {
cell3 = cellrow.getStringCellValue() ;
}
cellrow = row.getCell(GConst.LOADED_STRAT_QUOTE_IND);
String cell4 = "" ;
if (cellrow == null) {
cell4 = "" ;
}
else {
cell4 = cellrow.getStringCellValue() ;
}
if (cell1.toLowerCase().contains(fileFilter.toLowerCa se())) {
stratdata.add(new tablerowData(cell0, cell1, cell2, cell3, cell4));
}
}
}
}
}
in.close();
}
catch (FileNotFoundException f) {
Logging.handle(f);
}
catch (IOException f) {
Logging.handle(f);
}
pnlTable.removeAll();
pnlTable.setLayout(new GridLayout(1,0));
pnlTable.setOpaque(false);
JPanel pnltblFltr = new JPanel(new FlowLayout());
pnltblFltr.setOpaque(false);
pnltblFltr.removeAll();
JTable strattable = new JTable();
DefaultTableModel model = new DefaultTableModel();
strattable.setModel(model);
model.setRowCount(0);
model.setColumnIdentifiers(new String[] {"Strategy Ref",
"Strategy",
"Filename",
"Date Created",
"Quote?"});
for (tablerowData p : stratdata)
{
model.addRow(new String[] {p.StratRef, p.Strategy, p.Filename, p.DateCreated, p.Quote });
}
JScrollPane scrollPane = new JScrollPane(strattable);
strattable.setFillsViewportHeight(false) ;
pnltblFltr.add(scrollPane) ;
pnlTable.add(pnltblFltr);
this.add(pnlTable,1);
}
class tablerowData
{
private String StratRef ;
private String Strategy ;
private String Filename ;
private String DateCreated ;
private String Quote;
public tablerowData(String a, String b, String c, String d, String e)
{
StratRef = a ;
Strategy = b ;
Filename = c ;
DateCreated = d ;
Quote = e ;
}
}
}