Layout Problem
[nobr]Hi.
I have a class which creates a Jtable and a Jbutton, and when i run that class it appears as i want it to:
Picture 1 here: http://www.allydm.co.uk/pictures/problem1.jpg
However, when i call it in a Jpane, by using this code:
FarmList.add(new GetFarms());
It appears like this:
Picture 2 here: http://www.allydm.co.uk/pictures/problem2.jpg
The code for the Jpanel in which its being called is:
javax.swing.JPanel FarmList;
FarmList =new javax.swing.JPanel();
FarmList.setBorder(javax.swing.BorderFactory.createTitledBorder(null,"Farm List", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION,new java.awt.Font("Tahoma", 1, 11)));
FarmList.setPreferredSize(new Dimension(446, 317));
FarmList.add(new GetFarms());
Can anyone see why the layout is being changed?
Incase the code for class is relevant, it is as follows:
publicclass GetFarmsextends JPanel{
publicstaticvoid main(String[] args)throws Exception{
JFrame frame =new JFrame("Covenant Farm List");
frame.add(new GetFarms());
frame.setSize(450,300);
frame.setVisible(true);
}
publicint GetNumberFarms()throws MalformedURLException, IOException{
URL theUrl =new URL("http://www.allydm.co.uk/covenant/total_farms.php");
BufferedReader in =new BufferedReader(
new InputStreamReader(
theUrl.openStream()));
String NumberFarms = in.readLine();
int NumberFarmsRaw = Integer.parseInt(NumberFarms);
return NumberFarmsRaw;
}
privatefinalint COLUMNS = 4;
privateint ROWS = GetNumberFarms();
private JTable sampleJTable;
private String[][] cells =new String[ROWS][COLUMNS];
public GetFarms()throws Exception{
URL theUrl =new URL("http://www.allydm.co.uk/Covenant/farm_list.php");
BufferedReader in =new BufferedReader(
new InputStreamReader(
theUrl.openStream()));
String inputLine = in.readLine();
String[] lines = inputLine.split("<br>");
for (int i = 0; i < ROWS; i++){
String[] stuff = lines[i].split(" ");
for (int j = 0; j < COLUMNS; j++){
cells[i][j] = stuff[j];
}
}
in.close();
final String[]columnNames ={"Username","DA","Sentry","Last Update"};
sampleJTable =new JTable(cells, columnNames);
JScrollPane tablePane =new JScrollPane(sampleJTable);
tablePane.setPreferredSize(new Dimension(420, 200));
add(tablePane, BorderLayout.CENTER);
javax.swing.JButton RefreshFarms;
RefreshFarms =new javax.swing.JButton();
RefreshFarms.setText("Refresh Farms");
RefreshFarms.setDefaultCapable(false);
RefreshFarms.setFocusable(false);
add(RefreshFarms, BorderLayout.PAGE_END);
RefreshFarms.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent evt){
try{
sampleJTable.setModel(new DefaultTableModel(cells, columnNames));
}catch (Exception ex){
ex.printStackTrace();
}
}
});
}
}
Thanks for any help with this[/nobr]

