Wait a minute..
public Object getElementAt(int i) { return "Task " + i; }
you can call setElementAt from here to next Tuesday, but getElementAt will always return the same value!
Wait a minute..
public Object getElementAt(int i) { return "Task " + i; }
you can call setElementAt from here to next Tuesday, but getElementAt will always return the same value!
If this were my problem I'd be forced back to brute force debugging at this stage. I'd start by adding a few prints to confirm that the model data has actually changed, and that getElementAt returns the new value.
I's also write a tiny ListDataListener and check that the update is triggering the notifications we expect.
Sorry if that's not very helpful, but when all the code looks OK there's no real substitute for step-by-step "what was it I misunderstood" brute-force checking.
Good luck!
Or maybe someone else has some better insight? Come on guys!
Sorry - missed that when scanning your code.
Did you try what I said in my previous post?
After changing your model's data you must call fireContentsChanged (inherited from AbstractListModel) to notify listeners (including the JList) that something has changed - something like
fireContentsChanged(this, i, i);
... but you should check out the API doc yourself, of course.
populate it as in my previous post (and apine's longer example). Access it like
for (String[] sa : data) { // loop thru all the string arrays in the arraylist
for (int w = 0; w < sa.length; w++) {
System.out.println(" Word " + w + ": " + sa[w]);
}
}
ps
Personally I would avoid the problem of sizing the first dim of the array by using an ArrayList, although I'd keep the array for words since that's what split gives you
ArrayList<String[]> data = new ArrayList<String[]>();
...
data.add(sentence.split(" "));
2d arrays in Java are just an array of arrays. You have arrays of words, so you can just add those to an array of sentences - roughly like this...
String[][] data = new String[99][]; // first dim big enough for all sentences
int sentenceNum = 0;
while (lineScanner.hasNext()){
String sentence = lineScanner.next();
String[] words = sentence.split(" ");
data[sentenceNum] = words;
sentenceNum++;
}
for (int s = 0; s < data.length; s++) {
System.out.println("Sentence " + s);
for (int w = 0; w < data[s].length; w++) {
System.out.println(" Word " + w + ": " + data[s][w]);
}
}
In a word - yes.
You need to call JFileChoser in response to some kind of user interaction - eg clicking a menu item.
The JFileChooser returns you a File object, so you then open some kind of output Stream (eg new ObjectOutputStream) to that File, and write your data to the Stream
Your setValue looks OK as far as it goes, but there's no way for the JList to know that you've done that, so it doesn't update itself.
After changing your model's data you must call fireContentsChanged (inherited from AbstractListModel) to notify listeners (including the JList) that something has changed - something like fireContentsChanged(this, i, i);
... but you should check out the API doc yourself, of course.
Maybe something got lost in the move from NB to E.?
Looking at the code you posted I can't see anything that initialises connectionFactory, so I'm unsurprised that connectionFactory.createConnection() gives an NPE.
You changed
public String Anglia[];
to
StringArrays Anglia = new StringArrays();
While that's not necessarily a bad thing to do, it does invalidate all the code that assumed Anglia was an array of Strings.
And, the declaration of a new Anglia variable in the constructor is still a major error - its scope is limited to the constructor and it is lost as soon as the constructor is finished.
This is going to be some silly typo thing, so
Can you post the most recent def of the StringArrays class and the full compiler error message with the exact line it's referring to?
// instance version - how to reference
StringArrays sa = new StringArrays();
for (i=0; i<sa.Anglia.length; i++)
System.out.println(sa.Anglia[i]);
OR
String[] localRefToArrayInOtherlass = new StringArrays().Anglia;
for (i=0; i<localRefToArrayInOtherlass .length; i++)
System.out.println(localRefToArrayInOtherlass[i]);
...
Your public variable is an instance variable, so you need an instance of StringArrays to access it OR you can make it static and access it via the class name, ie
// instance version - how to reference
StringArrays sa = new StringArrays();
... sa.Anglia;
// static version - how to reference
StringArrays.Anglia;
Either way you have a serious bug in your code. You declare the public array on line 11, but in the constructor (line 19) you declare another array with the same name - this "hides" the public one, and is discarded at the end of the constructor, leaving the public variable un-initialised.
OK, now I understand.
oOoKodakoOo please meet convertRowIndexToModel
convertRowIndexToModel pleased meet oOoKodakoOo
No, seriously
public int convertRowIndexToModel(int viewRowIndex)
Maps the index of the row in terms of the view to the underlying TableModel. If the contents of the model are not sorted the model and view indices are the same.
Parameters:
viewRowIndex - the index of the row in the view
Returns:
the index of the corresponding row in the model
This is the expected behaviour. What exactly is the problem you are trying to solve?
You declare (at least) three different variables called userName. This is almost certainly a mistake. Just keep the very first one, and in the other 2 places just use the first one, eg instead of
String userName = reader.nextLine(); // creates a new userName local to this method
just have
userName = reader.nextLine(); // uses the userName declared in the class
would a possible way to return the Integer value from the hashtable, then just check if that value equals N-1? that would find the match correctly wouldn't it?
Yes (that's what I had in mind when I first suggested HashTable). You can loop thru the keyset checking the corresponding values, something like:
for (Card c : topcards.keySet)
if (topcards.get(c) == (n-1)) // card c appears (n-1) times
It's not really an error. Eclipse's default settings are configured for maximum safety, and sometimes you just have to tell it to chill out a bit.
What layout manager are you using? Personally I would always go with GridBagLayout for a window that can be re-sized - that way you have total control over what happens.
I personally wouldn't recommend JLayer. It's not been updated for many years, and I had a lot of problems when I tried to use it 5 or 6 years ago.
I think Eclipse "restriction errors" with Java code is well within the scope of this forum, so please do explain exactly what's happening.
OK. I have people arriving for dinner in 5 mins, so I;ll be offline until tomorrow.
I'm an OO purist. I'd put the Card every time. I'd also write an equals(Card otherCard) method in the Card class and not rely on the other code knowing about how the Strings work.
apines has a good point - you can incorporate that by aborting if the hashtable size goes >2.
I think this just ceased to be a Java problem! I'm not being funny, but without a definition of the algorithm there's no way to code or debug an implementation of that algorithm. Trial & error may work, if you're very lucky, but probably not.
I guess this algorithm must be fairly standard, so it shouldn't be too hard to find it in Google-land.
OK, but what's the recursive algorithm, in english?
Sorry, but I don't know what algorithm you are trying to implement. Can you give a brief english-language version?
ps I hope your final else never gets executed - it just calls recursively with the same parameters -> stack overflow. If that's a "not expected to happen" case it 's safer to print a little error message and return 0.
How about looping thru the top cards (no need for the ArrayList) and building a HashTable<Card, Integer> showing the number of occurrences of each card, ie
for each top card:
if hashtable's keys contain this card, increment its counter
else add a new entry (card, 1)
Then you know exactly how many of each card you've got.
If the count is right, remove all occurrences of that card from the stacks
Your return is part of the if statement starting on line 8, so if that if test is false the code drops thru line 11, where the method terminates without executing a valid return statement.
You know what's strange? I have no idea how! I think it's something the originator (ie you) can do, but I can't. I guess there's a check box or something?
y > x > 1
y > x
returns a boolean (true/false), so then it continues
boolean > 1
which is invalid - you can't compare a boolean and an int.
You need to compare y with x, then compare x with 0, then "and" the two results together
ie (in pseudo code)
(x greater than y) and (y greater than 0)
OK, great, mark this thread "solved"?
Java assumes that a class is not Serializsable (ie not able to be written to an Object stream) unless you explicitly say it is. To do that you declare the class as
implements Serializable
(check out the API for the Serializable interface)
If all your class data is just regular Strings and ints and so on, that's all you need to do.
OK, well, thanks for trying anyway. Maybe it's a Mac Java problem? If you post a runnable version that demonstrates the problem I can try it under Win 7 Java 1.6.22 for you if you like.
That's wierd. I tried your code, it didn't work, I changed it as above, it worked.
Here's the exact code I used:
import java.io.File;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import java.io.IOException;
import javax.swing.SwingUtilities;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
public class Main {
public static void main(String[] args) throws IOException {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Main main = new Main();
}
});
}
// Controlled UI
private MainUI ui;
Main() {
ui = new MainUI(this);
ui.setVisible(true);
fetchSeries();
}
private void fetchSeries() {
// Originally, the vector is filled via database query, but
// i'll just hard code it now...
//Vector v = db.getSeries();
Vector v = new Vector();
v.add("Entry 1");
v.add("Entry 2");
v.add("Entry 3");
ui.listSeries(v);
}
}
class MainUI {
// Controller controlling this UI
private Main mControl;
// Constants
private static final int MAIN_WIDTH = 200;
private static final int MAIN_HEIGHT = 460;
// Variables
private JFrame mainFrame;
private JPanel mainPanel, seriesPanel;
private JScrollPane seriesPane;
private JList seriesList;
private JLabel lblSeries;
private DefaultListModel lmSeries;
private Dimension screensize;
MainUI(Main main_par) {
mControl = main_par;
createUI();
}
private void createUI() {
// Get screen size
Toolkit toolkit = Toolkit.getDefaultToolkit();
screensize = toolkit.getScreenSize();
// Create and set the main frame
mainFrame = new JFrame("vg");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setBounds((screensize.width / 2) - (MAIN_WIDTH / 2),
(screensize.height / 2) - (MAIN_HEIGHT / 2), MAIN_WIDTH, …
Hi kvass
I don't think anyone here is being disparaging. Previous posts have given enough info for someone to get documentation and sample code that's relevant to the problem.
The original authors have already spent hours writing and checking those web pages, so it's unrealistic to expect someone here to duplicate that effort.
If the O/P came back with "I've tried the following program from the web and I have the following problem that I cant fix ..." then someone would help. But "I couldn't find anything on Google" points to a completely different kind of problem that's outside the scope of a Java forum.
ps. I just Googled "jlayer mp3 sample code" and the very first hit was this
http://www.cs.princeton.edu/introcs/faq/mp3/mp3.html
which has a simple sample program. Now how hard was that really?
OK, I got a fix based on the way I usually do this, but I haven't worked out why your version doesn't work yet, but here goes:
I moved up the code that creates the list to be before creating the scrollpane, then used the scrollpane constructor with the list as parameter
lmSeries = new DefaultListModel();
seriesList = new JList(lmSeries);
seriesList.setPreferredSize(new Dimension(180, 380));
seriesPane = new JScrollPane(seriesList);
ps: I note that add is inherited from Container, not overridden in scrollpane, so maybe it doesn't hook the model into the scrollpane in the same way that the constructor does. Anyway, it's working now.
This is a good problem! That looks OK to me as well. Maybe the problem lies somewhere else? How hard would it be for you to strip this down to a stand-alone runnable program that displays the problem and that we can work on?
I may still be worth spending 30secs changing them to 0 at the top and seeing what effect that has?
What are the values returned by load.getInsets()? - looks like the canvas will be smaller than the frame by that amount?
JList() Constructs a JList with an empty, read-only, model.
The API says it all.
Without the code? No, no idea.
I apologise for doubting your veracity.
I simply do not believe you. I googled "JMF documentation" which gave 318,000 hits; these were all on the first page...
http://www.oracle.com/technetwork/java/javase/index-141145.html
and this
http://www.javaworld.com/javaworld/jw-04-1997/jw-04-jmf.html
and this
http://www.codemiles.com/java/what-is-jmf-t628.html
... you get the idea
Open the file writer in append mode :
new FileWriter("file4.txt", true)
It's all on the web - Google.
Java Media Framework (JMF) - although it's not as good as it shoul dbe.
Glad to help - mark this thread as closed now?
Line 5 - you have +1 that shouldn't be there - you are removing the first entry but adding a new zero entry at the end - hence it never stops recursing.