Saturday, March 5, 2011

Write Message In Baby Book

Moving Image and Text Place cell in a JTable

Place Image and Text in a JTable cell

already seen as placing some components in the cells of a JTable, but this time not all cells in a column, we'll see We can put a label with image and text in the cell that we want.

Images





Code


Top Class


package classes;


import java.awt.BorderLayout;
import java.awt.event .* ;
import javax.swing .*;
import javax.swing. table.DefaultTableModel;


public class Main extends JApplet {

JTable table;
CeldaConJLabel editor;
JScrollPane scroll;
DefaultTableModel model;
int row = 0;
int col = 0;
ButtonGroup group;
JRadioButton r1, r2;
JButton place;
ImageIcon img1, img2;
JLabel lseleccionada;
JTextField txt;

public void init () {

model = new DefaultTableModel (2, 3);
tabla = new JTable (model);


CeldaConJLabel editor = new (this);
tabla.getColumnModel (). getColumn (0). setCellRenderer (editor);
tabla.getColumnModel (). getColumn (1). setCellRenderer (editor);
tabla.getColumnModel (). GetColumn (2). SetCellRenderer (editor);
tabla.setRowHeight (35);

tabla.addMouseListener (new MouseListener ( ) {



@ Override public void mouseClicked (MouseEvent e) {
/ / TODO Auto-generated method stub
tabla.getSelectedRow row = ( )
tabla.getSelectedColumn col = ();
}



@ Override public void mouseEntered (MouseEvent e) {
/ / TODO Auto-generated method stub

}



@ Override public void mouseExited (MouseEvent e) {
/ / TODO Auto-generated method stub

}



@ Override public void mousePressed (MouseEvent e) {
/ / TODO Auto-generated method stub



}


@ Override public void mouseReleased (MouseEvent e) {
/ / TODO Auto-generated method stub

}

});

scroll = new JScrollPane (table);
add (scroll);

pabajo JPanel = new JPanel ( )
txt = new JTextField (15);
pabajo.add (new JLabel ("Text"));
pabajo.add (txt);
ButtonGroup group = new ();
img1 = new ImageIcon (this.getClass (). getResource ("../ lib/img1.png "));
img2 = new ImageIcon (this.getClass (). GetResource ("../ lib/img2.png "));
r1 = new JRadioButton ("");
r1.setSelected (true);
r2 = new JRadioButton ("");
grupo.add (r1);
grupo.add (r2);
put = new JButton ("Put");
colocar.addActionListener (new ActionListener () {


@ Override
public void actionPerformed(ActionEvent arg0) {
if(r1.isSelected()){
if(lseleccionada!=null){
lseleccionada.setIcon(img1);
lseleccionada.setText(txt.getText());
}
}else{
if(lseleccionada!=null){
lseleccionada.setIcon(img2);
lseleccionada.setText(txt.getText());
}
}}


});

pabajo.add (r1);
pabajo.add (new JLabel (img1));
pabajo.add (r2);
pabajo.add (new JLabel (img2));
pabajo.add (put);

add (pabajo, BorderLayout.SOUTH);

}}



Class CeldaConJLabel


package classes;


import java.awt.Component;
import java.util.ArrayList;
import java.util.EventObject;


import javax.swing.JLabel;
import javax.swing.JTable;
import javax. swing.event.CellEditorListener;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;


public class CeldaConJLabel implements TableCellRenderer{


Principal p;
JLabel l;

public CeldaConJLabel(Principal prin){
p=prin;
l=new JLabel("");
}

@Override
Component public getTableCellRendererComponent (JTable table, Object arg1,
boolean isSelected, boolean arg3, int row, int col) {
/ / TODO Auto-generated method stub
if (isSelected & & row & & col == == p.fila p.col) {
p.lseleccionada = l;
tabla.repaint ();
return l;
} else {
tabla.repaint ();
return null;
}
}}


The main class creates a JTable, DefaultTableModel object, an object of class CeldaConJLabel, also creates two radioButton to select images, a JTextField for the text to be displayed and a JButton to position text and image in the selected cell.

By selecting the cell and press the button to place, we tell the table that displays the image and text in the selected cell.

CeldaConJLabel class implements TableCellRenderer interface, and overwrites the getTableCellRendererComponent method, which allows us to modify an object that is returned to the selected cell, in this case a JLabel, but can be any component.

Applet

must first select the cell that you want to add the image with the text.

0 comments:

Post a Comment