Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

problem in JTable

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Would you like to try a free sample? Today we are featuring tiny ads:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic