7,116 Posted Topics
Re: 1. Create a new class that contains a label and a textfield and write the necessary constructor(s) and other methods. 2. If you just want it for positioning etc, put them both in a JPanel | |
Re: That whole block of code simply tells Java to exit the app when the window is closed, so just remove it or each window that should not call System.exit when it is closed. | |
Re: 5 seconds or 5 minutes? Are you doing those long-running tasks on the Swing event thread? If so, move them to a SwingWorker thread so they execute in the background while the GUI continues to display | |
Re: Your pseudo-code looks OK. Now, back to the test driver. You have all your code in main, which makes this difficult - so the first thing is to move the number generator into a new method that returns an array of 50 different random numbers, eg [CODE]public int[] generateNumbers() { … | |
Re: passnum[i] = 26;} int table[][] = new int [26][26]; table[passnum[i]][passnum[i++]]++ Now can you see it? (the max valid index for an array[26] is 25) | |
Re: I really want to endorse the "println" approach. Some of the hardest problems to solve are because: 1. You have an incorrect understanding of how some API works, or 2. You are missing something that you didn't know you needed In both cases no amount of introspection or code re-reading … | |
Re: The SwingWorker class is designed for running long-running tasks in the background with a Swing GUI. | |
Re: When you split the code into classes you ended up with "import" statements in the middle of your file. That's illegal, they must be the first thing in the file (except for a "package" statement). So either you just put the imports at the beginning and leave all your classes … | |
Re: There shouldn't be any problem doing this with a JLabel. You can set the size, fg/bg colours (including transparent background), font, style and point size etc. You can position it wherever you like by using a null LayoutManager. It will automatically display on top of the JFrame. You may have … | |
Re: Sunshineserene: you are making this very difficult for anyone to help you because your problem description is so vague. Exactly which array in exactly which class is being "called" from exactly where? Please specify the exact line numbers where the variables and code in question can be found. | |
Re: You have overidden paint(Graphics g) but this is not a recommended practice. paint(Graphics g) also has responsibility for painting child objects etc, and this can all get lost when you override it. The recommended practice is to override paintComponent(Graphics g) instead. You override it in exactly the same way, but … | |
Re: 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 … | |
Re: main line 44 You create the pw using the no-args constructor which you document as follows: [QUOTE]The no-arg constructor initializes an object with null* strings for name, employee number, and hire date.[/QUOTE] You then immediately print it put and get empty data - just like you said! Nowhere do you … | |
Re: 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 - … | |
Re: Separation of user interface and logic/model is a fundamental design pattern that is followed by all serious code. Here's a simple heuristic for determining where to put any particular function: Ask yourself "suppose I write two versions, one with a GUI, one with a command-line interface (or maybe an HTML … | |
Re: 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... [CODE] String[][] data = new String[99][]; // first dim big enough for all sentences int sentenceNum = 0; while (lineScanner.hasNext()){ … | |
Re: Put all the code to run the SELECT and update the gui into a single small new method, with the v1 value as a parameter... [CODE]public void displayQuestion(int questionID) { Statement s = con.createStatement(); s.execute("SELECT Question FROM Ques WHERE ID=" + questionID); // update GUI etc }[/CODE] then in your … | |
Re: AffineTransform. You create an instance of AffineTransform that maps the rotation you want and apply that to a Graphics2D instance with setTransform(...). Paint your icon to the Graphics2D and hey-presto, you have a rotated icon. Here's a quick intro: [url]http://www.javareference.com/jrexamples/viewexample.jsp?id=88[/url] | |
Re: Looking at the code you posted I can't see anything that initialises connectionFactory, so I'm unsurprised that connectionFactory.createConnection() gives an NPE. | |
Re: 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 [CODE]// instance version - how to reference StringArrays sa = new StringArrays(); ... sa.Anglia; // static version - how … | |
Re: If you need to create a variable number of ArrayLists then you will need some kind of container to put them in, eg an array or ArrayLists or an ArrayList of ArrayLists. Start with an new empty container, then in your loop create copies of the original ArrayList and add … | |
Re: For simple cases like this you can just declare some kind of collection (eg ArrayList) as a static variable in the class, then, in the constructor, add the new instance to the collection. | |
Re: This is the expected behaviour. What exactly is the problem you are trying to solve? | |
Re: You don't initialise Smallest, so it starts as 0, so if all your values are >0, smallest will always be zero. Best to initialise it to the largest valid int value. double avg; avg = sum / count; Although you have declared avg as double, the calc of sum/count is … | |
Re: You set the model for tabel in the init method (line 75), but then after returning from that you set a different model for tabel (line 39). That seems odd. | |
Re: 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 [CODE]String userName = reader.nextLine(); // creates a new userName local to this method[/CODE] just have … | |
Re: pseudocode: [CODE]define class for new objects 3 variables constructor that takes 3 values to set the variables ... actionperformed loop: read next line from file use split (as above) to separate the 3 fields create new object using the 3 fields add new object to an ArrayList end loop[/CODE] | |
Re: 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 … | |
Re: Java Media Framework (JMF) - although it's not as good as it shoul dbe. | |
Re: You can add a ListSelectionListener to the first list. It will be called whenever the user changes his selection in the list. When that is called you can query which line is selected and use that to put the appropriate data in the second list. | |
Re: Count and match your quotes - there are 7 on that line = 1 unmatched. The colour coding (blue = string literal) in your posted code is a good clue. | |
Re: 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 … | |
Re: y > x > 1 [I]y > x[/I] returns a boolean (true/false), so then it continues [I]boolean > 1[/I] 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 … | |
Re: Does [2] run in its own thread? Presumably it needs to wait until there something in the list? If so, the normal solution is to use a LinkedBlockingQueue - you will find the documentation in the usual places. | |
Re: There are a couple of ways to play a simple sound in Java. This explains the newer. better (but harder) way: [url]http://www.anyexample.com/programming/java/java_play_wav_sound_file.xml[/url] and this is the older, simpler,way: [url]http://stackoverflow.com/questions/26305/how-can-i-play-sound-in-java[/url] Either way, you call the appropriate code from your actionPerformed method. | |
Re: Are you wanting to add the two arrays as two elements, or are you wanting to add all the elements in the arrays? For the first case [CODE] int[] a = {1,2,3}; int[] b = {4,5}; HashSet h = new HashSet(); h.add(a); h.add(b); for (Object o : h) System.out.println(o); // … | |
Re: You can catapult this to a higher level by creating a class for the stores and their coordinates - something like: [CODE]class Store { String name; int x, y, width, height; int doorX. (etc)[/CODE] Pass all the coords to the constructor. Then have methods in the class like [CODE]public void … | |
Re: Open the file writer in append mode : new FileWriter("file4.txt", true) | |
Re: Control Panel > Default Programs > Set Associations | |
| |
Re: Is this for homework? What's [B][I]your [/I][/B]theory as to why this code will not work (apart from spelling errots!) | |
Re: [CODE]// pseudo code int max (int [] v) { if (v has only 1 element) return that element int[] v1 = copy of v excluding the first element int maxV1= max(v1) if (first element of v > maxV1) return first element of v; return maxV1; }[/CODE] | |
Re: You have created an action listener, but you have not associated it with the button. Check out the addActionListener method that JButton inherits from its supercalss AbstractButton | |
Re: I haven't tried this myself but... The message String is displayed in a JLabel, so you should be able to use simple HTML to format the text. eg "<HTML><H1>This is a message</H1></HTML>" | |
Re: Your toString looks fine. You could add more of the data items if you want. The equals method seems valid - but not what I would expect. It seems to say that two scores are "equal" if one of them has the same data as the other's "friend" data. Normally … | |
Re: What exactly is the error message - or are we supposed to guess? ;-) | |
Re: It's a problem with the package names - you seem to have the class name at the end of the package names, which is syntactically valid, but not a normal thing to do. I would start by putting them both in the same package package myJava.SimpleFrame; and there's no need … | |
Re: This discussion about the array size seems to have gone off in a strange direction. Regardless of where you get the array from you can find out its length at runtime using the "length" attribute that every array has. The following code illustrates this [CODE]for (int i = 0; i … |
The End.