Computer Science Canada Two swing question: |
Author: | FileFantasy [ Thu Dec 21, 2006 10:37 pm ] |
Post subject: | Two swing question: |
1) I created 30 20x20 buttons, and put them in a JPanel that's too small to fit all of them (five buttons each row, 3 rows on screen at once). So I put this JPanel into a JScrollPane, but it doesn't detect that it can scroll down to the 4th-6th row... Why is that? 2) What do I need to do to a JTextField so that it detects key strokes / text changes? (I use ActionListener on it right now, but it only triggers when I press Enter) |
Author: | HellblazerX [ Fri Dec 22, 2006 7:56 am ] |
Post subject: | Re: Two swing question: |
FileFantasy wrote: 1) I created 30 20x20 buttons, and put them in a JPanel that's too small to fit all of them (five buttons each row, 3 rows on screen at once). So I put this JPanel into a JScrollPane, but it doesn't detect that it can scroll down to the 4th-6th row... Why is that?
I believe the JScrollPane only scrolls if the objects directly inside it are too big. Your buttons may be too large the the JScrollPane, but your JPanel is not. You should enlarge the JPanel should it fits all the buttons. FileFantasy wrote: 2) What do I need to do to a JTextField so that it detects key strokes / text changes? (I use ActionListener on it right now, but it only triggers when I press Enter)
Why not use a KeyListener as well? |
Author: | FileFantasy [ Fri Dec 22, 2006 6:16 pm ] |
Post subject: | |
Ok, I got the JScrollPane to work, but is there a way to set how many pixels I scroll down when I press the down button [v] once? (I want to scroll 28 pixels down each time) And also, how may I go about adding a KeyListener? I tried but it says something like "your class cannot use keylistener because it's not abstract". |
Author: | OneOffDriveByPoster [ Sat Dec 23, 2006 9:42 pm ] |
Post subject: | |
FileFantasy wrote: Ok, I got the JScrollPane to work, but is there a way to set how many pixels I scroll down when I press the down button [v] once? (I want to scroll 28 pixels down each time)
And also, how may I go about adding a KeyListener? I tried but it says something like "your class cannot use keylistener because it's not abstract". If you say you "implement" KeyListener, you must have all the methods in the KeyListener interface. You can have a separate class for your KeyListener too; that way, you can get away with "extending" KeyAdapter. |
Author: | wtd [ Sat Dec 23, 2006 11:49 pm ] |
Post subject: | |
Or perhaps you could use an anonymous inner class? |
Author: | FileFantasy [ Sun Dec 24, 2006 6:57 pm ] | ||||||
Post subject: | |||||||
Ok, thanks for the help, I actually solved those problems today! ![]() However, I've encountered a new question: Is it possible to change the size of a panel after the first time? For example: I have this
Then, I said when a button is clicked, this happens:
But now, when I do
The dimension does not change one bit... (Note that I did setResizable (false); but I don't think that changes anything other than the user himself cannot resize the window, right?) |
Author: | wtd [ Sun Dec 24, 2006 7:35 pm ] |
Post subject: | |
When you created a new Dimension object, you gave it a value based on the value in lines. That does not automatically update. |
Author: | FileFantasy [ Sun Dec 24, 2006 9:31 pm ] | ||
Post subject: | |||
Yes, I know that, that's why I have this code
It's suppose to re-add the JTextArea into the JFrame, then update the JFrame with setVisble(true_; But it's not working, is there any other way to update the UI? |
Author: | wtd [ Mon Dec 25, 2006 12:27 am ] |
Post subject: | |
Ah. I thought that was when you were initially setting things up. |
Author: | FileFantasy [ Thu Dec 28, 2006 5:40 am ] | ||
Post subject: | |||
Nope. I want to change the size of the window after the initial UI is created, and I locked resizing because I only want the user to be able to resize vertically. So I want to restrict the possible size of my window to be: (Horizontal pixels x Vertical pixels) 356x65 356x125 356x185 356x245 etc... Is there a way to do that? Here's the actual attempted coding:
where: linechange is a JButton centercen is a JPanel center is the JPanel that holds centercen and other JPanels It's not working though, the window doesn't resize on button pressed. |