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]

[5848 byte] By [allydma] at [2007-11-26 22:00:30]
# 1

First of all learn to use the Java naming standards:

a) classes are upper cased: GetFarms

b) method and variables are mixed case: getFarmNumber(), numberFarms....

Don't use an array to read data into from your file. You don't know how many entries will be in the file. Simply use a Vector of Vectors. So you create a "data" Vector. Then for every line you read from the file you create another Vector and add this Vector to the data vector. Then you build your table using the data Vector and a Vector containing your column names.

Not sure why the layout looks wierd. If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.

camickra at 2007-7-10 4:00:26 > top of Java-index,Desktop,Core GUI APIs...
# 2

Don't forget to use the Code Formatting Tags so the posted code retains its original formatting.

>> I believe i have

Not sure why the layout looks wierd. If you need further help then you need to create a Short, Self Contained, Compilable and Executable, Example Program (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

>> I will create one, if someone isn't able to pick up on the error later on.

Don't use an array to read data into from your file. You don't know how many entries will be in the file. Simply use a Vector of Vectors. So you create a "data" Vector. Then for every line you read from the file you create another Vector and add this Vector to the data vector. Then you build your table using the data Vector and a Vector containing your column names.

>> I controll that website, and the data on it, the number of columns remains constant and i created a function to count the number of rows, so that i know how many to expect and handle them. Although i'm not sure how that relates to my problem.

Anybody have any suggestions as to why i might be getting the outlined problem?

Thanks.

allydma at 2007-7-10 4:00:26 > top of Java-index,Desktop,Core GUI APIs...
# 3

>> I believe i have

That was a reminder for the SSCCE you will be posting in the future.

>> I will create one, if someone isn't able to pick up on the error later on.

We are not mind readers. We don't know what layout manager you are using for the main panel so we don't know why it looks like it does. Maybe someone will be able to make a wild guess, but why should we waste time guessing when you can take 5 minutes to create a SSCCE that demonstrates what you are doing.

>> Although i'm not sure how that relates to my problem.

It doesn't. Its just a better design. Don't hard code values into your program.

In fact in this particular case, the coding is actually simpler if you just use a Vector. No need for the "how many lines" method. You just have a simple loop that reads a line and parses the data.

camickra at 2007-7-10 4:00:26 > top of Java-index,Desktop,Core GUI APIs...
# 4
Hi.As i was coding a mock up i found a solution to my problem. Thanks for the above help.
allydma at 2007-7-10 4:00:26 > top of Java-index,Desktop,Core GUI APIs...
# 5
> Although i'm not sure how that relates to my problem.Which is why I always ask for a SSCCE with my standard response.When you simplify the problem you find the answer most times. If you don't then you still have something to post for everyone to see.
camickra at 2007-7-10 4:00:26 > top of Java-index,Desktop,Core GUI APIs...