aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes problem in JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "problem in JTable" Watch "problem in JTable" New topic
Author

problem in JTable

Anagha Medicharla
Greenhorn

Joined: Feb 06, 2001
Posts: 11
Hi everybody,
I am using JTable.I want rows that takes multiline text.
I have written the code as follows.what is happening is when I press enter on a cell of that row it's going to next row(means it's is not going to next line of same cell but it is crearing new row and it's gong to that row.)
Can anybody help me .
I am giving cod here:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import javax.swing.DefaultCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.JScrollPane;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.JOptionPane;
import javax.swing.border.*;
public class ConersationLogInfo extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JScrollPane jScrollPane1 = new JScrollPane();
//Construct the frame
public ConersationLogInfo() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception{
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(576, 300));
this.setTitle("CONVERSATION LOG");
jPanel1.setBorder(BorderFactory.createLineBorder(Color.black));
jPanel1.setMinimumSize(new Dimension(80, 80));
jPanel1.setPreferredSize(new Dimension(80, 80));
jPanel2.setBorder(BorderFactory.createLineBorder(Color.black));
jPanel2.setLayout(null);
jScrollPane1.setBounds(new Rectangle(1, 10, 573, 218));
contentPane.add(jPanel1, BorderLayout.NORTH);
contentPane.add(jPanel2, BorderLayout.CENTER);
jPanel2.add(jScrollPane1, null);
MyTableModel myModel = new MyTableModel(){
public Class getColumnClass(int columnIndex) {
return String.class;
}
};
JTable table = new JTable(myModel);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
int lines = 2;
table.setRowHeight( table.getRowHeight() * lines);
table.setDefaultRenderer(String.class,new MultiLineCellRenderer());
jScrollPane1.getViewport().add(table, null);

}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
}
class MyTableModel extends AbstractTableModel {
final String[] colheads={"DATE","TIME","CALLER_ID","REASON","CONVERSATION INFO"};
final String[][] data={{"","","","",""}, {"","","","",""}};
public int getColumnCount() {
return colheads.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return colheads[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
/*
* Don't need to implement this method unless your table's
* editable.
*/
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.

return true;
}
}
class MultiLineCellRenderer extends JTextArea implements TableCellRenderer {
public MultiLineCellRenderer() {
setLineWrap(true);
setWrapStyleWord(true);
setOpaque(true);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
setFont(table.getFont());
if (hasFocus) {
setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") );
if (table.isCellEditable(row, column)) {
setForeground( UIManager.getColor("Table.focusCellForeground") );
setBackground( UIManager.getColor("Table.focusCellBackground") );
}
} else {
setBorder(new EmptyBorder(1, 2, 1, 2));
}
setText((value == null) ? "" : value.toString());
return this;
}
}
Anagha
Brett Spell
Ranch Hand

Joined: Feb 24, 2001
Posts: 96
Anagha,
You need to create a JTextArea-based editor for your JTable. The default editor (which is what you're using) uses a JTextField and stops editing when the Enter key is pressed.
------------------
Brett Spell
Author, Professional Java Programming
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
Anagha:
when posting huge code samples like this, please,
use the opening tag [ code ] and closing tag [ /code ]
without the spaces, this will indent the code.
Addl. tags are defined at http://www.javaranch.com/ubb/ubbcode.html
Thanks for your cooperation.
Also, you could edit your posts by clicking on the
notepad/pencil icon.
regds.
- satya

Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in JTable