Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Two swing question:
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
FileFantasy




PostPosted: 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)
Sponsor
Sponsor
Sponsor
sponsor
HellblazerX




PostPosted: 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?
FileFantasy




PostPosted: Fri Dec 22, 2006 6:16 pm   Post subject: (No 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".
OneOffDriveByPoster




PostPosted: Sat Dec 23, 2006 9:42 pm   Post subject: (No 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.
wtd




PostPosted: Sat Dec 23, 2006 11:49 pm   Post subject: (No subject)

Or perhaps you could use an anonymous inner class?
FileFantasy




PostPosted: Sun Dec 24, 2006 6:57 pm   Post subject: (No subject)

Ok, thanks for the help, I actually solved those problems today! Laughing

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
code:
centercen.setSize (new Dimension (356, 5+lines*20));


Then, I said when a button is clicked, this happens:
code:
lines+= 3;


But now, when I do
code:

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




PostPosted: Sun Dec 24, 2006 7:35 pm   Post subject: (No subject)

When you created a new Dimension object, you gave it a value based on the value in lines. That does not automatically update.
FileFantasy




PostPosted: Sun Dec 24, 2006 9:31 pm   Post subject: (No subject)

Yes, I know that, that's why I have this code
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?
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Mon Dec 25, 2006 12:27 am   Post subject: (No subject)

Ah. I thought that was when you were initially setting things up.
FileFantasy




PostPosted: Thu Dec 28, 2006 5:40 am   Post subject: (No 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:
code:
    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.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: