Setting Editable columns in JTable

I am trying to create a table where the first column is uneditable. I am guessing I want to use the isCellEditable method at some point? But this seems to be a query, rather than a setting. Can somebody explain how I go about making this setting?

Swing related questions should be posted in the Swing forum.
I am guessing I want to use the isCellEditable method at some point?Good guess. If you don't know how to use a method, then search the forum using the method name as a keyword to find postings that already have example of using this method. A lot faster then posting a question and hoping someone answers it.
I prefer overriding the isCellEditable method of JTable.

Similar Messages

  • How to set a column in JTable non-editable?

    I have a JTable, and how can I set a particular column non-editable?
    Thank you.

    You could create a sub class of DefaultTableModel, you pass the column number to the constructor or you add a method like setColumnEditable(0, false); to set column 0 non-editable.
    You overwrite the method isCellEditable so it will return false when the column number that is passed to the method is 0 (if you want to have column 0 non-editable).

  • Editable Columns in JTable

    I have a JTable that I want the cells in the first two columns to be NOT EDITABLE and the third column cells EDITABLE. How can this be accomplished?
    Thanks
    Doopsterus

    public boolean isCellEditable(int row, int column)
         int modelColumn = convertColumnIndexToModel( column );
         return (modelColumn > 1);
    }

  • How to set different renderers to different cells in same column of JTable?

    Hello Friends,
    I need your help again...
    Does any body knows, how to set different renderer's for different cells of same column in JTable..??
    For ex.
    Col1 Col2 Col3 Col4
    A       A       A      A
    A       A       A      B
    A       A       A      C
    A       A      A       D
    Where A B C D would be different Renderers.  I want set exactly same ,,, ie. one column with different renderer at different cell positions..
    Right now i m setting renderer using statement bellow :
    table.getColumnModel().getColumn(int).setCellRenderer(rederer_Instance);But with this, effect in the last renderer is applicable whole column....
    Can any body help me out ?????????
    please refer this thread for similar kind of discussion...
    http://forums.sun.com/thread.jspa?forumID=57&threadID=571445Thanks
    Suyog

    Please refer to the first reply of [this thread|http://forums.sun.com/thread.jspa?forumID=57&threadID=571445] for the answer. If you have a specific problem implementing it, post you code with a specific question.

  • Can't set custom column widths in JTable

    I'm trying to set custom columns widths. They are set as I wish but then they are reset to default values (all columns have the same size). This is caused by method JFrame.setVisible(true) which invokes JTree.setWidthsFromPreferredWidths(). It can be investigated from following stack trace printed when a column being resized (see below).
    How to prevent this auto width sizing?
    P.S. can't override JTable.setWidthsFromPreferredWidths(); seems it's private
    table = new JTable(model) {
         public void columnMarginChanged(ChangeEvent event) {
              new Exception("stack trace").printStackTrace();
              super.columnMarginChanged(event);
    // setting here custom columns widths via table column model ...java.lang.Exception: stack trace
         at mtu.gui.TrackList$1.columnMarginChanged(TrackList.java:30)
         at javax.swing.table.DefaultTableColumnModel.fireColumnMarginChanged(DefaultTableColumnModel.java:615)
         at javax.swing.table.DefaultTableColumnModel.propertyChange(DefaultTableColumnModel.java:679)
         at javax.swing.event.SwingPropertyChangeSupport.firePropertyChange(SwingPropertyChangeSupport.java:264)
         at javax.swing.event.SwingPropertyChangeSupport.firePropertyChange(SwingPropertyChangeSupport.java:232)
         at javax.swing.table.TableColumn.firePropertyChange(TableColumn.java:249)
         at javax.swing.table.TableColumn.firePropertyChange(TableColumn.java:255)
         at javax.swing.table.TableColumn.setWidth(TableColumn.java:482)
         at javax.swing.JTable$2.setSizeAt(JTable.java:2242)
         at javax.swing.JTable$5.setSizeAt(JTable.java:2336)
         at javax.swing.JTable.adjustSizes(JTable.java:2372)
         at javax.swing.JTable.adjustSizes(JTable.java:2340)
         at javax.swing.JTable.setWidthsFromPreferredWidths(JTable.java:2250) <======
         at javax.swing.JTable.doLayout(JTable.java:2165)
         at java.awt.Container.validateTree(Container.java:1089)
         at java.awt.Container.validateTree(Container.java:1096)
         at java.awt.Container.validateTree(Container.java:1096)
         at java.awt.Container.validateTree(Container.java:1096)
         at java.awt.Container.validateTree(Container.java:1096)
         at java.awt.Container.validateTree(Container.java:1096)
         at java.awt.Container.validateTree(Container.java:1096)
         at java.awt.Container.validateTree(Container.java:1096)
         at java.awt.Container.validateTree(Container.java:1096)
         at java.awt.Container.validateTree(Container.java:1096)
         at java.awt.Container.validateTree(Container.java:1096)
         at java.awt.Container.validateTree(Container.java:1096)
         at java.awt.Container.validate(Container.java:1064)
         at java.awt.Window.show(Window.java:455)
         at java.awt.Component.show(Component.java:1134)
         at java.awt.Component.setVisible(Component.java:1089) <=====
         at mtu.gui.Test.test_03(Test.java:56)
         at mtu.gui.Test.main(Test.java:29)
         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
         at java.lang.reflect.Method.invoke(Method.java:324)
         at com.intellij.rt.execution.application.AppMain.main(Unknown Source)

    When I create the table I have code like this:TableColumn col = getColumnModel().getColumn(2);
    col.setMinWidth(width + 4);
    col.setMaxWidth((int)(width * 1.2));Works just fine for me. The setWidthsFromPreferredWidths() method will use these limits as far as I can see.
    PC&#178;

  • HELP Friends!!! How to set the width of columns of JTABLE?

    Hi!
    How to set the width of a particular column in JTable?
    I tried using headerRenderers and though I'm able to set the color and font of the header (using getTableCellRendererComponent method)I can't change the size.
    Pls some one help me..it's very urgent.
    Thanks in advance.
    Regards,
    Naval

    JTable.getColumnModel().getColumn(i).setWidth(width). There are also setPreferredWidth, setMaxWidth and setMinWidth methods.

  • How can i set the column width in the jtable?

    how can i set the column width in the jtable?
    can anybody send me a simple example??

    TableColumn column = table.getColumnModel().getColumn( columnIndex );
    column.setWidth( desiredColumnWidth);
    column.setMinWidth( desiredMinColumnWidth);
    column.setMaxWidth( desiredMaxColumnWidth);

  • How can i make perticular row or perticular cell Editable  of a JTable

    Dear al,
    can u help me by guiding me for the problem of...
    i am having a JTable, in which a (first)column of each row is having a checkbox field.
    If the checkbox is checked then and then i should able to modify the cells in that row where the checkbox is.
    I have created the table with AbstractTableModel of which the isCellEditable(int row, int col) method is overwriten. Whatever return value (true/false) reflects the perticular
    cells becomes editable/non-editable respectively.
    but at run time...(mean the table is created now) and now i want to make the cells editable/non-editable depending on the checkbox value...
    how can i implement it.........
    please suggest.........
    thank you.........

    here is the sample code from tutorial.......
    * TableRenderDemo.java requires no other files.
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import javax.swing.DefaultCellEditor;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.AbstractTableModel;
    import javax.swing.table.DefaultTableCellRenderer;
    import javax.swing.table.TableCellRenderer;
    import javax.swing.table.TableColumn;
    * TableRenderDemo is just like TableDemo, except that it explicitly initializes
    * column sizes and it uses a combo box as an editor for the Sport column.
    @SuppressWarnings("serial")
    public class TableRenderDemo extends JPanel {
         private boolean DEBUG = true;
         public TableRenderDemo() {
              super(new GridLayout(1, 0));
              JTable table = new JTable(new MyTableModel());
              // table.setEditingColumn(0);
              // table.editCellAt(0, 0);
              table.setPreferredScrollableViewportSize(new Dimension(500, 100));
              // Create the scroll pane and add the table to it.
              JScrollPane scrollPane = new JScrollPane(table);
              // Set up column sizes.
              initColumnSizes(table);
              // Fiddle with the Sport column's cell editors/renderers.
              setUpSportColumn(table, table.getColumnModel().getColumn(2));
              // Add the scroll pane to this panel.
              add(scrollPane);
          * This method picks good column sizes. If all column heads are wider than
          * the column's cells' contents, then you can just use
          * column.sizeWidthToFit().
         private void initColumnSizes(JTable table) {
              MyTableModel model = (MyTableModel) table.getModel();
              TableColumn column = null;
              Component comp = null;
              int headerWidth = 0;
              int cellWidth = 0;
              Object[] longValues = model.longValues;
              TableCellRenderer headerRenderer = table.getTableHeader()
                        .getDefaultRenderer();
              for (int i = 0; i < 5; i++) {
                   column = table.getColumnModel().getColumn(i);
                   comp = headerRenderer.getTableCellRendererComponent(null, column
                             .getHeaderValue(), false, false, 0, 0);
                   headerWidth = comp.getPreferredSize().width;
                   comp = table.getDefaultRenderer(model.getColumnClass(i))
                             .getTableCellRendererComponent(table, longValues, false,
                                       false, 0, i);
                   cellWidth = comp.getPreferredSize().width;
                   if (DEBUG) {
                        System.out.println("Initializing width of column " + i + ". "
                                  + "headerWidth = " + headerWidth + "; cellWidth = "
                                  + cellWidth);
                   // XXX: Before Swing 1.1 Beta 2, use setMinWidth instead.
                   column.setPreferredWidth(Math.max(headerWidth, cellWidth));
         public void setUpSportColumn(JTable table, TableColumn sportColumn) {
              // Set up the editor for the sport cells.
              JComboBox comboBox = new JComboBox();
              comboBox.addItem("Snowboarding");
              comboBox.addItem("Rowing");
              comboBox.addItem("Knitting");
              comboBox.addItem("Speed reading");
              comboBox.addItem("Pool");
              comboBox.addItem("None of the above");
              sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
              // Set up tool tips for the sport cells.
              DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
              renderer.setToolTipText("Click for combo box");
              sportColumn.setCellRenderer(renderer);
         class MyTableModel extends AbstractTableModel {
              private String[] columnNames = { "First Name", "Last Name", "Sport",
                        "# of Years", "Vegetarian" };
              private Object[][] data = {
                        { "Mary", "Campione", "Snowboarding", new Integer(5),
                                  new Boolean(false) },
                        { "Alison", "Huml", "Rowing", new Integer(3), new Boolean(true) },
                        { "Kathy", "Walrath", "Knitting", new Integer(2),
                                  new Boolean(false) },
                        { "Sharon", "Zakhour", "Speed reading", new Integer(20),
                                  new Boolean(true) },
                        { "Philip", "Milne", "Pool", new Integer(10),
                                  new Boolean(false) } };
              public final Object[] longValues = { "Sharon", "Campione",
                        "None of the above", new Integer(20), Boolean.TRUE };
              public int getColumnCount() {
                   return columnNames.length;
              public int getRowCount() {
                   return data.length;
              public String getColumnName(int col) {
                   return columnNames[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 false;
                   return true;
              * Don't need to implement this method unless your table's data can
              * change.
              public void setValueAt(Object value, int row, int col) {
                   if (DEBUG) {
                        System.out.println("Setting value at " + row + "," + col
                                  + " to " + value + " (an instance of "
                                  + value.getClass() + ")");
                   data[row][col] = value;
                   fireTableCellUpdated(row, col);
                   if (DEBUG) {
                        System.out.println("New value of data:");
                        printDebugData();
              private void printDebugData() {
                   int numRows = getRowCount();
                   int numCols = getColumnCount();
                   for (int i = 0; i < numRows; i++) {
                        System.out.print(" row " + i + ":");
                        for (int j = 0; j < numCols; j++) {
                             System.out.print(" " + data[i][j]);
                        System.out.println();
                   System.out.println("--------------------------");
         * Create the GUI and show it. For thread safety, this method should be
         * invoked from the event-dispatching thread.
         private static void createAndShowGUI() {
              // Create and set up the window.
              JFrame frame = new JFrame("TableRenderDemo");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              // Create and set up the content pane.
              TableRenderDemo newContentPane = new TableRenderDemo();
              newContentPane.setOpaque(true); // content panes must be opaque
              frame.setContentPane(newContentPane);
              // Display the window.
              frame.pack();
              frame.setVisible(true);
         public static void main(String[] args) {
              // Schedule a job for the event-dispatching thread:
              // creating and showing this application's GUI.
              javax.swing.SwingUtilities.invokeLater(new Runnable() {
                   public void run() {
                        createAndShowGUI();

  • Editable columns in table don't select the whole row

    I am new to the forums and posted this to the wrong one the first time and I am not sure how to move it so I am just reposting it here. Sorry.
    I have a table with 7 columns. 2 of them are non-editable and the rest are either radio buttons or check boxes. When I click on the 2 non-editable columns, the whole row gets highlighted. When I click on any of the editable columns, the button is selected, but the whole row is not highlighted. If I made the editable columns non-editable then the whole row gets highlighted when the column is clicked on. I want the button selected and the row highlighted when the editable columns are clicked on. Here is some relevant code:
    class PackageTable extends JPanel
        public PackageTable(String pathfile)
           fieldsok = true;
           errorfield = new JTextField(250);
           startfield = new JTextField(250);
           stopfield = new JTextField(250);
           tableModel = new MyTableModel();
           table = new JTable(tableModel)
             public Component prepareRenderer(TableCellRenderer renderer, int rowIndex, int vColIndex)
               Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
               if (vColIndex == 0)
                 c.setBackground(new Color(238,238,238));
               else
                 c.setBackground(new Color(255,255,255));
               boolean selected = isRowSelected(rowIndex);
               if (selected)
                 c.setBackground(Color.yellow);
               return c;
           table.setPreferredScrollableViewportSize(new Dimension(300, 1000));
           table.setRowSelectionAllowed(true);
           table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
           TableColumn column = null;
           for (int col = 0; col < 7; col++)
              column = table.getColumnModel().getColumn(col);
              if (col == 0)
                column.setPreferredWidth(30);
              else if (col == 1)
                column.setPreferredWidth(300);
              else if (col == 2)
                column.setPreferredWidth(10);
                column.setCellRenderer(new RadioButtonRenderer());
                column.setCellEditor(new RadioButtonEditor(new JCheckBox()));
              else if (col == 3)
                column.setPreferredWidth(10);
                column.setCellRenderer(new RadioButtonRenderer());
                column.setCellRenderer(new RadioButtonRenderer());
                column.setCellEditor(new RadioButtonEditor(new JCheckBox()));
              else if (col == 4)
                column.setPreferredWidth(10);
                column.setCellRenderer(new RadioButtonRenderer());
                column.setCellEditor(new RadioButtonEditor(new JCheckBox()));
              else if ((col == 5) || (col == 6))
                column.setPreferredWidth(10);
      class RadioButtonRenderer implements TableCellRenderer
        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());
          if (value == null)
            return null;
          return (Component) value;
      class RadioButtonEditor extends DefaultCellEditor implements ItemListener
        private JRadioButton button;
        public RadioButtonEditor(JCheckBox checkBox)
          super(checkBox);
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
          if (value == null)
            return null;
          button = (JRadioButton) value;
          button.addItemListener(this);
          return (Component) value;
        public void addCellEditorListener(CellEditorListener listener)
          listenerList.add(CellEditorListener.class, listener);
        public void removeCellEditorListener(CellEditorListener listener)
          listenerList.remove(CellEditorListener.class, listener);
        protected void fireEditingStopped()
          CellEditorListener listener;
          Object[] listeners = listenerList.getListenerList();
          for (int i = 0; i < listeners.length; i++)
            if (listeners[i] == CellEditorListener.class)
              listener = (CellEditorListener)listeners[i+1];
              listener.editingStopped(changeEvent);
        protected void fireEditingCanceled()
          CellEditorListener listener;
          Object[] listeners = listenerList.getListenerList();
          for (int i = 0; i < listeners.length; i++)
            if (listeners[i] == CellEditorListener.class)
              listener = (CellEditorListener)listeners[i+1];
              listener.editingCanceled(changeEvent);
        public void cancelCellEditing()
          fireEditingCanceled();
        public boolean stopCellEditing()
          fireEditingStopped();
          return true;
        public Object getCellEditorValue()
          return button;
        public boolean isCellEditable(EventObject event)
          return true;
        public boolean shouldSelectCell(EventObject event)
          return true;
        public void itemStateChanged(ItemEvent e)
          super.fireEditingStopped();
      class MyTableModel extends AbstractTableModel
        String[] columnNames = {"","Configuration Files","Sorts","Plots","Both","Print","Alerts"};
        public MyTableModel() { }
        public int getColumnCount()
          return columnNames.length;
        public int getRowCount()
          return totaldata.size();
        public String getColumnName(int col)
          return columnNames[col];
        public Object getValueAt(int row,int col)
          return(((Vector)totaldata.get(row)).get(col));
        public Class getColumnClass(int c)
          return getValueAt(0, c).getClass();
        public boolean isCellEditable(int row, int col)
          if ((col == 0) || (col == 1))
            return false;
          else
            return true;
        public void setValueAt(Object value, int row, int col)
          Vector v1 = new Vector();
          v1 = (Vector)totaldata.get(row);
          v1.set(col,value);
          if (col == 1)
             if (((String)value).indexOf("/") == -1)
               JOptionPane.showMessageDialog(pdsframe, "The CONFIG file that was entered on line " + (row+1) + " is not valid.");
               v1.set(col,(Object)"");
               return;
          fireTableCellUpdated(row, col);
          fireTableChanged(new TableModelEvent(this));
        public void addNewRow(Vector newRow)
          totaldata.add(newRow);
          fireTableRowsInserted(totaldata.size()-1, totaldata.size()-1);
        public void deleteRow(int Row)
          totaldata.remove(Row);
          fireTableRowsDeleted(totaldata.size()-1, totaldata.size()-1);
      }I have searched forever to try to find how to do this and I can't seem to get it right. If all the radiobuttons in one column are selected and I click on one of those rows in one of those columns, then the whole row is highlighted.
    Can anyone help me out?
    Thanks.
    esk3 {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

    Sorry. I didn't know that I had to provide something that could be executed. I am going to try to put enough in so that it can. This is part of a larger program and this frame is called from another frame. Here it is. I hope it works.
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Dimension.*;
    import java.util.*;
    import java.text.*;
    // Java extension packages
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.border.*;
    import javax.swing.table.*;
    import javax.swing.JTable.*;
    import javax.swing.JScrollPane.*;
      private JFrame     pdsframe;
      private String printchecked;
      private String alertschecked;
      private JTextField cb;
      private JTextField pf;
      private JScrollPane scrollpane;
      private JTable table;
      private MyTableModel tableModel;
      private Vector totaldata;
      private JLabel startlabel;
      private JLabel stoplabel;
      private JTextField startfield;
      private JTextField stopfield;
      private JTextField errorfield;
      private boolean fieldsok;
      class MyTableModel extends DefaultTableModel
        String[] columnNames = {"","Configuration Files","Sorts","Plots","Both","Print","Alerts"};
        public MyTableModel() { }
        public int getColumnCount()
          return columnNames.length;
        public int getRowCount()
          return totaldata.size();
        public String getColumnName(int col)
          return columnNames[col];
        public Object getValueAt(int row,int col)
          return(((Vector)totaldata.get(row)).get(col));
        public Class getColumnClass(int c)
          return getValueAt(0, c).getClass();
        public boolean isCellEditable(int row, int col)
          if ((col == 0) || (col == 1))
            return false;
          else
            return true;
        public void setValueAt(Object value, int row, int col)
          Vector v1 = new Vector();
          v1 = (Vector)totaldata.get(row);
          v1.set(col,value);
          if (col == 1)
             if (((String)value).indexOf("/") == -1)
               JOptionPane.showMessageDialog(pdsframe, "The CONFIG file that was entered on line " + (row+1) + " is not valid.");
               v1.set(col,(Object)"");
               return;
          fireTableCellUpdated(row, col);
          fireTableChanged(new TableModelEvent(this));
        public void addNewRow(Vector newRow)
          totaldata.add(newRow);
          fireTableRowsInserted(totaldata.size()-1, totaldata.size()-1);
        public void deleteRow(int Row)
          totaldata.remove(Row);
          fireTableRowsDeleted(totaldata.size()-1, totaldata.size()-1);
      class RadioButtonRenderer implements TableCellRenderer
        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());
          if (value == null)
            return null;
          return (Component) value;
      class RadioButtonEditor extends DefaultCellEditor implements ItemListener
        private JRadioButton button;
        public RadioButtonEditor(JCheckBox checkBox)
          super(checkBox);
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
          if (value == null)
            return null;
          button = (JRadioButton) value;
          button.addItemListener(this);
          return (Component) value;
        public void addCellEditorListener(CellEditorListener listener)
          listenerList.add(CellEditorListener.class, listener);
        public void removeCellEditorListener(CellEditorListener listener)
          listenerList.remove(CellEditorListener.class, listener);
        protected void fireEditingStopped()
          CellEditorListener listener;
          Object[] listeners = listenerList.getListenerList();
          for (int i = 0; i < listeners.length; i++)
            if (listeners[i] == CellEditorListener.class)
              listener = (CellEditorListener)listeners[i+1];
              listener.editingStopped(changeEvent);
        protected void fireEditingCanceled()
          CellEditorListener listener;
          Object[] listeners = listenerList.getListenerList();
          for (int i = 0; i < listeners.length; i++)
            if (listeners[i] == CellEditorListener.class)
              listener = (CellEditorListener)listeners[i+1];
              listener.editingCanceled(changeEvent);
          }      fireEditingStopped();
          return true;
        public Object getCellEditorValue()
          return button;
        public boolean isCellEditable(EventObject event)
          return true;
        public boolean shouldSelectCell(EventObject event)
          return true;
        public void itemStateChanged(ItemEvent e)
          super.fireEditingStopped();
      class PackageTable extends JPanel
        public PackageTable(String pathfile)
           fieldsok = true;
           errorfield = new JTextField(250);
           startfield = new JTextField(250);
           stopfield = new JTextField(250);
           tableModel = new MyTableModel();
           table = new JTable(tableModel)
             public Component prepareRenderer(TableCellRenderer renderer, int rowIndex, int vColIndex)
               Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
               if (vColIndex == 0)
                 c.setBackground(new Color(238,238,238));
               else
                 c.setBackground(new Color(255,255,255));
               boolean selected = isRowSelected(rowIndex);
               if (selected)
                 c.setBackground(Color.yellow);
               return c;
           table.setPreferredScrollableViewportSize(new Dimension(300, 1000));
           table.setRowSelectionAllowed(true);
           table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
           TableColumn column = null;
           for (int col = 0; col < 7; col++)
              column = table.getColumnModel().getColumn(col);
              if (col == 0)
                column.setPreferredWidth(30);
              else if (col == 1)
                column.setPreferredWidth(300);
              else if (col == 2)
                column.setPreferredWidth(10);
                column.setCellRenderer(new RadioButtonRenderer());
                column.setCellEditor(new RadioButtonEditor(new JCheckBox()));
              else if (col == 3)
                column.setPreferredWidth(10);
                column.setCellRenderer(new RadioButtonRenderer());
                column.setCellEditor(new RadioButtonEditor(new JCheckBox()));
              else if (col == 4)
                column.setPreferredWidth(10);
                column.setCellRenderer(new RadioButtonRenderer());
                column.setCellEditor(new RadioButtonEditor(new JCheckBox()));
              else if ((col == 5) || (col == 6))
                column.setPreferredWidth(10);
          JScrollPane scrollPane = new JScrollPane(table);
          scrollPane.setBounds(20,180,1000,300);
          JLabel pflabel = new JLabel("Package File: ");
          pf = new JTextField(pathfile);
          cb = new JTextField(250);
          startlabel = new JLabel("START_TIME = ");
          stoplabel = new JLabel("STOP_TIME = ");
          JLabel typelabel = new JLabel("TYPE OF FORMAT TO OUTPUT");
          JButton savebutton = new JButton("Save and Run");
          JButton jbtAddRow = new JButton("Add New Row");
          JButton jbtUpdateRow = new JButton("Update Row");
          JButton jbtDeleteRow = new JButton("Delete Row");
          JButton cancelbutton = new JButton("Cancel");
          cb.setEditable(false);
          cb.setBounds(130,230,100,30);
          cb.setEnabled(false);
          cb.setVisible(false);
          pflabel.setBounds(20,20,100,30);
          pf.setEditable(false);
          pf.setBounds(130,20,300,30);
          startlabel.setBounds(20,90,100,30);
          startfield.setEditable(false);
          startfield.setBackground(new Color(255,255,255));
          startfield.setBounds(130,90,300,30);
          stoplabel.setBounds(20,120,100,30);
          stopfield.setEditable(false);
          stopfield.setBackground(new Color(255,255,255));
          stopfield.setBounds(130,120,300,30);
          typelabel.setBounds(525,150,400,30);
          savebutton.setBounds(130,500,130,30);
          jbtAddRow.setBounds(280,500,130,30);
          jbtUpdateRow.setBounds(430,500,130,30);
          jbtDeleteRow.setBounds(580,500,130,30);
          cancelbutton.setBounds(730,500,130,30);
          add(scrollPane);
          add(pflabel);
          add(pf);
          add(startlabel);
          add(stoplabel);
          add(startfield);
          add(stopfield);
          add(typelabel);
          add(jbtAddRow);
          add(jbtUpdateRow);
          add(jbtDeleteRow);
          add(savebutton);
          add(cancelbutton);
      public void createAndShowGUI() {
        //Create and set up the window.
        pdsframe = new JFrame("PDS Batch Parameters");
        pdsframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        totaldata = new Vector();
        //Create and set up the content pane.
        PackageTable newContentPane = new PackageTable(pathfile);
        newContentPane.setOpaque(true); //content panes must be opaque
        pdsframe.setContentPane(newContentPane);
        //Display the window.
        pdsframe.setLayout(null);
        pdsframe.setSize(1100,700);
        pdsframe.setLocationRelativeTo(null);
        pdsframe.setFocusableWindowState(true);
        pdsframe.setVisible(true);
        pdsframe.show();
        pdsframe.setAlwaysOnTop(true);
        pdsframe.requestFocus();
    public void main()
        Toolkit.getDefaultToolkit().beep();
        createAndShowGUI();
    }Does this help? I hope it works. These are the relevant parts. Thanks.
    esk3

  • Columns in JTable, Help!

    Hello There,
    I have two question about the JTable in Java:
    1. How can i set a column width? I want to have one column widder than the other and not editable at runtime.
    2. How can i have multiple lines (block of data/small paragraph) in one cell? because whenver i write a 3 new lines of data in a cell the JTable converts it cont. line.
    Please Help!!

    Here there is a JTable where 3 colums are being resized
    TableColumn column1 = null;
                   for (int i = 0; i < 3; i++) {
                       column1 = table1.getColumnModel().getColumn(i);
                       if (i == 0) {
                           column1.setPreferredWidth(20);
                       } else {
                            if (i == 1){
                                 column1.setPreferredWidth(160);
                            else
                                 column1.setPreferredWidth(80);
                   }

  • Keyboard Access For a Column in JTable with default editor as JComboBox

    I want to get Key board access for a column in JTable.
    The user should be able to select from a drop down list for the column with default
    editor set as JComboBox.
    Presently,it works fine with mouse,also I am able to focus it with Keyboard using
    ALT+Up keys,but how to make drop down list appear.
    Plz help,it's urgent.
    Thanks in Advance

    Hi,
    In addition to setting DO_SUM = 'X' you need to specify function in H_FTYPE field. It should be set to 'AVG' in your case.
    ls_fielcat-do_sum = 'X'.
    ls_fieldcat-h_ftype = 'AVG.

  • Group feature at ALV grid with editable columns

    Am I right ?
    Group function by using a sorted layout is disabled while having one (or more) editable columns, isn't it ?
    ... or is there a possibility to use gouped rows ?
    Greetings
    Markus

    Also...
    *& Report ZDEMO_ALVGRID_EDIT *
    *& Example of a simple ALV Grid Report *
    *& The basic ALV grid, Enhanced to display specific fields as *
    *& editable depending on field value *
    REPORT ZDEMO_ALVGRID_EDIT .
    TABLES: ekko.
    TYPE-POOLS: slis. "ALV Declarations
    *Data Declaration
    TYPES: BEGIN OF t_ekko,
    ebeln TYPE ekpo-ebeln,
    ebelp TYPE ekpo-ebelp,
    statu TYPE ekpo-statu,
    aedat TYPE ekpo-aedat,
    matnr TYPE ekpo-matnr,
    menge TYPE ekpo-menge,
    meins TYPE ekpo-meins,
    netpr TYPE ekpo-netpr,
    peinh TYPE ekpo-peinh,
    field_style TYPE lvc_t_styl, "FOR DISABLE
    END OF t_ekko.
    DATA: it_ekko TYPE STANDARD TABLE OF t_ekko INITIAL SIZE 0,
    wa_ekko TYPE t_ekko.
    *ALV data declarations
    DATA: fieldcatalog TYPE slis_t_fieldcat_alv WITH HEADER LINE.
    DATA: it_fieldcat TYPE lvc_t_fcat, "slis_t_fieldcat_alv WITH HEADER LINE,
    wa_fieldcat TYPE lvc_s_fcat,
    gd_tab_group TYPE slis_t_sp_group_alv,
    gd_layout TYPE lvc_s_layo, "slis_layout_alv,
    gd_repid LIKE sy-repid.
    *Start-of-selection.
    START-OF-SELECTION.
    PERFORM data_retrieval.
    PERFORM set_specific_field_attributes.
    PERFORM build_fieldcatalog.
    PERFORM build_layout.
    PERFORM display_alv_report.
    *& Form BUILD_FIELDCATALOG
    Build Fieldcatalog for ALV Report
    FORM build_fieldcatalog.
    wa_fieldcat-fieldname = 'EBELN'.
    wa_fieldcat-scrtext_m = 'Purchase Order'.
    wa_fieldcat-col_pos = 0.
    wa_fieldcat-outputlen = 10.
    wa_fieldcat-emphasize = 'X'.
    wa_fieldcat-key = 'X'.
    APPEND wa_fieldcat TO it_fieldcat.
    CLEAR wa_fieldcat.
    wa_fieldcat-fieldname = 'EBELP'.
    wa_fieldcat-scrtext_m = 'PO Item'.
    wa_fieldcat-col_pos = 1.
    APPEND wa_fieldcat TO it_fieldcat.
    CLEAR wa_fieldcat.
    wa_fieldcat-fieldname = 'STATU'.
    wa_fieldcat-scrtext_m = 'Status'.
    wa_fieldcat-col_pos = 2.
    APPEND wa_fieldcat TO it_fieldcat.
    CLEAR wa_fieldcat.
    wa_fieldcat-fieldname = 'AEDAT'.
    wa_fieldcat-scrtext_m = 'Item change date'.
    wa_fieldcat-col_pos = 3.
    APPEND wa_fieldcat TO it_fieldcat.
    CLEAR wa_fieldcat.
    wa_fieldcat-fieldname = 'MATNR'.
    wa_fieldcat-scrtext_m = 'Material Number'.
    wa_fieldcat-col_pos = 4.
    APPEND wa_fieldcat TO it_fieldcat.
    CLEAR wa_fieldcat.
    wa_fieldcat-fieldname = 'MENGE'.
    wa_fieldcat-scrtext_m = 'PO quantity'.
    wa_fieldcat-col_pos = 5.
    APPEND wa_fieldcat TO it_fieldcat.
    CLEAR wa_fieldcat.
    wa_fieldcat-fieldname = 'MEINS'.
    wa_fieldcat-scrtext_m = 'Order Unit'.
    wa_fieldcat-col_pos = 6.
    APPEND wa_fieldcat TO it_fieldcat.
    CLEAR wa_fieldcat.
    wa_fieldcat-fieldname = 'NETPR'.
    wa_fieldcat-scrtext_m = 'Net Price'.
    wa_fieldcat-edit = 'X'. "sets whole column to be editable
    wa_fieldcat-col_pos = 7.
    wa_fieldcat-outputlen = 15.
    wa_fieldcat-datatype = 'CURR'.
    APPEND wa_fieldcat TO it_fieldcat.
    CLEAR wa_fieldcat.
    wa_fieldcat-fieldname = 'PEINH'.
    wa_fieldcat-scrtext_m = 'Price Unit'.
    wa_fieldcat-col_pos = 8.
    APPEND wa_fieldcat TO it_fieldcat.
    CLEAR wa_fieldcat.
    ENDFORM. " BUILD_FIELDCATALOG
    *& Form BUILD_LAYOUT
    Build layout for ALV grid report
    FORM build_layout.
    Set layout field for field attributes(i.e. input/output)
    gd_layout-stylefname = 'FIELD_STYLE'.
    gd_layout-zebra = 'X'.
    ENDFORM. " BUILD_LAYOUT
    *& Form DISPLAY_ALV_REPORT
    Display report using ALV grid
    FORM display_alv_report.
    gd_repid = sy-repid.
    call function 'REUSE_ALV_GRID_DISPLAY'
    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
    i_callback_program = gd_repid
    i_callback_user_command = 'USER_COMMAND'
    is_layout_lvc = gd_layout
    it_fieldcat_lvc = it_fieldcat
    i_save = 'X'
    TABLES
    t_outtab = it_ekko
    EXCEPTIONS
    program_error = 1
    OTHERS = 2.
    IF sy-subrc <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
    WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    ENDFORM. " DISPLAY_ALV_REPORT
    *& Form DATA_RETRIEVAL
    Retrieve data form EKPO table and populate itab it_ekko
    FORM data_retrieval.
    SELECT ebeln ebelp statu aedat matnr menge meins netpr peinh
    UP TO 10 ROWS
    FROM ekpo
    INTO CORRESPONDING FIELDS OF TABLE it_ekko.
    ENDFORM. " DATA_RETRIEVAL
    *& Form set_specific_field_attributes
    populate FIELD_STYLE table with specific field attributes
    form set_specific_field_attributes .
    DATA ls_stylerow TYPE lvc_s_styl .
    DATA lt_styletab TYPE lvc_t_styl .
    Populate style variable (FIELD_STYLE) with style properties
    The NETPR field/column has been set to editable in the fieldcatalog...
    The following code sets it to be disabled(display only) if 'NETPR'
    is gt than 10.
    LOOP AT it_ekko INTO wa_ekko.
    IF wa_ekko-netpr GT 10.
    ls_stylerow-fieldname = 'NETPR' .
    ls_stylerow-style = cl_gui_alv_grid=>mc_style_disabled.
    "set field to disabled
    APPEND ls_stylerow TO wa_ekko-field_style.
    MODIFY it_ekko FROM wa_ekko.
    ENDIF.
    ENDLOOP.
    endform. " set_specific_field_attributes
    REWARD POINTS IF usefful !!

  • Vertical headers for columns in JTable

    Hi,
    I'm actually working on JTables and i'd like to set the headers of some columns to appear vertically instead of by default horizontal.
    I've found some tuts or examples to set the header on multiple lines but no one to set the header vertical...
    I want something like that :
    before :
    column1 | column2
    row1 |
    row2 |
    and after setting header columns vertical ( with column1 and column2 the column headers of my JTable )
    c | c
    o | o
    l | l
    u | u
    m | m
    n | n
    1 | 2
    ( and orientation of row headers not changed )
    Thanx for your reply
    Scottish

    Hi,
    Create you own colunm header renderer... it is actualy a cellRenderer. That cellRenderer should extends JLabel or JButton depending on what you want and you must put a paint method in it that draws the vertical text.
    JRG

  • How to use Special value set with Column clause to display addtional column

    hi
    can anyone tell me
    How to use Special value set with Column clause to display addtional column
    how can i use FND POPID and with COLUMN clause ?

    Dear Yaseen,
                To get the value in formula cumulative,after you have created the formula for that particular key figure,go to edit and click on tab "calculations".
    In that select overall result as 'count all values not equal to zero' and single result as 'count all values not equal to zero' .
    And mark the cumulative check box underneath.
    I hope you get the desired result and please do me favour .
    Sumit goomber

  • Hide a column in JTable

    Dear all,
    I want to hide a column in JTable object, I already tried resize it to 0 but the effect is not good, I also cannot remove the column, since I need this column for other process.
    So, any other good idea besides resizes and remove?
    Thank you very much!

    I have tried that too, but the column isn't hide, the size is not actually come to 0,...That's because JTable has a default minimum width of 15.... if you want to hide the column, you must set the minwidth as well. For example:
    myTable.getColumnModel().getColumn(column_to_hide).setMinWidth(0);
    myTable.getColumnModel().getColumn(column_to_hide).setPreferredWidth(0);;o)
    V.V.

Maybe you are looking for

  • HT1267 The Apple ID in my new IPhone is not the same as my ID . how can i sort this

    Hi, The Users ID on My New IPhone Is not the same as my apple ID How can I sort this ...

  • Hiding a row based on the the drill down via table interfaces

    I have a requirement to hide a particular row (key field) of a table if a characterisitic (0PLANT) is included in the table. I have managed to hide the key field via the table interface but need to know how I would see if the characteristic is drille

  • BEx Web analyzer Error

    hello, This is the first time we are using the BEx Web Analyser thru portal we are recieving following error message. Kindly comment on the same to get it resolved. - Amit <b>Root Cause</b> The initial exception that caused the request to fail was: M

  • Migration from XI 3.0 to PI 7.1

    Hi PI experts, We are planning to migrate our interfaces from 3.0 to 7.1. Many interfaces have EARs. Has anyone used 'Migration controller' (which picks the EARs, modifies them & deploys them in the target location on SAP PI 7.1.) It would be great i

  • Drawing in CS Applications

    Colleagues, My most recent post on the CS SDK blog is about drawing--specifically, it provides routines that make drawing paths work the same way in Illustrator, InDesign, and Photoshop. (Basically, I added routines that make AI and PS work the way t