Hi Friends I am new to Java Swings. I am trying to add JTable. But the table is not displaying. I have the following java code (2 files)
****************************************************************************************
package edu.ucsc.genome.qa.sigGateWay;
import java.io.*;
import java.util.*;
import java.awt.Color;
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.JTableHeader;
import edu.ucsc.genome.qa.sigData.KnownGene;
import edu.ucsc.genome.qa.sigGateWay.KnownGeneHtml;
public class KnownGeneGui extends JFrame
{
JPanel topPane = new JPanel();
JPanel downPane = new JPanel();
Container contentPane;
JLabel label;
JScrollPane scrollPaneDown = new JScrollPane();
JButton nButton = new JButton("North");
JButton sButton = new JButton("South");
JButton eButton = new JButton("East");
JButton wButton = new JButton("West");
JComboBox comboBox = new JComboBox();
private String sqlQuery[] = {"sql_1","sql_2","sql_3","sql_4","sql_5","sql_6"};
public KnownGeneGui()
{
contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
comboBox = new JComboBox(sqlQuery);
comboBox.setMaximumRowCount(4);
topPane.setMaximumSize(new java.awt.Dimension(1000,200));
topPane.setMinimumSize(new java.awt.Dimension(1000,200));
topPane.setPreferredSize(new java.awt.Dimension(1000,200));
topPane.setBorder(BorderFactory.createLineBorder(Color.black));
topPane.add(comboBox);
// add topPane to contentPane - not JFrame itself
contentPane.add(topPane, BorderLayout.NORTH);
label = new JLabel(sqlQuery[0]);
contentPane.add(label);
// Size scrollPaneDown - leftPane size doesn't matter since it's the scrollable view
scrollPaneDown.setMaximumSize(new java.awt.Dimension(1000, 700));
scrollPaneDown.setMinimumSize((new java.awt.Dimension(1000, 700)));
scrollPaneDown.setPreferredSize(new java.awt.Dimension(1000, 700));
scrollPaneDown.setBorder(BorderFactory.createLineBorder(Color.blue));
scrollPaneDown.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPaneDown.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
// set downPane as the scrollable component
//scrollPaneDown.setViewportView(downPane);
//downPane.add(scrollPaneDown);
ItemListenerImpl itemListener = new ItemListenerImpl();
comboBox.addItemListener(itemListener);
downPane.add(scrollPaneDown);
// important to call revalidate here so leftPane will notify scrollPaneLeft to update accordingly
downPane.revalidate();
contentPane.add(downPane, BorderLayout.SOUTH);
}
private class ItemListenerImpl implements ItemListener
{
public void itemStateChanged(ItemEvent ievent)
{
if(ievent.getStateChange() == ItemEvent.SELECTED)
{
//label.setText(sqlQuery[comboBox.getSelectedIndex()]);
try
{
KnownGeneCgiTable kgTable = new KnownGeneCgiTable();
if(kgTable != null)
{
if(comboBox.getSelectedIndex() == 1)
{
//Place Components in Container
label.setText(" Your query is processing! Pl wait...Do not close the window!!!");
JTable table = kgTable.getKnownGeneCgiTable();
scrollPaneDown.add(table);
}
else if(comboBox.getSelectedIndex() != 1)
{
label.setText("Select the correct query");
}
}
else
{
scrollPaneDown.add(new JButton("Error in table ...."));
}
}
catch(Exception e)
{
}
}
}
}
public static void main(String args[])
{
KnownGeneGui g = new KnownGeneGui();
g.setSize(1000,750);
g.setVisible(true);
g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
**************************************************************************************
package edu.ucsc.genome.qa.sigGateWay;
import java.io.*;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;;
import edu.ucsc.genome.qa.sigData.KnownGene;
import edu.ucsc.genome.qa.sigSql.KnownGeneList;
public class KnownGeneCgiTable extends JFrame
{
public static JTable getKnownGeneCgiTable() throws Exception
{
List<KnownGene> kgList = KnownGeneList.getAllKnownGenes();
int total = kgList.size();
String data[][] = new String[total][3];
String headers[] = {"Known Gene Name","Chromosome","Protein ID"};
if(kgList != null && kgList.size() > 0)
{
int i = 0;
Iterator it = kgList.iterator();
while(it.hasNext())
{
KnownGene kg = (KnownGene) it.next();
data[i][0] = kg.getName();
data[i][1] = kg.getChrom();
data[i][5] = kg.getProteinId();
i++;
}
}
return new JTable(new DefaultTableModel(data, headers));
}
}