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.

Similar Messages

  • 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.

  • 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

  • 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

  • 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.

  • Ask split long text into two line into same cell for ALV

    Dear All,
    Is it possible split long text into two line into same cell for ALV display data?
    Regards,
    Luke
    Moderator message: please search for information before asking.
    Edited by: Thomas Zloch on Feb 24, 2011 10:12 AM

    Keep options: Select the paragraph, then CtrlAltK (Mac: CmdOptK, I
    think).
    If you want the paragraphs in pairs, create two paragraph styles. Style
    A has keep options set to start on a new page, and also, its Next Style
    is style B.
    Style B has no keep options, but it's Next Style is Style A.
    Select all the text.
    From the flyout menu of the paragraph styles palette, apply Style A
    "then next style."
    Now all paragraphs will be alternating -- style A, style B, style A, etc.
    Now do what I wrote initially, and you'll have pairs of paragraph in
    separate text frames.

  • Adding lines in single cell on spreadsheet

    How to add lines in single cell on spreadsheet?

    i think holding (CTRL or ALT) + Enter should work? Did you try those?

  • How to use line wrapping and line spacing in java?how to use line wrapping

    how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?how to use line wrapping and line spacing in java?

    Hello,
    You should post your question at:
    http://java.forum.sun.com
    Thanks
    - Rose

  • How to use line wrapping and line spacing in java?

    how to use line wrapping and line spacing in java?

    Hi,
    This is explained in the java Tutorial. Please see the link:
    http://java.sun.com/docs/books/tutorial/i18n/text/line.html
    and find some sample examples.
    Hope this helps,
    --Sun/DTS                                                                                                                                                                                                                                                                                                                                                                                       

  • Function: Return First Line of Another Cell (before line break)

    Howdy all!
    I have a bunch of cells that have multiple lines (line breaks created by pressing option-return). I want to create a function that will return the first line of a cell (IE: the text before the first line break). Any thoughts on how to do this?
    IE, if A1=
    Test 1
    Test 2
    Test 3
    Then I want to point the function from another cell to A1 and have it return Test 1.
    The code would look like this:
    =REPLACE(A1,SEARCH([line break],A1),LEN(A1)−SEARCH([line break],A1)+1,"")
    (IE: It searches for the line break, then replaces everything from that part on with nothing.)
    The problem is that I don't know how to get Numbers to think about [line break]. Thought that =char() would work but I don't know the unicode to put in (85, 2028, and 2029 didn't work).
    Thoughts?
    Matthew

    Thanks, I thought that I had tried that at some point, but I guess I hadn't done it correctly!
    I made one adjustment, I subtracted one from the SEARCH. The formula as you have it actually returns the line break along with the first line (you can't see it in your example because your rows are taller). (I also removed the third peramiter from the SEARCH, but that is a personal preference.) Outside of that this works perfectly. Thanks!
    B1=LEFT(A1,SEARCH("
    ",A1)−1)
    Matthew

  • How can I eliminate grid lines of selected cells when printing, in iOS numbers

    How can I eliminate grid lines of selected cells when printing, using iOS numbers

    Hi Cmlgfx,
    Thanks for visiting Apple Support Communities.
    You can style a table to remove grid lines and only show an "outline" around the table itself.
    See this article for help with table styles:
    Numbers for iOS (iPad): Place and style tables
    http://support.apple.com/kb/PH3365
    To show or hide the table name, table border, and grid lines, alternate row colors, or adjust fonts, tap Table Options at the bottom of the Table window (you may need to scroll up in the window to see it), and then set your preferences using the options in the Table Options window. (When you’re done, tap at the top of the window to see other options.)
    All the best,
    Jeremy

  • Firefox 4 does not wrap long lines.

    Firefox 4 is not wrapping long lines for www.lindasgourmetcookies.com. This worked in previous versions of Firefox. See "URL of affected site" for an example. Lines also wrap properly on IE

    There are a lot of typos in the CSS file: ';' instead of a ':' (font-size<u>;</u> 12px;), so you want to fix that first.<br />
    See the Tools > Error Console (Firefox > Web Developer)
    <pre><nowiki>.choicesbox2 a:visited {color:#0000bb; font-size; 12px;}
    .choicesboxl a:visited {color:#0000bb; font-size; 12px;}
    </nowiki></pre>
    A good place to ask questions and advice about web development is at the mozillaZine Web Development/Standards Evangelism forum.<br />
    The helpers at that forum are more knowledgeable about web development issues.<br />
    You need to register at the mozillaZine forum site in order to post at that forum.<br />
    See http://forums.mozillazine.org/viewforum.php?f=25

  • JTable - Dynamic wrapping of text in a cell.

    Hello,
    Can anyone tell me how to dynamically wrap the text in a particular cell of a JTable if it is to long to be displayed on one line? I don't care if it affects the row height. Basically, I have a table with 3 columns, the thrird of which is a "notes" column and allows up to 500 characters to be displayed. When the text is too long the table truncates it with an elipsis (...) and the user can't see the rest. I would like the text to wrap within the cell (even if the height has to change) so that the user can read the entire note more easily.
    Thank you,
    G Poole

    You should probably repost this in the swing forum

  • Remove grid lines from a cell

    Hi,
    I'm trying to hide the griid lines of an specific cell in a table.
    First I tried to make a DefaultTableCellRenderer with no results (setting border to null).
    What I want is similar to do table.setShowGrid(false); but not for all the table, only for the cells I want (I know the setShowVerticalLines(boolean) and setShowHorizontalLines(boolean) functions).
    I'been using the searcher and googling with no results, all help is good :)
    The result I hope is a board like this:
    |_|_|_|_|_ _
    |_|_|_|_|_|_|_
    |_|_|_|_|_|_|_|_
    Any idea?
    PD: Sorry for my english.

    I think I found a solution. If I set ShowGrid to false and I manage the borders manually I can build the form that I want, for example with this render:
    public class TableroRender extends DefaultTableCellRenderer
         private static final long serialVersionUID = 1L;
         public Component getTableCellRendererComponent(JTable table,
         Object value,
         boolean isSelected, boolean hasFocus, int row, int column)
              super.getTableCellRendererComponent(table, value, isSelected,
              hasFocus, row, column);
              setOpaque(true);
              if(column==0)
                   setBorder(new MatteBorder(1,0,1,0,Color.red)) ;
                      //setBackground(new Color(192, 192, 192));
              return this;
    }What do you think?
    Edited by: petovi on Nov 28, 2007 7:41 AM

  • 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.

Maybe you are looking for

  • Problem loading table in SQL server

    Hi, I'm trying to load a table in SQL server from another instance of SQL server. I have defined the physical and and logical data stores and reverse engineered the models to retrieve the tables. The target table was created manually.. If I try to ru

  • Need help with inverter program

    I am very new to labview and I need a lot of help fast. I have been given this vi program, which I have attached, that operates a bunch of inverters. The program is driving the inverter which in turn drives the agitator on a washing machine. The agit

  • One PC, Multiple Apple ID's, how to integrate?

    Im not sure if this problem of mine is as a result of the latest iTunes update, but suddenly Im getting told that I can't download my purchases as for some reason my son's apple id is now the only one that can be associated with the pc (for 90 days..

  • How do I uninstall Pages 4.3 after loading Pages 5.2.2

    I saved a document created in Pages 5.2.2 and put it in the dock for easy access but when I try to open it I get the message saying I need a newer version of Pages to open it. I have now found that my computer is trying to open it using Pages 4.3 whi

  • Password Protected File Problem

    Hi I am using Acrobat 10.1.13 using windows 8.1 and when I try to protect a pdf I created from being edited (no password required to open the file) and when compatibility is set to Acrobat X and Later, I keep running into issues.  If I try to email i