Autor
|
Tema: Pintar celdas de JTable al hacer clic (Leído 2,703 veces)
|
Zoik
Desconectado
Mensajes: 91
|
Primeramente buenas a todos. Bueno estoy empezando a mirar temas de heurísitica y he empezado haciendo la búsqueda del recorrido mas corto con el algoritmo A*. Hasta aquí todo bien, pero me surge un problema y es que para testear todo esto, quiero hacer una JTable que al yo clicar en una celda de color blanco esta se pinte de color negro para dar a entender a la aplicación que es una celda infranqueable por el contrario si clico en una negra, se volverá blanca. Básicamente el problema es que cuando selecciono la celda blanca, me vuelve negras esa y todas las siguientes. A ver si me pudieseis echar una mano. package base; import graphics.Gui; public class Main { public static void main (String[] args ) { new Gui(); } }
package graphics; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JTable; public class Gui extends JFrame { /** * */ private static final long serialVersionUID = 1L; private ExampleTableModel model = new ExampleTableModel(); public Gui() { table.setRowHeight(30); table.setTableHeader(null); table.setCellSelectionEnabled(true); table.setCellSelectionEnabled(false); MyDefaultTableCellRenderer renderer = new MyDefaultTableCellRenderer(); table. setDefaultRenderer(String. class, renderer ); frame.setSize(500, 500); frame.addWindowListener( { { } } ); frame.getContentPane().add(table); frame.setResizable(false); frame.setVisible( true ); } }
package graphics; import javax.swing.table.AbstractTableModel; { /** * */ private static final long serialVersionUID = 1L; private final String[] columnNames = {"", "", "", "", "", "", "", "", "", "", "", "", ""}; {"", "", "", "", "", "", "", "", "", "", "", "", ""}, {"", "", "", "", "", "", "", "", "", "", "", "", ""}, {"", "", "", "", "", "", "", "", "", "", "", "", ""}, {"", "", "", "", "", "", "", "", "", "", "", "", ""}, {"", "", "", "", "", "", "", "", "", "", "", "", ""}, {"", "", "", "", "", "", "", "", "", "", "", "", ""}, {"", "", "", "", "", "", "", "", "", "", "", "", ""}, {"", "", "", "", "", "", "", "", "", "", "", "", ""}, {"", "", "", "", "", "", "", "", "", "", "", "", ""}, {"", "", "", "", "", "", "", "", "", "", "", "", ""}, {"", "", "", "", "", "", "", "", "", "", "", "", ""}, {"", "", "", "", "", "", "", "", "", "", "", "", ""}, {"", "", "", "", "", "", "", "", "", "", "", "", ""}, {"", "", "", "", "", "", "", "", "", "", "", "", ""} }; @SuppressWarnings("unchecked") public Class getColumnClass(int column) { return getValueAt(0, column).getClass(); } public int getColumnCount() { return columnNames.length; } public String getColumnName ( int column ) { return columnNames[column]; } public int getRowCount() { return data.length; } public Object getValueAt ( int row, int column ) { return data[row][column]; } public void setValueAt (Object value, int row, int column ) { data[row][column] = value; } }
package graphics; import java.awt.Color; import java.awt.Component; import javax.swing.JTable; import javax.swing.table.DefaultTableCellRenderer; { /** * */ private static final long serialVersionUID = 1L; public Component getTableCellRendererComponent boolean hasFocus, int row, int column) { Component cell = super. getTableCellRendererComponent (table, value, isSelected, hasFocus, row, column); setBorder(noFocusBorder); if(row == table.getSelectedRow() && column == table.getSelectedColumn()) { System. out. println("Recorriendo " + row + " - " + column + "\nSeleccionada " + table. getSelectedRow() + " - " + table. getSelectedColumn()); if(cell.getBackground().equals(colors[1])) { System. out. println("Pinto Blanco"); cell. setBackground(Color. WHITE); } else if(cell.getBackground().equals(colors[0])) { System. out. println("Pinto Negro"); cell. setBackground(Color. BLACK); } } return cell; } }
Un saludo y gracias de antemano
|
|
|
En línea
|
|
|
|
Zoik
Desconectado
Mensajes: 91
|
Buenas gente. Ya lo tengo solucionado, lo dejo por aquí por si a alguien le interesa. package base; import graphics.Gui; public class Main { public static void main (String[] args ) { new Gui(); } }
package graphics; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JTable; { /** * */ private static final long serialVersionUID = 1L; private ExampleTableModel model = new ExampleTableModel(); private MyDefaultTableCellRenderer renderer; public Gui() { table.setRowHeight(30); table.setTableHeader(null); table.setCellSelectionEnabled(false); table.addMouseListener(this); renderer = new MyDefaultTableCellRenderer(); table. setDefaultRenderer(String. class, renderer ); frame.setSize(500, 500); frame.addWindowListener( { { } } ); frame.getContentPane().add(table); frame.setResizable(false); frame.setVisible( true ); } { } { } { } { int check = renderer.checkContainsCoordinates(table.getSelectedRow(), table.getSelectedColumn()); if(check == -1) { renderer.addCoordinate(table.getSelectedRow(), table.getSelectedColumn()); } else if(check != -1) { renderer.removeCoordinate(check); } table.repaint(); } { } }
package graphics; import javax.swing.table.AbstractTableModel; { /** * */ private static final long serialVersionUID = 1L; private final String[] columnNames = {"", "", "", "", "", "", "", "", "", "", "", "", ""}; {"", "", "", "", "", "", "", "", "", "", "", "", ""}, {"", "", "", "", "", "", "", "", "", "", "", "", ""}, {"", "", "", "", "", "", "", "", "", "", "", "", ""}, {"", "", "", "", "", "", "", "", "", "", "", "", ""}, {"", "", "", "", "", "", "", "", "", "", "", "", ""}, {"", "", "", "", "", "", "", "", "", "", "", "", ""}, {"", "", "", "", "", "", "", "", "", "", "", "", ""}, {"", "", "", "", "", "", "", "", "", "", "", "", ""}, {"", "", "", "", "", "", "", "", "", "", "", "", ""}, {"", "", "", "", "", "", "", "", "", "", "", "", ""}, {"", "", "", "", "", "", "", "", "", "", "", "", ""}, {"", "", "", "", "", "", "", "", "", "", "", "", ""}, {"", "", "", "", "", "", "", "", "", "", "", "", ""}, {"", "", "", "", "", "", "", "", "", "", "", "", ""} }; @SuppressWarnings("unchecked") public Class getColumnClass(int column) { return getValueAt(0, column).getClass(); } public int getColumnCount() { return columnNames.length; } public String getColumnName ( int column ) { return columnNames[column]; } public int getRowCount() { return data.length; } public Object getValueAt ( int row, int column ) { return data[row][column]; } public void setValueAt (Object value, int row, int column ) { data[row][column] = value; } }
package graphics; import java.awt.Component; import java.util.ArrayList; import javax.swing.JTable; import javax.swing.table.DefaultTableCellRenderer; import staticvar.staticvar; { /** * */ private static final long serialVersionUID = 1L; private ArrayList<Integer[]> coordinates = new ArrayList<Integer[]>(); public void addCoordinate(int row, int col) { coordinates. add(new Integer [] {row, col }); } public void removeCoordinate(int position) { coordinates.remove(position); } public Component getTableCellRendererComponent boolean hasFocus, int row, int col) { if(checkContainsCoordinates(row, col) != -1) { this.setBackground(staticvar.colors[1]); } else if(checkContainsCoordinates(row, col) == -1) { this.setBackground(staticvar.colors[0]); } return this; } public int checkContainsCoordinates(int row, int col) { for(int i = 0; i < coordinates.size(); i++) { if(coordinates.get(i)[0] == row && coordinates.get(i)[1] == col) { return i; } } return -1; } }
package staticvar; import java.awt.Color; public class staticvar { public static final Color [] colors = {Color. WHITE, }
Un saludo
|
|
|
En línea
|
|
|
|
|
Mensajes similares |
|
Asunto |
Iniciado por |
Respuestas |
Vistas |
Último mensaje |
|
|
JTable pintar en celldas
Java
|
sapito169
|
1
|
2,720
|
18 Enero 2009, 22:26 pm
por Chuidiang
|
|
|
JTable tamaño de celdas ajustable y mostrar texto completo
Java
|
marvic
|
1
|
29,578
|
12 Abril 2009, 19:57 pm
por marvic
|
|
|
Se podría hacer clic en un tabulador
Programación Visual Basic
|
Senior++
|
6
|
2,868
|
16 Junio 2011, 15:13 pm
por Senior++
|
|
|
Mostrar menú al hacer clic en un listview
ASM
|
.:UND3R:.
|
5
|
4,377
|
13 Mayo 2013, 03:27 am
por .:UND3R:.
|
|
|
Deshabilitar Editar Celdas JTable
Java
|
MiguelP
|
2
|
11,920
|
29 Abril 2014, 20:08 pm
por MiguelP
|
|