 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Kova Guest
|
Posted: Sat Feb 26, 2005 10:10 pm Post subject: autofocus in JScrollPane |
|
|
Hi.
I have a problem. In my application in one fixed size JScrollPane user
can add rows of stuff dynamicly at runtime. Focus is then set to the
first component in a new row (requestFocusInWindow()) but jscrollpane's
vertical bar dosen't automaticly scrolls down. Can anyone help me out
with this please? Thank you.
--
Kova
|
|
| Back to top |
|
 |
Andrey Kuznetsov Guest
|
Posted: Sat Feb 26, 2005 11:08 pm Post subject: Re: autofocus in JScrollPane |
|
|
| Quote: | I have a problem. In my application in one fixed size JScrollPane user can
add rows of stuff dynamicly at runtime. Focus is then set to the first
component in a new row (requestFocusInWindow()) but jscrollpane's vertical
bar dosen't automaticly scrolls down. Can anyone help me out with this
please? Thank you.
JComponent#scrollRectToVisible(Rectangle rect); |
--
Andrey Kuznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities
|
|
| Back to top |
|
 |
Kova Guest
|
Posted: Sun Feb 27, 2005 12:40 am Post subject: Re: autofocus in JScrollPane |
|
|
Andrey Kuznetsov wrote:
| Quote: | JComponent#scrollRectToVisible(Rectangle rect);
|
temp_tf.scrollRectToVisible(temp_tf.getBounds());
or temp_tf.scrollRectToVisible(temp_tf.getVisibleRect());
doesn't work, tried that
I called it last in method that adds new rows.
--
Kova
|
|
| Back to top |
|
 |
Andrey Kuznetsov Guest
|
Posted: Sun Feb 27, 2005 9:15 am Post subject: Re: autofocus in JScrollPane |
|
|
| Quote: | temp_tf.scrollRectToVisible(temp_tf.getBounds());
or temp_tf.scrollRectToVisible(temp_tf.getVisibleRect());
sorry, but these both does not make any sense. |
why want you to scroll to rect which is alredy visible?
temp_tf.getBounds() is bigger then you need - it is unclear where to scroll.
Rectangle bounds = temp_tf.getBounds();
Rectangle visible = temp_tf.getVisibleRect();
Rectangle r = new Rectangle(0, bounds.height - visible.height,
visible.height, visible.width);
try temp_tf.scrollRectToVisible(r);
--
Andrey Kuznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities
|
|
| Back to top |
|
 |
Kova Guest
|
Posted: Sun Feb 27, 2005 11:30 am Post subject: Re: autofocus in JScrollPane |
|
|
Andrey Kuznetsov wrote:
| Quote: | temp_tf.scrollRectToVisible(temp_tf.getBounds());
or temp_tf.scrollRectToVisible(temp_tf.getVisibleRect());
sorry, but these both does not make any sense.
why want you to scroll to rect which is alredy visible?
temp_tf.getBounds() is bigger then you need - it is unclear where to scroll.
|
Sorry, I'm still learining Java and didn't encounter Rectangle before.
Don't know what means "A Rectangle specifies an area in a >>coordinate
space<<" so I can't figure out how to do it myself. Web isn't much help
also. Thought since getBounds gets exactly info input as Rectangle is
made of that it should work.
| Quote: | Rectangle bounds = temp_tf.getBounds();
Rectangle visible = temp_tf.getVisibleRect();
Rectangle r = new Rectangle(0, bounds.height - visible.height,
visible.height, visible.width);
try temp_tf.scrollRectToVisible(r);
|
still doesn't work, doesn't throw any exceptions either.
--
Kova
|
|
| Back to top |
|
 |
Andrey Kuznetsov Guest
|
Posted: Sun Feb 27, 2005 3:04 pm Post subject: Re: autofocus in JScrollPane |
|
|
| Quote: | Rectangle bounds = temp_tf.getBounds();
Rectangle visible = temp_tf.getVisibleRect();
Rectangle r = new Rectangle(0, bounds.height - visible.height,
visible.height, visible.width);
try temp_tf.scrollRectToVisible(r);
still doesn't work, doesn't throw any exceptions either.
post your code (SSCCE if possible) |
--
Andrey Kuznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities
|
|
| Back to top |
|
 |
Kova Guest
|
Posted: Sun Feb 27, 2005 11:40 pm Post subject: Re: autofocus in JScrollPane |
|
|
Andrey Kuznetsov wrote:
| Quote: | post your code (SSCCE if possible)
|
don't know what SSCCE is but here's a miniature application that I made
to show you the problem. It's a JFrame that has JScrollPane with that
problem. You add new rows with <Enter>. Thank you for the interest.
You'll need TableLayout to run it
([url]http://www.clearthought.info/software/tablelayout)[/url].
------------------------------------------------------------------
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import info.clearthought.layout.*;
public class test1 extends JFrame implements Action {
private JPanel artikli_pnl;
private JScrollPane artikli_sp;
private HashMap artikli_indexi_hmap;
private int artikli_index_int;
private TableLayout artikli_tlayout;
private JTextField temp_tf;
private int temp_int, i;
double artikli_tlayout_size[][] = {{100, 5, 50, 5, 50, 5, 70} , {}};
public static void main(String[] args) {
test1 GlavniFrame_SMFW = new test1();
GlavniFrame_SMFW.setBounds(0, 0, 800, 600);
GlavniFrame_SMFW.setVisible(true);
}
public test1() {
super("test1");
addWindowListener(new WindowAdapter() {
public void WindowClosing(WindowEvent e) {
System.exit(0);
}
});
getContentPane().setLayout(new FlowLayout());
artikli_pnl = new JPanel();
artikli_sp = new JScrollPane(artikli_pnl);
artikli_sp.setPreferredSize(new Dimension(400, 200));
this.getContentPane().add(artikli_sp);
artikli_tlayout = new TableLayout(artikli_tlayout_size);
artikli_pnl.setLayout(artikli_tlayout);
artikli_indexi_hmap = new HashMap();
artikliAddRow();
artikliAddRow();
artikliAddRow();
artikli_sp.setBorder(BorderFactory.createEmptyBorder());
for (int i=0; i < artikli_pnl.getComponentCount(); i++) {
((JTextField)artikli_pnl.getComponent(i)).getInputMap().put(KeyStroke.getKeyStroke("ENTER"),
"doAction");
((JTextField)artikli_pnl.getComponent(i)).getActionMap().put("doAction",
this);
}
}
private void artikliAddRow() {
i = artikli_pnl.getComponentCount() / 4;
System.out.println(i);
artikli_tlayout.insertRow(i, 21.0);
temp_tf = new JTextField();
artikli_pnl.add(temp_tf, "0, " + i + ", f, f");
artikli_indexi_hmap.put(temp_tf, new Integer(artikli_index_int++));
temp_tf.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "doAction");
temp_tf.getActionMap().put("doAction", this);
temp_tf = new JTextField();
artikli_pnl.add(temp_tf, "2, " + i + ", f, f");
artikli_indexi_hmap.put(temp_tf, new Integer(artikli_index_int++));
temp_tf.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "doAction");
temp_tf.getActionMap().put("doAction", this);
temp_tf = new JTextField();
artikli_indexi_hmap.put(temp_tf, new Integer(artikli_index_int++));
artikli_pnl.add(temp_tf, "4, " + i + ", f, f");
temp_tf.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "doAction");
temp_tf.getActionMap().put("doAction", this);
temp_tf = new JTextField();
artikli_indexi_hmap.put(temp_tf, new Integer(artikli_index_int++));
artikli_pnl.add(temp_tf, "6, " + i + ", f, f");
temp_tf.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "doAction");
temp_tf.getActionMap().put("doAction", this);
}
public void actionPerformed(ActionEvent e) {
if (((JTextField)e.getSource()).getParent() == artikli_pnl) {
temp_int =
((Integer)artikli_indexi_hmap.get(e.getSource())).intValue() + 1;
if (temp_int >= artikli_pnl.getComponentCount()) {
artikliAddRow();
artikli_sp.revalidate();
artikli_sp.repaint();
}
temp_tf = (JTextField)artikli_pnl.getComponent(temp_int);
temp_tf.requestFocusInWindow();
temp_tf.selectAll();
// AUTO SCROLL STUFF TO CONFIGURE:
Rectangle bounds = temp_tf.getBounds();
Rectangle visible = temp_tf.getVisibleRect();
Rectangle r = new Rectangle(0, bounds.height - visible.height,
visible.height, visible.width);
try {
temp_tf.scrollRectToVisible(r);
}
catch (Exception ex) {
System.out.println(ex);
}
}
}
public Object getValue(String key) {return null;}
public void putValue(String key, Object value) {}
}
------------------------------------------------------------------------
|
|
| Back to top |
|
 |
Andrey Kuznetsov Guest
|
Posted: Fri Mar 04, 2005 8:20 pm Post subject: Re: autofocus in JScrollPane |
|
|
import info.clearthought.layout.TableLayout;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
public class test1 extends JFrame implements Action {
private JPanel artikli_pnl;
private JScrollPane artikli_sp;
private HashMap artikli_indexi_hmap;
private int artikli_index_int;
private TableLayout artikli_tlayout;
private JTextField temp_tf;
private int temp_int, i;
double artikli_tlayout_size[][] = {{100, 5, 50, 5, 50, 5, 70}, {}};
public static void main(String[] args) {
test1 GlavniFrame_SMFW = new test1();
GlavniFrame_SMFW.setBounds(0, 0, 800, 600);
GlavniFrame_SMFW.setVisible(true);
}
public test1() {
super("test1");
addWindowListener(new WindowAdapter() {
public void WindowClosing(WindowEvent e) {
System.exit(0);
}
});
getContentPane().setLayout(new FlowLayout());
artikli_pnl = new JPanel();
artikli_sp = new JScrollPane(artikli_pnl);
artikli_sp.setPreferredSize(new Dimension(400, 200));
this.getContentPane().add(artikli_sp);
artikli_tlayout = new TableLayout(artikli_tlayout_size);
artikli_pnl.setLayout(artikli_tlayout);
artikli_indexi_hmap = new HashMap();
artikliAddRow();
artikliAddRow();
artikliAddRow();
artikli_sp.setBorder(BorderFactory.createEmptyBorder());
for (int i = 0; i < artikli_pnl.getComponentCount(); i++) {
((JTextField)
artikli_pnl.getComponent(i)).getInputMap().put(KeyStroke.getKeyStroke("ENTER"),
"doAction");
((JTextField)
artikli_pnl.getComponent(i)).getActionMap().put("doAction",
this);
}
}
private void artikliAddRow() {
i = artikli_pnl.getComponentCount() / 4;
System.out.println(i);
artikli_tlayout.insertRow(i, 21.0);
temp_tf = new JTextField();
artikli_pnl.add(temp_tf, "0, " + i + ", f, f");
artikli_indexi_hmap.put(temp_tf, new Integer(artikli_index_int++));
temp_tf.getInputMap().put(KeyStroke.getKeyStroke("ENTER"),
"doAction");
temp_tf.getActionMap().put("doAction", this);
temp_tf = new JTextField();
artikli_pnl.add(temp_tf, "2, " + i + ", f, f");
artikli_indexi_hmap.put(temp_tf, new Integer(artikli_index_int++));
temp_tf.getInputMap().put(KeyStroke.getKeyStroke("ENTER"),
"doAction");
temp_tf.getActionMap().put("doAction", this);
temp_tf = new JTextField();
artikli_indexi_hmap.put(temp_tf, new Integer(artikli_index_int++));
artikli_pnl.add(temp_tf, "4, " + i + ", f, f");
temp_tf.getInputMap().put(KeyStroke.getKeyStroke("ENTER"),
"doAction");
temp_tf.getActionMap().put("doAction", this);
temp_tf = new JTextField();
artikli_indexi_hmap.put(temp_tf, new Integer(artikli_index_int++));
artikli_pnl.add(temp_tf, "6, " + i + ", f, f");
temp_tf.getInputMap().put(KeyStroke.getKeyStroke("ENTER"),
"doAction");
temp_tf.getActionMap().put("doAction", this);
// AUTO SCROLL STUFF TO CONFIGURE:
Rectangle bounds = temp_tf.getBounds();
artikli_pnl.scrollRectToVisible(bounds);
}
public void actionPerformed(ActionEvent e) {
if (((JTextField) e.getSource()).getParent() == artikli_pnl) {
temp_int =
((Integer)
artikli_indexi_hmap.get(e.getSource())).intValue() + 1;
if (temp_int >= artikli_pnl.getComponentCount()) {
artikliAddRow();
artikli_sp.revalidate();
artikli_sp.repaint();
}
temp_tf = (JTextField) artikli_pnl.getComponent(temp_int);
temp_tf.requestFocusInWindow();
temp_tf.selectAll();
// AUTO SCROLL STUFF TO CONFIGURE:
Rectangle bounds = temp_tf.getBounds();
artikli_pnl.scrollRectToVisible(bounds);
}
}
public Object getValue(String key) {
return null;
}
public void putValue(String key, Object value) {
}
}
--
Andrey Kuznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|