
-----------------------------------
FileFantasy
Thu Dec 21, 2006 10:37 pm

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)

-----------------------------------
HellblazerX
Fri Dec 22, 2006 7:56 am

Re: 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?
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.

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?

-----------------------------------
FileFantasy
Fri Dec 22, 2006 6:16 pm


-----------------------------------
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".

-----------------------------------
OneOffDriveByPoster
Sat Dec 23, 2006 9:42 pm


-----------------------------------
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 
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.

-----------------------------------
wtd
Sat Dec 23, 2006 11:49 pm


-----------------------------------
Or perhaps you could use an anonymous inner class?

-----------------------------------
FileFantasy
Sun Dec 24, 2006 6:57 pm


-----------------------------------
Ok, thanks for the help, I actually solved those problems today!  :lol: 

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
centercen.setSize (new Dimension (356, 5+lines*20));

Then, I said when a button is clicked, this happens:
lines+= 3;

But now, when I do

centercen.setSize (new Dimension (356, 5+lines*20));
add (centercen, BorderLayout.CENTER);
setVisible(true);

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?)

-----------------------------------
wtd
Sun Dec 24, 2006 7:35 pm


-----------------------------------
When you created a new Dimension object, you gave it a value based on the value in lines.  That does not automatically update.

-----------------------------------
FileFantasy
Sun Dec 24, 2006 9:31 pm


-----------------------------------
Yes, I know that, that's why I have this code
centercen.setSize (new Dimension (356, 5+lines*20)); 
add (centercen, BorderLayout.CENTER); 
setVisible(true);

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?

-----------------------------------
wtd
Mon Dec 25, 2006 12:27 am


-----------------------------------
Ah.  I thought that was when you were initially setting things up.

-----------------------------------
FileFantasy
Thu Dec 28, 2006 5:40 am


-----------------------------------
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: 
    public void actionPerformed (ActionEvent e) 
    { 
        if (e.getSource () == linechange) 
        { 
            if (lines == 30) 
            { 
                lines = 3; 
            } 
            else 
            { 
                lines += 3; 
            } 
            linechange.setText ("Lines Of Output Displayed: " + lines); 
            centercen.setSize (new Dimension (356, 5 + lines * 20)); 
            center.add (centercen, BorderLayout.CENTER); 
            add (center, BorderLayout.CENTER); 
            setVisible (true); 
        } 
    } 
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.
