JScrollPane, JTable and horizontal scroll

I was coding a graphical interface using Java, and what I wanted was to insert a JTable object inside a JScrollPane. The table was bigger than the JScrollPane, but I didnt know why, only the vertical scroll bar was shown. I spent hours reading Swing tutorials, checking my code… everything seemed to be ok, so.. why it wasnt working?
Finally, I found the answer: there is a bug in Java when you are using JScrollPane and JTable. And the solution was to create a new class extending JTable, in order to override JTable.getScrollableTracksViewportWidth() method with this one:

public boolean getScrollableTracksViewportWidth() {

if (autoResizeMode != AUTO_RESIZE_OFF) {

if (getParent() instanceof JViewport) {

return (((JViewport)getParent()).getWidth() > getPreferredSize().width);

}

}

return false;

}

Source: DaniWeb Forum

The Class java.util.Properties

I didnt know this class before start using it some months ago. Now I love it because it makes easier the handling of properties in your Java code.

public class Properties extends Hashtable {

protected Properties defaults;
public Properties();
public Properties(Properties defaults);
public String getProperty(String key);
public String setProperty(String key, String defaultValue);
public Enumeration propertyNames();
public void load(InputStream in) throws IOException;
public void save(OutputStream out, String header);
public void list(PrintStream out);

}

It extends java.util.HashTable, so this class stores each property as a pair of (key, value). If your application needs some parameters to work, you can include them in a file, and then, use a java.util.Properties object to load them.

Properties props = new Properties();
FileInputStream file = new FileInputStream(”propertiesfile.txt”);
props.load(file)

Using these three lines, you will have a java.util.Properties object filled with the content of “propertiesfile.txt”. But, whats the format of this file? It has to contain a pair per line, and both of them have to be separated by a white space, ‘=” or ‘:’. The following file would be correct:

Property1=Value1
#This is a comment
Property2:Value2
Property3 Value3

After loading this file, how can we access the properties? If we want to use the value of “Property1″:

String value = getProperty(”Property1″);

Or if we want to modify the value of “Property2″:

setProperty(”Property2″,”New value”);

More info: java.util.Properties

Powered by WordPress with GimpStyle Theme design by Horacio Bella.
Entries and comments feeds. Valid XHTML and CSS.