I'm currently working on a Java program and having an issue with the program not displaying anything.
Within the main method is the following code:
WindowClient client = new WindowClient();
client.pack();
client.setVisible(true);
And WindowClient:
import javax.swing.JFrame;
import java.awt.GridBagLayout;
import javax.swing.SpringLayout;
import org.datacontract.schemas._2004._07.NaturalDisasterService.NaturalDisaster;
import org.tempuri.INaturalDisasterServiceProxy;
import java.awt.BorderLayout;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.awt.event.ActionEvent;
public class WindowClient extends JFrame {
private INaturalDisasterServiceProxy ndsp;
private JTextField textDisasterName; // text box on insertPanel for disaster names
private JTextField textDisasterDescription; // text box on insertPanel for disaster descriptions
public JPanel insertPanel, detailPanel;
public JList list; // the list
public JLabel lblDisasterNameStaticInsert, //displays text "Disaster Name:" on insertPanel
lblDisasterDescStaticInsert, // displays text "Disaster Description:" on insertPanel
lblDisasterDescDataDetail, // displays disaster description from database on detailPanel
lblDisasterDescStaticDetail; // label displays text "Disaster Desc:" on detailPanel
public WindowClient() {
buildWindow();
ndsp = new INaturalDisasterServiceProxy();
ndsp.setEndpoint("url redacted");
updateList();
}
/**
* @param args
*/
public void getSelection() {
showDetailPanel();
}
public void insertLoad() {
if (detailPanel.isVisible()) {
hideDetailPanel();
}
showInsertPanel();
}
public void saveToDB() {
hideInsertPanel();
}
public void updateList() {
NaturalDisaster nds[];
try {
nds = ndsp.getData();
DefaultListModel model = new DefaultListModel();
for(NaturalDisaster disaster : nds){
model.addElement(disaster.getDisasterName());
}
getContentPane().remove(list);
list = new JList(model);
list.setBounds(81, 11, 247, 132);
getContentPane().add(list);
getContentPane().repaint();
getContentPane().revalidate();
} catch (RemoteException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
//e.printStackTrace();
}
}
public void buildWindow() {
getContentPane().setLayout(null);
list = new JList();
list.setBounds(81, 11, 247, 132);
getContentPane().add(list);
insertPanel = new JPanel();
insertPanel.setBounds(10, 150, 414, 67);
getContentPane().add(insertPanel);
insertPanel.setLayout(null);
detailPanel = new JPanel();
detailPanel.setBounds(10, 150, 414, 67);
detailPanel.setLayout(null);
// Label for disaster names on insertPanel
lblDisasterNameStaticInsert = new JLabel("Disaster Name:");
lblDisasterNameStaticInsert.setFont(new Font("Tahoma", Font.BOLD, 11));
lblDisasterNameStaticInsert.setBounds(10, 5, 105, 14);
insertPanel.add(lblDisasterNameStaticInsert);
// label for disaster descripitons on the insertPanel
lblDisasterDescStaticInsert = new JLabel("Disaster Description:");
lblDisasterDescStaticInsert.setFont(new Font("Tahoma", Font.BOLD, 11));
lblDisasterDescStaticInsert.setBounds(10, 30, 120, 14);
insertPanel.add(lblDisasterDescStaticInsert);
// text boxes on the insertPanel
textDisasterName = new JTextField();
textDisasterName.setBounds(138, 2, 266, 20);
insertPanel.add(textDisasterName);
textDisasterName.setColumns(10);
textDisasterDescription = new JTextField();
textDisasterDescription.setBounds(138, 27, 266, 20);
insertPanel.add(textDisasterDescription);
textDisasterDescription.setColumns(10);
getContentPane().add(detailPanel);
// label for the disaster description from database on display panel
lblDisasterDescDataDetail = new JLabel("New label");
lblDisasterDescDataDetail.setBounds(10, 30, 120, 14);
detailPanel.add(lblDisasterDescDataDetail);
// label displays text "Disaster Desc:" on display panel
lblDisasterDescStaticDetail = new JLabel("Disaster Desc:");
lblDisasterDescStaticDetail.setBounds(10, 5, 105, 14);
detailPanel.add(lblDisasterDescStaticDetail);
JButton btnGetSelection = new JButton("Get Selection");
btnGetSelection.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
getSelection();
}
});
btnGetSelection.setBounds(10, 228, 105, 23);
getContentPane().add(btnGetSelection);
JButton btnInsert = new JButton("Insert");
btnInsert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
insertLoad();
}
});
btnInsert.setBounds(125, 228, 124, 23);
getContentPane().add(btnInsert);
JButton btnSave = new JButton("Save Info to DB");
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveToDB();
}
});
btnSave.setBounds(259, 228, 165, 23);
getContentPane().add(btnSave);
//getContentPane().setVisible(true);
}
public void hideInsertPanel() {
getContentPane().remove(insertPanel);
insertPanel.setVisible(false);
}
public void showInsertPanel() {
getContentPane().add(insertPanel);
insertPanel.setVisible(true);
insertPanel.getParent().revalidate();
}
public void hideDetailPanel() {
getContentPane().remove(detailPanel);
detailPanel.setVisible(false);
}
public void showDetailPanel() {
getContentPane().add(detailPanel);
detailPanel.setVisible(true);
detailPanel.getParent().revalidate();
}
}
Some of the other questions on the issue of JFrame malfunction mention the need to setVisible()
and pack()
, but I have made both of these calls. The only result is that the program runs, and a program does open in Windows task bar, but does not hav a corresponding window (the preview displays a thin white bar that doesn't display if the program is selected as active).
While not the primary issue, this is my first work with Swing in a few years, so feel free to point out other mistakes.