Swing Problem : Wrap Multiple line in a Cell (JTable)

Hello...
I'm doing a swing app. The functions included are to print the Jtable...
in Colum 3 i've applied wrapping word in a cell.. the problem is when i print the table, the border in column 3 which wrap the word will not be visible...y?how can i solve this problem...can see my code like below
MyCellRenderer cellRenderer = new MyCellRenderer(); 
TableColumn column = null;
  column = tblProgram.getColumn(ResourceBundle.getBundle("Program").getString("TBL_DESC"));
//get data in column 3  
column.setCellRenderer(cellRenderer);
  public class MyCellRenderer extends JTextArea implements TableCellRenderer {
        public MyCellRenderer() {
          setLineWrap(true);
          setWrapStyleWord(true);
          setMargin(new Insets(0, 5, 0, 5));
      public Component getTableCellRendererComponent(JTable table, Object
              value, boolean isSelected, boolean hasFocus, int row, int column) {
           setText((String)value);
          setSize(table.getColumnModel().getColumn(column).getWidth(),
                  getPreferredSize().height);
          if (table.getRowHeight(row) != getPreferredSize().height) {
                  table.setRowHeight(row, getPreferredSize().height);
          return this;
   } ty in advance :D

Multi-Post:
http://forum.java.sun.com/thread.jspa?threadID=741629&tstart=20
Thanks for fixing the code format tags, next time just edit the post instead of reposting.

Similar Messages

  • Wrap Mulitiple line in a Cell (JTable)

    Hello Everyone. Here is code that I am trying to work at. I am selecting data from database and displaying using JTable, but second column is too long and it won't look nice. I want to wrap lines within cell. Code below is working except it is not doing line wrap. I really want help...Soon the better.
    import java.sql.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.table.*;
    import java.awt.*;
    public class JTableJDBC extends GUIJDBC {
        JTable dataTable = new JTable();
        JScrollPane dataTableScrollPane = new JScrollPane();
        public static void main(String args[]) {
         JTableJDBC jtJDBC = new JTableJDBC("JDBC Program with JTables");
         jtJDBC.show();
         jtJDBC.pack();
        public JTableJDBC(String title) {
         super(title);
         dataTableScrollPane.setViewportView(dataTable);
         getContentPane().add("Center",dataTableScrollPane);//from GUIJDBC
        public void actionPerformed(ActionEvent evt) {
         String sel = inputText.getText().trim();
         String query = "Select * " +
             "from ERROR " +
             "where ERRORCODE LIKE '" + sel + "' ORDER BY ERRORCODE";
         System.out.println("Display Query: " + query);
         ConnectionJDBC CJ = new ConnectionJDBC(); //from ConnectionJDBC
         Connection dbConnect = null;
         Statement dbStatement = null;
         ResultSet dbRS = null;
         try {
             dbConnect = CJ.makeConnection(); //from ConnectionJDBC
             dbStatement = dbConnect.createStatement();
             dbRS = dbStatement.executeQuery(query);
             presentResultSet(dbRS);
         } catch (SQLException sqlex) {
             JOptionPane.showMessageDialog(null,sqlex.getMessage());
         finally {
             CJ.closeConnection(dbConnect,dbStatement);
        public void presentResultSet(ResultSet rs)
         throws SQLException {
         Vector dataVector = new Vector();
         if (!rs.next())
             JOptionPane.showMessageDialog(null,"No records for customer");
         else {
             do {
              Vector rowVector = new Vector();
              rowVector.addElement(rs.getString("ERRORCODE"));
              rowVector.addElement(rs.getString("ERRORDESCRIPTION"));
              dataVector.addElement(rowVector);
              } while (rs.next());
         Vector headVector = new Vector(2);
         headVector.addElement("ERROR CODE");
         headVector.addElement("ERROR DESCRIPTION");
         dataTable = new JTable(dataVector, headVector){
                public Component prepareRenderer(TableCellRenderer r, int row, int col)
                        Component c = super.prepareRenderer(r, row, col);
                        if (col == 0 && !isCellSelected(row, col))
                            Color bg = new Color(210, 211, 230);
                            c.setBackground(bg);
                            c.setForeground(Color.black);
                        else if (col == 1 && !isCellSelected(row, col))
                            Color bg = new Color(237, 239, 255);
                            c.setBackground(bg);
                            c.setForeground(Color.black);
                        else if (col == 2 && !isCellSelected(row, col))
                            Color bg = new Color(220, 237, 255);
                            c.setBackground(bg);
                            c.setForeground(Color.black);
                        else if (col == 3 && !isCellSelected(row, col))
                            Color bg = new Color(220, 237, 255);
                            c.setBackground(bg);
                            c.setForeground(Color.black);
                        else if (col == 4 && !isCellSelected(row, col))
                            Color bg = new Color(220, 237, 255);
                            c.setBackground(bg);
                            c.setForeground(Color.black);
                        else if (col == 5 && !isCellSelected(row, col))
                            Color bg = new Color(220, 237, 255);
                            c.setBackground(bg);
                            c.setForeground(Color.black);
                        else if (col == 6 && !isCellSelected(row, col))
                            Color bg = new Color(220, 237, 255);
                            c.setBackground(bg);
                            c.setForeground(Color.black);
                        else if (col == 7 && !isCellSelected(row, col))
                            Color bg = new Color(220, 237, 255);
                            c.setBackground(bg);
                            c.setForeground(Color.black);
                  else {
                             //Default
                             c.setBackground(getBackground());
                             c.setForeground(getForeground());
                return c;
              public boolean isCellEditable(int row,int column)
                            return false;
              public String getToolTipText(MouseEvent event)
                   int row = rowAtPoint( event.getPoint() );
                   int col = columnAtPoint( event.getPoint() );
                   Object o = getValueAt(row,col);
                   if( o == null )
                        return null;
                   if( o.toString().equals("") )
                        return null;
                   return o.toString();
         dataTable.setPreferredScrollableViewportSize(new Dimension(500, 80));
         int vColIndex = 0;
         int width = 100;
         TableColumn col = dataTable.getColumnModel().getColumn(vColIndex);
             col.setMinWidth(width);
             col.setMaxWidth(width);
             col.setPreferredWidth(width);
         dataTable.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
         dataTableScrollPane.setViewportView(dataTable);
         dataTable.setGridColor(Color.black);
    }Thank you in advance.

    Can you share and example of JTextArea?
    I would like to do: When customer right click's or just clicks on a particular row I want to display another window showing them content of that row in JTextArea. Can I do that and if show how can I acheive that with above code. Please let me know or email me at [email protected]
    Thanks a lot. I think you are understand exactly what I am trying to acheive. If you have any question then please feel free to email me and I am also check this forum every few min.
    This is my first Swing application and I am trying to do my best. All your help will be appreciated.

  • Multiple lines in a cell in excel download

    Hi,
    I have used insert_full of i_oi_spreadsheet interface for downloading internal table.. i have a text field which i need to display in multiple lines in one cell.
    Is that possible? Please help,
    Regards,
    Rohit.

    Rohit,
    Clipboard this code into your editor and see if it helps you out.  Please reward points.
    *& Report  ZR_SANDBOX_PROG
    REPORT Zsandbox_prog .
    INCLUDE OLE2INCL.
    DATA: hExcel TYPE OLE2_OBJECT,      " Excel object
          hWorkBooks TYPE OLE2_OBJECT,  " list of workbooks
          hWorkbook TYPE OLE2_OBJECT,   " workbook
          hSheet TYPE OLE2_OBJECT,      " worksheet object
          hRange TYPE OLE2_OBJECT,      " range object
          hRange2 TYPE OLE2_OBJECT,      " range object
          hBorders TYPE OLE2_OBJECT,    " Border object
          hInterior TYPE OLE2_OBJECT,   " interior object - for coloring
          hColumn TYPE OLE2_OBJECT,     "column
          hCell TYPE OLE2_OBJECT,       " cell
          hFont TYPE OLE2_OBJECT,       " font
          hSelected TYPE OLE2_OBJECT,   " range object
          hPicture TYPE OLE2_OBJECT,    "picture object
          hLogo TYPE OLE2_OBJECT.       "Logo object
    types:  begin of t_Excel,
              Period_Literal(20),
              Pd_Wk_Literal(20),
              Coop_Literal(40),
              Sugg_Price(20),
              Cost(20),
              Margin(20),
              Comments(100),
            end of t_Excel.
    data: r_Excel type t_Excel.
    data: i_Excel type table of t_Excel with header line.
    field-symbols: <Val> type any.
    data: col_Cnt type i.
    data: row_Cnt type i.
    data: l_range(30).
      parameters: wraptext as checkbox.
      CREATE OBJECT hExcel 'EXCEL.APPLICATION'.
      PERFORM ERR_HDL.
    get list of workbooks, initially empty
      CALL METHOD OF hExcel 'Workbooks' = hWorkbooks.
      PERFORM ERR_HDL.
    add a new workbook
      CALL METHOD OF hWorkbooks 'Add' = hWorkbook.
      PERFORM ERR_HDL.
    Get Worksheet object.
      get property of hWorkbook 'ActiveSheet' = hSheet.
      SET PROPERTY OF hExcel  'Visible' = 1.
      row_cnt = 1.  col_cnt = 1.
      do 5 times.
        Perform Load_Dummy_Values.
      enddo.
    *Pass the internal table values to the spreadsheet.
      loop at i_Excel into r_Excel.
        do 7 times.
          assign component sy-index of STRUCTURE r_excel TO <Val>.
          PERFORM Fill_The_Cell USING row_cnt col_cnt 0 <Val>.
          col_cnt = col_cnt + 1.      "increment column
        enddo.
        row_cnt =   row_cnt + 1.    "increment row
        col_cnt = 1.                  "reset column to A (ie. col 1)
      endloop.
      if not WrapText is initial.
        move 'G:G' to l_range.
        CALL METHOD OF hExcel 'RANGE' = hRange EXPORTING #1 = l_range.
        set property of hRange 'WrapText' = 1.
      endif.
    Release excel.
      FREE OBJECT hExcel.
      PERFORM ERR_HDL.
    FORM Fill_The_Cell USING I J BOLD TheValue.
      CALL METHOD OF hExcel 'Cells' = hCell EXPORTING #1 = I #2 = J.
      PERFORM ERR_HDL.
      SET PROPERTY OF hCell 'Value' = TheValue.
      PERFORM ERR_HDL.
      GET PROPERTY OF hCell 'Font' = hFont.
      PERFORM ERR_HDL.
      SET PROPERTY OF hFont 'Bold' = BOLD.
      PERFORM ERR_HDL.
    ENDFORM.
    *&      Form  ERR_HDL
          outputs OLE error if any
    -->  p1        text
    <--  p2        text
    FORM ERR_HDL.
      IF SY-SUBRC <> 0.
        message i000(zz) with 'OLE Automation error: ' SY-SUBRC.
        exit.
      ENDIF.
    ENDFORM.                    " ERR_HDL
    Form Load_Dummy_Values.
      r_excel-Period_Literal = 'Period 1'.
      r_excel-Pd_Wk_Literal = 'P1 / Wk1'.
      r_excel-Coop_Literal = 'Promo Event'.
      r_excel-Sugg_Price = '$ 1.25'.
      r_excel-Cost  = '$ 0.50'.
      r_excel-Comments =
       'This is a really long field with one hundred bytes to it.'.
      append r_excel to i_Excel.
    EndForm.

  • How do i add multiple lines in a cell (like a list) in Excel for MAC?

    I'm trying to add multiple lines (in the form of a list) in a individual cell in Excel for MAC.  I used ALT Enter on my PC but that doesn't work on the iMac.  Does anyone know how to do this?
    Thanks!

    It's been a while but I think you hold SHIFT while typing a page break (RETURN) to make a new line without shifting to another cell.
    However, as Excel isn't an Apple product, I am sure you will get a faster and more current answer by using Microsoft's Office:Mac forums here:
    Office for Mac forums
    They are very good.

  • How do I add multiple lines to a cell

    I am trying to create an assignment sheet in Numbers. I am using the "Schedule" template. Under each day of the week, I want to add daily assignments. But each assignment may need multiple lines. In the screenshot below, I want to add another line under "Genesis 5" but still have it within the same cell. How can I do that?

    I do extensive research  build on the work of others

  • Problem appending multiple lines with StringBuffer()

    I have problem executing this code -
    I am reading a line from keyboard. And after every line I store a space as well.
    O/p shows just the first line, space and then null.
    Why would it not print the next lines.
        StringBuffer buf = new StringBuffer();
    do {
    str = br.readLine();
        buf.append(str).append(" ");
       } while (str != null);

    Well, when I input 3 lines and press ^C to end the
    input , the above lines get appended but the last
    line does not append.Why are you pressing ^C? That should make your app abend if you're reading from the System.in stream.
    Anyway, I'd guess you're entering the last line without pressing Enter, so it doesn't actually get entered (nor read).

  • How to display a long text with several in multiple lines in template cell

    Hi,
       here i have one string such as 'Power MOS_TestTestTest'. But it will display in the template cell as
       'Power
       MOS_TestTestTest'.
       And what i want is 'Power MOS_TestTes
                                   tTest'.
      How to achieve this ? ths

    Hi,
    If the Width of the Templete in not sufficient to display whole text then from the next word it is dislpayed in the next line. As in your case ..
    'Power
    MOS_TestTestTest'.
    If you want to have this like
    'Power MOS_TestTes
    tTest'.
    You need to write the logic to split the string as per your rquirement and print them.

  • Several line in a cell

    is it possible to create several line in one cell in a table?

    it's nothing to do with wrapping text, i just want to create multiple line in one cell,
    i want to create
    cell1   cell2     cell3
    ab       ab1     1
              ab2     
              ab3     
    but..... ab1, ab2, and ab3 in one cell

  • Adding multiple rows to a cell?

    Is it possible to add multiple rows of text to a single cell? I am trying to create a workout log. Typicaly I would record several exercises with weights and repititions to a single days workout. Is this possible in Numbers?
    Suggestion?
    Thanks.

    Hi Dcneuro,
    I am trying to create a workout log. Typicaly I would record several exercises with weights and repititions to a single days workout.
    You can enter multiple lines of text in a cell... But I'm wondering why you would want to do that. A log would be more useful if you structure it like this, one exercise to a row and repeat the date where appropriate:
    If you structure it that way or similarly (rather than entering multiple lines in a cell) then you can then use Numbers to do the things spreadsheet software is designed to do: derive summary statistics, charts, etc.  The formulas in this summary table are:
    B2, copied down:  =COUNTIF(Log::B,A2)
    C2, copied down: =SUMIF(Log::B,A2,Log::D)
    D2, copied down: =AVERAGEIF(Log::B,A2,Log::D)
    E2, copied down: =AVERAGEIF(Log::B,A2,Log::C)
    Obviously these workout numbers make no sense. This is just a simple example to give you an idea of the kinds of things you can do if you structure your log properly.
    Also, have a look at the Running Log template (File>New>Personal>Running Log on the Mac, or on the iPad: + then Create Spreadsheet then scroll down to the Personal section).
    SG

  • Multiple Lines in a Single ALV Grid Cell

    Is there any way to Display Multiple Lines in a Single ALV Grid Cell.
    This can be accomplished by Sorting the First 3 fields and Make it Looks like below displayed format.
    But My problem is while downloading also it should be Displayed in the same format.
    All inputs are highly appreciated.

    there was a post similar to this some days back... search it..
    any ways...
    what u can do is...
    arrange ur final internal table so that it will look like same line...
    like...lets say ur internal table is :
    A1 B1 C1 D1 123
    A1 B1 C2 D2 123
    A1 B2 C1 D1 123
    A1 B2 C2 D2 123
    and u can rearrange ur table to be like
    A1 B1 C1 D1 123
          C2 D2 123
    A1 B2 C1 D1 123
          C2 D2 123
    what i mean to say is dont pass the fields if they are same in the previous line... try it out..
    and then pass it two ur ALV.
    it will look like they are in one line
    Edited by: soumya prakash mishra on Jun 17, 2009 1:31 PM

  • Multiple Lines in a Jtable Cell

    I am trying to place multiple lines in a single cell of Jtable
    The text Of the cell comes from the database.I am trying to do it using a JLabel .If anyone can help me please do so
    thanks
    kishore
    [email protected]

    It can be done but it is quite a lot of work. Basically you have to produce your own cell renderer and use that instead. A good book on swing will tell you how to do it.

  • How to wrap a line in a table cell in adf

    How do I wrap a line of text in a table cell?

    Yes, I tried that. But I have 2 sets of information. I want it to wrap exactly at the start of the second piece of information
    So I am trying to use noWrap in combination with width. How else can I do this. appending \n does not work

  • Problem in printing multiple line items

    Hi,
       i designed a SAPSCRIPT FORM.When i execute the t-code MB01 by giving PO which has multiple line items ,  output type is invisible.
    But when i iexecute the t-code MB01 by giving PO which has single item , output type is visible and form is printing.
    Do i need to copy the print program and make changes in there or if i change elements in the form will that helps in printing multiple line items.
    thanks.

    HI,
    wat i understand is u r trying to loop at a variable window .but tht is not suggestable.
    write a loop in program lines. read the values in to different variables and the try printing them as text elements that will solve the problem.
    or else in other way use a template and that can also solve the problem.

  • Table Cell Editor which allows to input multiple lines of text...

    Hi there
    Does anyone know how to write a table cell editor which allows users to input multiple lines of text? please provide some sample if possible...
    Thanks
    Ken

    I'm assuming you also want the renderer to display multiple lines? if so, make a class that extends JTextArea and that implements the TableCellEditor and TableCellRenderer interfaces, then set instances of this as the editor and renderer for the TableColumn in question. The implementation of most of the methods in these interfaces is trivial, often just this.setBackground() based on the table.getSelectionBackground() or table.getBackground(), a this.setText() based on the value passed in, then just a 'return this'.
    You might want to make an abstract class which implements these interfaces for you, and which delegates to a simple abstract method that you override to return the renderer/editor component.
    Note that you must use two instances of the class you make, one for the renderer and one for the editor, since it must be able to be able to render the remainder of the table's cells without interfering with the JTextArea performing the editing. (alternatively you could make two classes that extend JTextArea, each just implementing one of the interfaces, but this is more effort I think.) Also note that you must increase the table's row height to get more than one row visible.

  • How to wrap the too big string name in multiple line

    Hi All,
    String s="Diwali_Photos_For_Testing_Purpose_Test"
    How to wrap this string into multiple lines in jsp.
    by setting the width of the outputText component, it is not wrapping into multiple lines.
    If the String s="Deputy chief executive takes up position at Hibernian"
    then it will be wrapped in next line..

    In IE you can apply CSS property 'word-wrap' on that field with the value 'break-word'. For other browsers you'll have to write some JS yourself which breaks long words with spaces or even do it already at the server side.

Maybe you are looking for