Amigos,
Criei botões como primeiro, registro anterior, ultimo registro
Gostaria de dar vida a estes botões fazendo com que eles movimentem o JTable, como fazer isto?
Dario
Amigos,
Criei botões como primeiro, registro anterior, ultimo registro
Gostaria de dar vida a estes botões fazendo com que eles movimentem o JTable, como fazer isto?
Dario
Ahhh bom… lendo o seu título, pensei que vc ia querer mover o seu JTable pela tela…
Você quer mudar só a barra de seleção, e fazer com que o ScrollPane acompanhe, certo? Nós usamos esse código para isso:
public class GuiUtils
{
public static void scrollToVisible(JTable table, int rowIndex)
{
scrollToVisible(table, rowIndex, 0);
}
public static void scrollToVisible(JTable table, int rowIndex, int vColIndex)
{
if (!(table.getParent() instanceof JViewport))
return;
setViewPortPosition((JViewport) table.getParent(), table.getCellRect(
rowIndex, vColIndex, true));
}
public static Collection<Integer> getReverseSelectedRows(JTable table)
{
Set<Integer> rows = new TreeSet<Integer>(new Comparator<Integer>()
{
public int compare(Integer o1, Integer o2)
{
return o2.compareTo(o1);
}
});
for (int r : table.getSelectedRows())
rows.add(r);
return rows;
}
public static void selectAndScroll(JTable table, int rowIndex)
{
table.getSelectionModel().setSelectionInterval(rowIndex, rowIndex);
scrollToVisible(table, rowIndex);
}
public static void scrollToSelection(JTree tree)
{
if (!(tree.getParent() instanceof JViewport))
return;
setViewPortPosition((JViewport) tree.getParent(),
tree.getPathBounds(tree.getSelectionPath()));
}
private static void setViewPortPosition(JViewport viewport,
Rectangle position)
{
// The location of the viewport relative to the object
Point pt = viewport.getViewPosition();
// Translate the cell location so that it is relative
// to the view, assuming the northwest corner of the
// view is (0,0)
position.setLocation(position.x - pt.x, position.y - pt.y);
// Scroll the area into view
viewport.scrollRectToVisible(position);
}
public static void expandAllNodes(JTree tree)
{
for (int i = 0; i < tree.getRowCount(); i++)
tree.expandRow(i);
}
public static void expandFirstNodes(JTree tree)
{
for (int i = tree.getRowCount() - 1; i >= 0; i--)
tree.expandRow(i);
}
/**
* Encode a color in a string in RGB format. This string is compatible to
* HTML format.
*
* @param color The color to encode (e.g. Color.RED)
* @return The encoded color (e.g. FF0000)
*/
public static String encodeColor(Color color)
{
if (color == null)
return "000000";
return String.format("%02x%02x%02x", color.getRed(), color.getGreen(),
color.getBlue());
}
public static Component getOwnerWindow(Component component)
{
Component parent = component;
while (parent != null && !(parent instanceof Frame)
&& !(parent instanceof Dialog))
parent = parent.getParent();
return parent;
}
}
Use o método selectAndScroll.
Você precisará fazer continhas, como pegar a linha selecionada e somar 1 para avançar um registro.
Use para isso métodos do próprio JTable como getSelectedRow().
não compreendi bem,
não existe nada como jTable1.first()???
como ficaria o código?
Dario
Ficaria mais ou menos assim:
First:
GuiUtils.selectAndScroll(suaTable, 0);
Next:
GuiUtils.selectAndScroll(suaTable, suaTable.getSelectedRow()+1);
Previous:
GuiUtils.selectAndScroll(suaTable, suaTable.getSelectedRow()-1);
Last:
GuiUtils.selectAndScroll(suaTable, suaTable.getRowCount()-1);
entendi, agora funcionou legal.
ps.
onde posso deixar meu msn neste forum para me corresponder com outros programadores?
Você já deixou, no seu perfil.
Boa tarde.
Estive tentando utilizar esta classe. Mas ao importar ela para o netbeans, este método ficou marcado. Não é recolhecido pela classe e o objetos position também não foram reconhecidos.
Preciso alterar alguma coisa para que a classe fique livre das mensagens de erros?
Grato
private static void setViewPortPosition(JViewport viewport,
Rectangle position)
{
// The location of the viewport relative to the object
Point pt = viewport.getViewPosition();
// Translate the cell location so that it is relative
// to the view, assuming the northwest corner of the
// view is (0,0)
position.setLocation(position.x - pt.x, position.y - pt.y);
// Scroll the area into view
viewport.scrollRectToVisible(position);
}
Esta é a parte do código que apresenta erros no netbeans.
Colega. Desculpe pela mensagem anterior. Eu não executei os import direito então ele apresentou erro. Esta correto agora. Obrigado.
Ola ViniGodoy
estava precisando dessa dica tambem, ja funcionou na minha aplicacao …
Obrigado
Separei o codigo referente a JTable:
import javax.swing.JTable;
import javax.swing.JViewport;
import java.awt.Rectangle;
import java.awt.Point;
import java.util.Collection;
import java.util.TreeSet;
import java.util.Set;
import java.util.Comparator;
public class JTableScroll
{
public static void first(JTable table)
{
int pos = 0;
selectAndScroll(table, pos);
}
public static void next(JTable table)
{
int pos = table.getSelectedRow()+1;
selectAndScroll(table, pos);
}
public static void previous(JTable table)
{
int pos = table.getSelectedRow()-1;
selectAndScroll(table, pos);
}
public static void last(JTable table)
{
int pos = table.getRowCount()+1;
selectAndScroll(table, pos);
}
public static void selectAndScroll(JTable table, int rowIndex)
{
table.getSelectionModel().setSelectionInterval(rowIndex, rowIndex);
scrollToVisible(table, rowIndex);
}
public static Collection<Integer> getReverseSelectedRows(JTable table)
{
Set<Integer> rows = new TreeSet<Integer>(new Comparator<Integer>()
{
public int compare(Integer o1, Integer o2)
{
return o2.compareTo(o1);
}
});
for (int r : table.getSelectedRows())
rows.add(r);
return rows;
}
private static void scrollToVisible(JTable table, int rowIndex)
{
scrollToVisible(table, rowIndex, 0);
}
private static void scrollToVisible(JTable table, int rowIndex, int vColIndex)
{
if (!(table.getParent() instanceof JViewport))
return;
setViewPortPosition((JViewport) table.getParent(), table.getCellRect(rowIndex, vColIndex, true));
}
private static void setViewPortPosition(JViewport viewport, Rectangle position)
{
// The location of the viewport relative to the object
Point pt = viewport.getViewPosition();
// Translate the cell location so that it is relative
// to the view, assuming the northwest corner of the
// view is (0,0)
position.setLocation(position.x - pt.x, position.y - pt.y);
// Scroll the area into view
viewport.scrollRectToVisible(position);
}
}
Chamadas:
//First
JTableScroll.first(suaTable);
//Next
JTableScroll.next(suaTable);
//Previous
JTableScroll.previous(suaTable);
//Last
JTableScroll.last(suaTable);
//Linha qualquer, 7 por exemplo
int pos = 7;
JTableScroll.selectAndScroll(suaTable,pos);