Topic: Java Code Repository

Offline Tiwaking!

  • Hero Member
  • Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!
  • Posts: 12,585
This thread is where you can find or post useful Java information, code snippets, tips or tricks.

I'll start with an amazingly useful tool:

Stephen G Wares incredible Sound to Class tool which turns a sound file to an independent, thread based .java file!
You should also check out the Image to Class file which is just below the previous link


My own contributions to this thread are:

Working with JApplets: Your guide to frustration ville

Applets and JApplets are horrible. They hate you. They laugh at your efforts to tame them and it just isnt worth bothering to use them


HOWEVER: If you are stubborn like me, and want a quick, easily distributed, NON-INTRUSIVE, SECURE and platform-independent method of getting your program out and about then they are a Godsend. A God which hates you and considers your efforts to spread your program to be pitiful at best, but a godsend nonetheless.


For starters: If you need file WRITE access, forget it

You will need to use CGI scripts to do so. If the server hosting your Applet has disabled cgi, forget it.


If you need file READ access, prepare for pain

Despite everything you read on the internets, you CAN initialise a FILE() object. HOWEVER:

You cannot read it and
You cannot write to it

This is akin to buying a dirty magazine and finding there is no cover art and the pages are solidly stuck together with some unidentifiable substance. It is about as disappointing too


What you need to do is to CHANGE your STRING file/folder path name into an INPUTSTREAM object BASED on the pathname. This is done as follows (spoilered to prevent cluttered thread):

Code: [Select]
BufferedInputStream inputStream = new BufferedInputStream(
this.getClass().getResourceAsStream(path));


Note that the getResourceAsStream() method returns a BufferedInputStream type

Now here is the fun bit: If you need a STRING from the file (like a filename or foldername)

Code: [Select]
public void convertStreamToString(InputStream is,String path){
    if (is != null) {
        StringBuilder sb = new StringBuilder();
        String line;
        ArrayListarray=new ArrayList();
            try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                while ((line = reader.readLine()) != null) {
                    sb.append(line).append("\n");
                }//while line=reader.ReadLine
                is.close();
            }catch(IOException in){
                System.err.println(in.toString(),"Error in Convert Stream To String!");
            }//catch
        }//if is != null
        System.out.print(sb.toString());
}//method

Simple? Well. Not really. If you need to get a list of all the FILES in a directory....well. That can be a story for another day


You can also retrieve IMAGES via the InputStream method as well
Code: [Select]
private ImageIcon loadImage(String path) {
    int MAX_IMAGE_SIZE = 240000;  //Change this to the size of
                                 //your biggest image, in bytes.
    int count=0;
    BufferedInputStream imgStream = new BufferedInputStream(
       this.getClass().getResourceAsStream(path));
            if (imgStream != null) {
                byte buf[] = new byte[MAX_IMAGE_SIZE];
                        try {
                            count = imgStream.read(buf);
                            imgStream.close();
                        } catch (java.io.IOException ioe) {
                        System.err.println("Couldn't read stream from file: " + path);
                        return null;
                        }
                if (count <= 0) {
                        System.err.println("Empty file: " + path);
                        return null;
                }
                return new ImageIcon(Toolkit.getDefaultToolkit().createImage(buf));
                } else {
                        System.err.println("Couldn't find file: " + path);
                        return null;
                }//clse
}//method

In summation: Never forget that nextInt() will leave an empty string behind!

Edit: Edited with code tags and proper formatting

Posted: March 28, 2010, 08:05:57 pm
I am now banned from GetSome

Codex

  • Guest
If you're not being paid lots of monies then you're being wasted

that syntax hurts my brain let alone trying to comprehend it

Reply #1 Posted: March 28, 2010, 11:49:54 pm

Offline Tiwaking!

  • Hero Member
  • Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!
  • Posts: 12,585
Quote from: Codex;1092618
If you're not being paid lots of monies then you're being wasted
I'd point you in the direction of Bell. He's the man when it comes to Java.

Its been forever since a bedroom programmer led the way in the development of anything great
Quote from: Codex;1092618
that syntax hurts my brain let alone trying to comprehend it

Compared to the old procedural languages, Java is incredibly easy to learn. Almost too easy, which is a problem to people who are familiar with C and C++ but not C# (which is basically Java)

The only thing you really need is a compiler. If you dont like command line compiling then you can use the easy to use program I use, BlueJ!

http://www.bluej.org/download/download.html

There is an exceptionally powerful and useful tool called Eclipse as well. I dont use Eclipse at home and begrudgingly use it in class since it is the only way to do Android/Nexus One development programs easily

http://www.eclipse.org/downloads/

I would not advise using this unless you are very familiar with Object Orientated Programming and Java.

A simple Java program: Printing a line of text

public class HelloCodex{
//beginning class bracey

public static void main(String[]args){
//this bracey denotes the beginning of a method. All braceys need a beginning bracey and an end bracey

System.out.println("Hello Codex! We must meet in TF2 one day");

//Note that all lines processable code must end with a semi-colon (;) Lines
//Thusly you could have a single line of code which spans multiple 'lines' but terminates with semi-colon

}
//main method, the 'executable' you could say

}
//class <- note that the // denotes a comment. This will not be processed

Reply #2 Posted: March 29, 2010, 10:48:41 am
I am now banned from GetSome

Offline gat

  • Just settled in
  • gat has no influence.
  • Posts: 548
i heard xeno was pr0 at java..

Reply #3 Posted: March 29, 2010, 10:51:24 am

Offline Apostrophe Spacemonkey

  • Fuck this title in particular.

  • Apostrophe Spacemonkey is awe-inspiring!Apostrophe Spacemonkey is awe-inspiring!Apostrophe Spacemonkey is awe-inspiring!Apostrophe Spacemonkey is awe-inspiring!Apostrophe Spacemonkey is awe-inspiring!Apostrophe Spacemonkey is awe-inspiring!Apostrophe Spacemonkey is awe-inspiring!Apostrophe Spacemonkey is awe-inspiring!Apostrophe Spacemonkey is awe-inspiring!Apostrophe Spacemonkey is awe-inspiring!Apostrophe Spacemonkey is awe-inspiring!Apostrophe Spacemonkey is awe-inspiring!
  • Posts: 19,050
Quote from: Codex;1092618
If you're not being paid lots of monies then you're being wasted

that syntax hurts my brain let alone trying to comprehend it

The Java / C# Syntax is nice, but it looks much nicer when it's formatted and highlighted.

It would be good to have some kind of code highlighting for the forums, for the two people who post code.

Code: [Select]
  public static List&lt;Tag&gt; GetCards(int loginID, int groupID)
        {
            Timereg tDx = GetConnection();

            int userID = CheckForValidLoginSession(loginID);

            //Retrive list of staff
            var Results = from c in tDx.CardTag
                          where c.GroupID == groupID
                          select c;
           
            List&lt;Tag&gt; siteList = new List&lt;Tag&gt;();

            foreach (CardTag cTag in Results)
            {
                Tag nSite = new Tag(cTag);                
                siteList.Add(nSite);
            }
            return siteList;
        }

Reply #4 Posted: March 29, 2010, 10:55:08 am

Offline Pyromanik

  • Hero Member
  • Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!
  • Posts: 28,834
Tiwa, please please please use the [noparse]
Code: [Select]
[/noparse] tags!

If you're posting php, syntax highlighting (shit though it is) is supported through the [php] tag.
Syntax is similar, so you could see how you get on using it for java. It'll probably be fine (especially since the highlighting is quite poor, as I mentioned).

Reply #5 Posted: March 29, 2010, 04:11:36 pm
Everyone needs more Bruce Campbell.

Offline Tiwaking!

  • Hero Member
  • Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!
  • Posts: 12,585
Quote from: Pyromanik;1092978
Tiwa, please please please use the [noparse]
Code: [Select]
[/noparse] tags!

Ahh I didnt even know the CODE tag existed!

The evils of the Scanner

I discovered something very interesting today: Do not use the java.util.Scanner in your programs!

When used on Android phones it slows down to a crawl. We are talking 1 - 3 minute boot up times for text files which have 150 lines in them

The exceptionally boring and unhelpful description of the Scanner is found here

However in the real world, the scanner acts like this
Quote

Hi guys,
I want to use Scanner class to read a list of floats from a text file

It seems however that this is incredibly slow (it took 30 minutes to
read 10,000 floats)

This appears to be the reason why my JApplets run/load incredibly slowly too. They have been fixed now

What you SHOULD use instead
Warp back ten years to 2000 and use the old way of splitting strings:
Code: [Select]
String line = &quot;This Line Is Split Using Spaces&quot;;
String[]newLine=line.split(&quot; &quot;);

This splits up the line using SPACE as the delimiter and creates and Array of Strings. In this case it would look like:

newLine[0] is "This"
newLine[1] is "Line"
newLine[2] is "Is"
etc

To read a line from a file without using the scanner you will need to use the BufferedReader object as follows:
Code: [Select]
BufferedReader br=new BufferedReader(new FileReader("yourFile.txt"));
String line;
    while((line=br.readLine())!=null){
        System.out.println(line);
    }

In conclusion: java.util.Scanner is NOT your friend. Avoid it

Reply #6 Posted: March 29, 2010, 05:31:45 pm
I am now banned from GetSome

Offline Pyromanik

  • Hero Member
  • Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!
  • Posts: 28,834
I didn't even know scanner existed, I've always just used input streams and readers.

Reply #7 Posted: March 29, 2010, 05:55:24 pm
Everyone needs more Bruce Campbell.

Offline Bell

  • Addicted
  • Bell is on the verge of being accepted.Bell is on the verge of being accepted.Bell is on the verge of being accepted.Bell is on the verge of being accepted.Bell is on the verge of being accepted.
  • Posts: 4,263
Quote from: Tiwaking!;1092758
I'd point you in the direction of Bell. He's the man when it comes to Java.


I've only done a paper or 2 in java 4-5 years ago, I'm pretty much a noob.
I always tried to do C++ while I was studying, even tho Ken kept pushing me to do java heh.

Oh and I hope you liked my little Q and A time at SIT heh.
Ken always drags me into the classroom everytime I go to say hi.

Reply #8 Posted: April 03, 2010, 05:05:42 pm

Offline Tiwaking!

  • Hero Member
  • Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!
  • Posts: 12,585
Quote from: Bell;1095470
Oh and I hope you liked my little Q and A time at SIT heh.
Ken always drags me into the classroom everytime I go to say hi.

Ken didnt even warn us you were coming AND you guys came on the worst day:

The last class of the last day of school and the second least liked class (First being SAAD with Phillip Chan)

Next time you are down, I will show you the 2d fighting game I have made into which I have animated myself. It should be fully ready (two characters, full sounds, full animations) in a week and then a week of testing. Hell if I am really on the ball, I might be able to convert it into a JApplet and stick it on my website. I havent figured out how to use a KeyListener on a JApplet yet. The Applet loses focus to the webpage it is on so never allows key input


ANYWAY, on topic:

Reading a Directory: The Java Way!

Reading a directory is ridiculously simple in Java. Make a File which is the name of the directory, then getFiles()
Code: [Select]
File directory=new File(&quot;c:\program files\Java&quot;);
String[]files=directory.getFiles()

This will list everything in the directory. Want to know if what you have got isFile() or isDirectory()? Simply use those two methods which will return TRUE if they are a FILE or a DIRECTORY


You can utilise this technique any time you need to make a list of files or objects during runtime. By listing everything in a directory holding all your CLASS files, you can make an array of Objects which exist inside a directory

Enjoy!

Reply #9 Posted: April 03, 2010, 09:40:07 pm
I am now banned from GetSome

Offline mattnz

  • Hero Member
  • mattnz is working their way up.mattnz is working their way up.mattnz is working their way up.
  • Posts: 10,004
Quote from: Tiwaking!;1092758
The only thing you really need is a compiler. If you dont like command line compiling then you can use the easy to use program I use, BlueJ!

http://www.bluej.org/download/download.html

There is an exceptionally powerful and useful tool called Eclipse as well. I dont use Eclipse at home and begrudgingly use it in class since it is the only way to do Android/Nexus One development programs easily

http://www.eclipse.org/downloads/

I would not advise using this unless you are very familiar with Object Orientated Programming and Java.

 
BlueJ is only easy because it does things for you that you should be doing yourself to really understand how it all works.

Eclipse is the man, so extensible as well. I even use it for PHP development, yus!!

Reply #10 Posted: April 04, 2010, 11:42:20 am
Now that you have read this, plz give me neg rep :>

Offline Apostrophe Spacemonkey

  • Fuck this title in particular.

  • Apostrophe Spacemonkey is awe-inspiring!Apostrophe Spacemonkey is awe-inspiring!Apostrophe Spacemonkey is awe-inspiring!Apostrophe Spacemonkey is awe-inspiring!Apostrophe Spacemonkey is awe-inspiring!Apostrophe Spacemonkey is awe-inspiring!Apostrophe Spacemonkey is awe-inspiring!Apostrophe Spacemonkey is awe-inspiring!Apostrophe Spacemonkey is awe-inspiring!Apostrophe Spacemonkey is awe-inspiring!Apostrophe Spacemonkey is awe-inspiring!Apostrophe Spacemonkey is awe-inspiring!
  • Posts: 19,050
I used Eclipse for my Java programming, it was pretty good.

Reply #11 Posted: April 04, 2010, 07:59:00 pm

Offline mattnz

  • Hero Member
  • mattnz is working their way up.mattnz is working their way up.mattnz is working their way up.
  • Posts: 10,004
Fun fact: The name 'Eclipse' is a dig at Sun

Reply #12 Posted: April 05, 2010, 04:56:38 pm
Now that you have read this, plz give me neg rep :>

Offline Tiwaking!

  • Hero Member
  • Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!
  • Posts: 12,585
Previously on Tiwaking! gives you bad advice...
Quote from: Tiwaking!;1053986
This appears to be the reason why my JApplets run/load incredibly slowly too. They have been fixed now

What you SHOULD use instead
Warp back ten years to 2000 and use the old way of splitting strings:
Code: [Select]
String line = &quot;This Line Is Split Using Spaces&quot;;
String&#91;]newLine=line.split(&quot; &quot;);
This splits up the line using SPACE as the delimiter and creates and Array of Strings. In this case it would look like:

newLine[0] is "This"
newLine[1] is "Line"
newLine[2] is "Is"
etc
Well it turns out this is horrifically wrong.

the split() command is INCREDIBLY slow in an emulation or android environment. Apparently it searches the line multiple time for the markers which is fine for our desktops, but for limited environments: Bad news.


Replace all uses of split("STRING","REPLACEMENT_STRING") with a loop which iterates, for example:
Code: [Select]
[s]line.split("/","")[/s]with
Code: [Select]
newLine=line.substring(line.indexOf("/"));And use an interator

Reply #13 Posted: May 07, 2010, 08:33:24 pm
I am now banned from GetSome

Offline AintNoMeInTeam

  • Devoted Member
  • AintNoMeInTeam has no influence.
  • Posts: 1,792
was going to post some shit here, but i think i deleted all my java projects...

had some neat stuff like recursively traversing the the contents of a folder and its subfolders and building a balanced binary tree from the contents to do some quick searches

Reply #14 Posted: May 08, 2010, 10:43:39 am
Quote
Nobody will ever win the battle of the sexes...there\'s too much fraternizing with the enemy.
-Henry Kissinger

Offline Tiwaking!

  • Hero Member
  • Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!
  • Posts: 12,585
This post is a warning to anyone attempting serialization in Java and does not contain code. Actual code will appear at a later date
Serialization and Java

Serialisation is the 'streaming' of objects, squishing them into a transmittable/compact form.

After 18 hours of programming I managed to master the art of this horrible, horrible concept. There are FOUR VERY BAD THINGS you have to learn endure.

Warning one

You MUST declare the ObjectOutputStream BEFORE otherwise your entire program will deadlock with no errors and no warning.


Warning two

You MUST flush the ObjectOutputStream before doing ANYTHING else.


Warning three

You MUST flush the ObjectOutputStream ALWAYS after sending an object.


Warning four

This one took me four hours to figure out.

You CANNOT make a Data Member('Global Variable' to you non-Javaese) of a Serializable class.

You can disagree with me about this last warning, but I promise you: I will laugh at you constantly when you repeatedly fail, ESPECIALLY when it takes less than six characters to completely corrupt your program.


Conclusion

There are two other things you have to do before serialization can occur.

1. The class you wish to serialize must implement java.io.Serializable.
1 a) Warning: Serialized classes cannot recursively call one another or be dependent on another class. Well, they can. But unless you've mastered the fiddly bastards then I wouldnt advise attempting it.

2. ObjectOutputStream and ObjectInputStream are required for the passing of Objects out and in. ObjectInputStream.readObject() is a blocking call which will not execute until it recieves input.
2 a) These two streams will completely hog the resources available from a Socket. You cant send objects and text chat along the same stream (unless someone knows of a way you can).



Please be warned and be program safe!

Reply #15 Posted: May 20, 2010, 04:58:56 pm
I am now banned from GetSome

Offline Pyromanik

  • Hero Member
  • Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!Pyromanik is awe-inspiring!
  • Posts: 28,834
Serialization a great way to pass things around.
Just have to remember that it essentially needs to compress EVERY object that is associated with the one you're trying to save/send.
Everything must be nice, not just whatever implements Serializable.

Reply #16 Posted: May 20, 2010, 05:54:28 pm
Everyone needs more Bruce Campbell.

Offline Tiwaking!

  • Hero Member
  • Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!
  • Posts: 12,585
In the tradition of discovering incredibly weird or obscure errors:

JTabbedPane dangers

It took an hour before I fixed it (a simple try/catch) but I managed to generate an impossible error today.

What is the difference between this:
Code: [Select]
public class ClientTabbedPane extends JTabbedPane{
  public void add(ClientTextPanel panel){
    add(&quot;I am a panel&quot;,panel);
  }
}
And this:
Code: [Select]
public class ClientTabbedPane extends JTabbedPane{
  public void add(ClientDisplayPanel panel){
    add("I am a panel",panel);
  }
}

Answer?

If you said "One is a ClientDisplayPanel and one is a ClientTextPanel" then you get half a mark.

The real answer is:

Code snippet two throws an impossible to throw error!

Notably THIS error:

java.lang.ArrayIndexOutOfBoundsException: -1

This error is impossible to throw. The reason a JTabbedPane would throw this error is if you:

1. Try to remove a component from index -1
2. Select a tab which is at index -1, which is an impossibility because tab -1 is the index of a non-existent tab.

Note that I have ADDED a component, not removed one.

the fix is:
Code: [Select]
public class ClientTabbedPane extends JTabbedPane{
  public void add(ClientDisplayPanel panel){
    try{
      add("I am a panel",panel);
    }catch(Exception ignoreStupidError){}
  }
}
The panel is added. The impossible error is thrown. Program continues.

Cripes Java can be screen-punchingly frustrating sometimes.

Edit: Wow.

The program goes, but sometimes if the tab creation is a wee bit too slow it throws a 'non-existent tab' error.

Urge to kill: Rising

Reply #17 Posted: May 21, 2010, 04:24:54 pm
I am now banned from GetSome

Offline Tiwaking!

  • Hero Member
  • Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!
  • Posts: 12,585
Weeks of struggling with these weird things have finally been solved.
JTables - How do they work?

JTables are the greatest creation in the history of the Java universe. They are also the least understood with the most frustrating application programming interface and incredibly unhelpful 'help' and 'how to use' from the Sun Java site.

In short:
1. Setup Column Names. e.g String[]columnNames={"Age","Sex","Location"};
2. Create a javax.swing.DefaultTableModel for the JTable.
3. Add the Columns/Fields to the DefaultTableModel.
4. Create a new JTable containing the DefaultTableModel.
Code: [Select]
/**
 * This method creates the columns for a table given an Array of Column Names
 */
public static DefaultTableModel createTable(String[]cn){
DefaultTableModel dtm=new DefaultTableModel();
for(int i=0;i<cn.length;i++){
dtm.addColumn(cn[i]);
}//4 column names
return dtm;
}//me

Followed by:
JTable table=new JTable(createTable(columnNames));


And DONE! Table DONE! You will now have a blank Table. BUT: There are two very serious problems.

1. The Table Column Names are not displayed!! :sad face:
2. Table is empty.

To fix problem (1) you must, Must, MUST put the Table into a JScrollPane. I dont know why, but you just have to.

JScrollPane scroll=new JScrollPane(table);

Then you must add this to whatever Container (JPanel or whatever) you are using. DO NOT add the JTable directly to the container.

My Table is still empty!

This is easy to fix. Grab the DefaultTableModel out of the JTable and add a row to it like so:
Code: [Select]

DefaultTableModel dtm=(DefaultTableModel)table.getModel();
Object[]rowData={new Integer(106),"Male","GetSome Forums"};
dtm.addRow(rowData);

And you are DONE! You can even put whole Class Objects into the row data, but be aware that a Table is not 'connected' in anyway. If you have, say, a row which has a Computer and a Computer price, updating the Computer objects price will not update the price displayed in the Computer Price column.


This makes updating abit of a pain, BUT if you are using the JTable as a display for a database then you should already be updating the table as soon as any changes are made. Have fun!

Reply #18 Posted: April 15, 2011, 10:39:30 pm
I am now banned from GetSome

Offline Tiwaking!

  • Hero Member
  • Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!
  • Posts: 12,585
A few rules about Applets
1. Dont use JApplets.
2. Dont use Canvas.
3. If you break either rule 1 or rule 2, do not use Canvas with JApplet, use java.applet.Applet instead.
4. You cannot read directories.
5. If you need to read a directory, dont bother. It is a massive headache inducing task.
6. If you break rule 4 or rule 5 then use getCodeBase() and append a URL or "\\" or "\\\" or "\\\\" to the required filepath.

Finally: Always try to keep files required as a CLASS file (like text files and stuff) and never read items out of a JAR file.
Consider yourself warned.

Reply #19 Posted: December 20, 2011, 02:12:40 pm
I am now banned from GetSome

Offline Bell

  • Addicted
  • Bell is on the verge of being accepted.Bell is on the verge of being accepted.Bell is on the verge of being accepted.Bell is on the verge of being accepted.Bell is on the verge of being accepted.
  • Posts: 4,263
Don't use Java

Class dismissed.

Reply #20 Posted: December 20, 2011, 02:30:13 pm

Offline Tiwaking!

  • Hero Member
  • Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!
  • Posts: 12,585
Quote from: Bell;1458210
Don't use Java


Quote from: Bell;1458210
Class dismissed.


Quote
Target is the guy everyone in a forum loves to hate. To some degree he brings this upon himself. For example, he may be a known cheater in a game forum, a conservative among liberals, a WindowsMICROSOFT guy among Mac Java[/size] enthusiasts

Reply #21 Posted: December 20, 2011, 04:28:18 pm
I am now banned from GetSome

Offline camy205

  • Addicted
  • camy205 barely matters.camy205 barely matters.
  • Posts: 3,206
Programming zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

Reply #22 Posted: December 20, 2011, 04:53:41 pm
Quote from: Craigorsarus;1484182
GetSome Thread - Generic Timeline:

 - Actual Topic
 - Variation of Topic
 - Someone calls someone a retard
 - Fight
 - Actual Topic
 - Fight
 - Troll
 - Your Mum
 - You\'re*
 - TROLOLOLOLOLOLOL
 - What is this thread about?

Offline toofast

  • Addicted
  • toofast barely matters.toofast barely matters.
  • Posts: 3,697
I didn't think such thing as a java enthusiast existed.

Reply #23 Posted: December 20, 2011, 05:19:46 pm

Offline Tiwaking!

  • Hero Member
  • Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!Tiwaking! is awe-inspiring!
  • Posts: 12,585
Applets and Timings
Please note that Applets and JApplets run at approximately five times faster than a JNLP or Desktop application, that is to say: Threads, For Loops, and delays must be increased by at least five times to achieve the same effect that the desktop tests run at.

Also: Graphical Interfaces cannot be 'restarted', nor can ArrayLists be altered in a graphical environment, even if it is an extended class. Items cannot be added into an Objects list, only removed.

That is all.

Reply #24 Posted: December 20, 2011, 09:51:42 pm
I am now banned from GetSome