Render jButton in a cell

Hi i have been able to render a jbutton button in a cell but would like now to only render it at the end of the cell ie the button will take up only half the cell.
Is this possible??
Thanks
Rudy

This really isn't that hard... just keep saying in your head... whatever I return in my TableCellRenderer will show up in my table...whatever I return in my TableCellRenderer will show up in my table...whatever I return in my TableCellRenderer will show up in my table...whatever I return in my TableCellRenderer will show up in my table.... :)
If you want text to show up... then make sure your returned component has the text.

Similar Messages

  • JButton in table cell in Find Mode

    Hello!
    I have JButton in JTable cell for calling LOV.
    When form goes to Find Mode all JButtons become disabled.
    Accordingly, I want that buttons active. Any help will be appreciated %)

    If the button is bound with an LOV Button binding, then I believe the framework checks the queryable property of the "target" attribute. Make sure that attribute has the "queryable" property checked in the View/Entity.
    Hope this helps.
    Erik

  • Adding JButton into JTable cells

    Hi there!!
    I want to add a JButton into JTable cells.In fact I have got two classes.Class2 has been extended from the AbstractTableModel class and Class1 which is using Class2's model,,,here's the code,,
    Class1
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    public class Class1 extends javax.swing.JFrame {
       //////GUI specifications
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new TestTableButton().setVisible(true);
            Class2 model=new Class2();
            jTable1=new JTable(model);
            jScrollPane1.setViewportView(jTable1);
        // Variables declaration - do not modify                    
        private javax.swing.JScrollPane jScrollPane1;
        // End of variables declaration                  
        private javax.swing.JTable jTable1;
    }Class2
    import javax.swing.table.*;
    public class Class2 extends AbstractTableModel{
        private String[] columnNames = {"A","B","C"};
        private Object[][] data = {
        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];
        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;
         * Don't need to implement this method unless your table's
         * data can change.
        public void setValueAt(Object value, int row, int col) {
            data[row][col] = value;
            fireTableCellUpdated(row, col);
    }what modifications shud I be making to the Class2 calss to add buttons to it?
    Can anybody help plz,,,,,??
    Thanks in advance..

    Hi rebol!
    I did search out a lot for this but I found my problem was a bit different,,in fact I want to use another class's model into a class being extended by JFrame,,so was a bit confused,,,hope you can give me some ideas about how to handle that scenario,,I know this topic has been discussed before here many a times and also have visited this link,,
    http://forum.java.sun.com/thread.jspa?threadID=465286&messageID=2147913
    but am not able to map it to my need,,,hope you can help me a bit...
    Thanks .....

  • Multiple JButtons inside JTable cell - Dispatch mouse clicks

    Hi.
    I know this subject has already some discussions on the forum, but I can't seem to find anything that solves my problem.
    In my application, every JTable cell is a JPanel, that using a GridLayout, places vertically several JPanel's witch using an Overlay layout contains a JLabel and a JButton.
    As you can see, its a fairly complex cell...
    Unfortunately, because I use several JButtons in several locations inside a JTable cell, sometimes I can't get the mouse clicks to make through.
    This is my Table custom renderer:
    public class TimeTableRenderer implements TableCellRenderer {
         Border unselectedBorder = null;
         Border selectedBorder = null;
         public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                   boolean hasFocus, int row, int column) {
              if (value instanceof BlocoGrid) {
                   if (isSelected) {
                        if (selectedBorder == null)
                             selectedBorder = BorderFactory.createMatteBorder(2,2,2,2, table.getSelectionBackground());
                        ((BlocoGrid) value).setBorder(selectedBorder);
                   } else {
                        if (unselectedBorder == null)
                             unselectedBorder = BorderFactory.createMatteBorder(2,2,2,2, table.getBackground());
                        ((BlocoGrid) value).setBorder(unselectedBorder);
              return (Component) value;
    }and this is my custom editor (so clicks can get passed on):
    public class TimeTableEditor extends AbstractCellEditor implements TableCellEditor {
         private TimeTableRenderer render = null;
         public TimeTableEditor() {
              render = new TimeTableRenderer();
        public Component getTableCellEditorComponent(JTable table, Object value,
                boolean isSelected, int row, int column) {
             if (value instanceof BlocoGrid) {
                  if (((BlocoGrid) value).barras.size() > 0) {
                       return render.getTableCellRendererComponent(table, value, isSelected, true, row, column);
             return null;
        public Object getCellEditorValue() {
            return null;
    }As you can see, both the renderer and editor return the same component that cames from the JTable model (all table values (components) only get instantiated once, so the same component is passed on to the renderer and editor).
    Is this the most correct way to get clicks to the cell component?
    Please check the screenshot below to see how the JButtons get placed inside the cell:
    http://img141.imageshack.us/my.php?image=calendarxo9.jpg
    If you need more info, please say so.
    Thanks.

    My mistake... It worked fine. The cell span code was malfunctioning. Thanks anyway.

  • JButton inside the cell of JTable

    JButton buttonInCellOfTable = new JButton();
    buttonInCellOfTable.setBackground(Color.RED);
    buttonInCellOfTable.setText("MORE");
    buttonInCellOfTable.setPreferredSize(new Dimension(10, 22));
    buttonInCellOfTable.setMinimumSize(new Dimension(10, 22));
    buttonInCellOfTable.setMaximumSize(new Dimension(10, 22));
    myJTable.addRow("No. 1", buttonInCellOfTable);
    But Color and size isn't shown unlike those are set.
    What can I do?

    * @(#)ButtonTableCellRenderer.java     Tiger     2005 April 29
    package com.yahoo.ron.table;
    import java.awt.Component;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax,swing.JTable;
    import javax.swing.table.TableCellRenderer;
    * @author     Ronillo Ang - [email protected]
    final public class ButtonTableCellRenderer extends JButton implements TableCellRenderer{
         public ButtonTableCellEditor(ActionListener actionListener){
              super("Editor");
              if(actionListener == null)
                   throw new NullPointerException();
              addActionListener(actionListener);
         public Component getTableCellRenderComponent(JTable table, Object value, boolean selected, boolean hasFocus, int row, int col){
              if(value instanceof String){
                   String text = String.valueOf(value);
                   setText(text);
              else
                   setText("Unknown value");
              return this;
    * @(#)TestingButtonTableCellRenderer.java      Tiger     2005 April 29
    package com.yahoo.ron.test;
    import com.yahoo.ron.table.ButtonTableCellRenderer;
    import com.yahoo.ron.table.UneditableTableModel;
    import com.yahoo.ron.ThreadMonitor;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.TableColumn;
    import javax.swing.table.TableColumnModel;
    * @author     Ronillo Ang - [email protected]
    * A class to test com.yahoo.ron.table.ButtonTableCellRenderer
    public class TestingButtonTableCellRenderer implements ActionListener{
         public TestingButtonTableCellRenderer(){
              JFrame frame = new JFrame("Testing ButtonTableCellRenderer");
              frame.getContentPane().setLayout(new GridLayout(1, 1));
              JTable table = new JTable(new UneditableTableModel(new ThreadMonitor()));
              TableColumnModel tableColumnModel = table.getColumnModel();
              // get the last column.
              TableColumn tableColumn = tableColumnModel.getColumn(tableColumnModel.getColumnCount() - 1);
              // apply the cell renderer.
              tableColumn.setCellRenderer(new ButtonTableCellRenderer(this));
              frame.getContentPane().add(new JScrollPane(table));
              frame.pack();
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setVisible(true);
         public void actionPerformed(ActionEvent ae){
              // do nothing for now.
         static public void main(String[] args){
              new TestingButtonTableCellRenderer();
    // Okie!! I dont include my source of ThreadMonitor.java and UneditableTableModel.
    // If problem occur, make some modification. Okie!
    // Thanks! God bless you all!

  • 3 jbuttons in a cell in a jtable

    hi i have a problem entering 3 buttons into 1 cell i a jtable
    can someone give me a good example how i put 3 buttons in the same cell
    i have a button render and its working good with 1 button but i just cant find a way to enter 3 buttons in the same cell
    tanks in advance

    Doesn't sound like a good design, but if you must, have the renderer return a JPanel with the 3 buttons, probably in a GridLayout or BoxLayout.
    I suppose you already know that the renderer only draws the components to the table cell, and your rendered buttons will not respond to mouse clicks or keyboard actions.
    db

  • How to put JTextfield and JButton in a cell of JTable

    I'm trying to put two Components into one cell of a JTable. This is no
    problem (in the TableCellRenderer) unless it comes to editing in these
    cells. I have written my own CellEditor for the table.but there is one
    minor problem: Neither the JTextfield nor the JButton has the focus so I
    can't make any input! Does anybody know what's wrong, please help me for this issue ,plese urgent

    Here you go
    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.AbstractCellEditor;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JTextField;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableCellEditor;
    public class TableEdit extends JFrame
         JTable table = new JTable();
         private String[] columnNames = {"non-edit1", "edit", "non-edit2"};
         public TableEdit()
              super("Table Edit");
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              createGUI();
              setLocationRelativeTo(null);
              setSize(400, 300);
              setVisible(true);
         private void createGUI()
              table.setModel(new DefaultTableModel(3,3)
                   public int getColumnCount()
                        return 3;
                   public boolean isCellEditable(int row, int column)
                        if(column == 1)
                             return true;
                        return false;
                   public String getColumnName(int column)
                        return columnNames[column];
                   public int getRowCount()
                        return 3;
                   public Class getColumnClass(int column)
                        return String.class;
                   public Object getValueAt(int row, int column)
                        return row + "" + column;
              table.setRowHeight(20);
              table.setDefaultEditor(String.class, new MyTableEditor());
              JScrollPane sp = new JScrollPane(table);
              add(sp);
         public class MyTableEditor extends AbstractCellEditor
         implements TableCellEditor
              JPanel jp = new JPanel(new BorderLayout(5,5));
              JTextField tf = new JTextField();
              JButton btn = new JButton("...");
              public MyTableEditor()
                   jp.add(tf);
                   jp.add(btn, BorderLayout.EAST);
                   btn.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent e)
                             JOptionPane.showMessageDialog(null, "Clicked Lookup");
              public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
                   tf.setText(String.valueOf(value));
                   return jp;
              public Object getCellEditorValue()
                   return tf.getText();
         public static void main(String[] parms)
              new TableEdit();
    }

  • Urgent!!! JText and JButton in same cell of JTable

    Does anyone have an example of a JTable with a cell that allows for text input as well as a JButton for invoking the JFileSystemChooser. I believe this involves adding 2 JComponents to a cell within the JTable.
    An example of this can be seen if you have Visual Cafe. Under Tools, Environment Options, Virtual Machines. If you click in the cell for the VM Executable then click again (Without double-clicking), a JButton appears that when clicked will invoke the FileSystemChooser. Users have the option of either entering text or invoking the chooser.
    thanks

    You can create a TableCellEditor your self that inherits from a JPanel that contains a textfield and a button.
    class MyCellEditor extends JPanel implements TableCellEditor
    JTextField field = new JTextField();
    JButton button = new JButton("...");
    public MyCellEditor()
    // Add a textfield and a jbutton.
    this.setLayout(new BorderLayout());
    this.add(BorderLayout.CENTER, field);
    this.add(BorderLayout.EAST, button);
    // Add listeners
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
    // Initialize you editor's value.
    field.setText(value.toString());
    // What else?
    return this;
    public Object getCellEditorValue()
    return field.getText();
    // Implements other methods in the interface...
    Hope this helps.

  • Placing JButtons in JTable cells

    I would like to place JButtons in one column of a JTable.
    Can anyone post or point me toward some example code that illustrates this?
    Many thanks in advance.

    did you try this with the cell renderer
    This should work. Try extending the DefaultCellRenderer and set them as a jbutton
    hope this will work

  • Control the size for a JButton in a cell of JTable

    Hi,
    I am trying to add a column buttons on cells of a JTable. I followed the example from http://www.esus.com/docs/GetQuestionPage.jsp?uid=1285 and made it work (thanks).
    But, the buttons always resize to fit the column. Is there a way I can set button size, so the button will be smaller?
    Any information would be appreciated. Thanks in advance.

    Use a panel as a renderer and add the button to the panel, and manually set the preferred size of the button yourself. Then when the panel is added to the cell it will be resized, but the button will remain at its preferred size.

  • I want code on placing textbix and Jbutton in a cell of JTable.Please

    Dear All,
    Iam doing project on swing and XMl iam using swing for fron end design. Here i have to use table in one cell i have to put textbox and button. please can any body send me the code. Pleae iam waiting for your valuable code.
    regards,
    surya

    instead of asking someone else to do the code and send it to you, why don't you try the code and submit it ot the forum for help?

  • I want code on placing textbox and Jbutton in a cell of JTable.Please

    Dear All,
    Iam doing project on swing and XMl iam using swing for fron end design. Here i have to use table in one cell i have to put textbox and button. please can any body send me the code. Pleae iam waiting for your valuable code.
    regards,
    surya

    Hi I also want the same thing,
    My requirement is like this, The content of the table will be kept on changing using a thread connecting to the servlet. SO the record number will be different
    Can any one help me in this matter

  • JLabel as cell of JTable

    Hi All,
    I am using JLabel as a cell of a JTable by overriding getTableCellRendererComponent method of the renderer.But I am not able to select/focus on any of the cell by clicking at cells.However If I use any other componenet like JTextArea, I can select any cell I want.
    I have to use JLabel because i want to put multiple color and fonts in a single cell.I dont't want to use JTextPane because it takes more memory as well as It has wraping problem
    Any help would be greatly appreciated

    It's not a good idea to create a NEW Component each and every time getXXXCellRenderer() is called. Object creation is VERY expensive!!!
    Remember, this Component is merely renderered (e.g. painted, or "rubber-stamped") into the Graphics Context of the JTable, after appropriate sizing to fit the cell, of course. So the SAME component can be used to render each and every cell, each and every time each cell needs to be rendered. (Unless different Component classes are used to render different cells, but still, one of each of these Components should be created in the Constructor, not each time a cell is rendered.)
    I have reworked your code to make it much more efficient, and to make it more readable:
    public class MyTableCellRenderer
        implements TableCellRenderer
        JLabel label = new JLabel();
        public MyTableCellRenderer()
            super();
            this.label.setOpaque(true);
        public Component getTableCellRendererComponent( JTable  table,
                                                        Object  value,
                                                        boolean isSelected,
                                                        boolean hasFocus,
                                                        int     row,
                                                        int     column )
            String st=null;
            st="<HTML><B><FONT Color=Red>" + value.toString() 
               + "</FONT></B></HTML>";
            this.label.setText(st);
            if (isSelected) {
                this.label.setBackground( table.getSelectionBackground());
            } else {
                this.label.setBackground(Color.yellow);
            return this.label;
    }You may find that using HTML as the text of JLabel is much too expensive, especially with a large table. I tried it once, with a table of only moderate size, and gave up. It's much faster to set the font, foreground color, etc., on the JLabel itself instead.
    I was trying to use HTML simply to get HTML Character Entities (like &amp;) to display correctly, and it just didn't work. I wrote my own class to parse strings for HTML Character Entities, replacing them with appropriate Unicode characters, and it worked much better.
    ---Mark

  • Using JButtons in a JTable

    I'm trying to find some information about how to use a JButton within a cell of a JTable. I'm able to insert the button using a custom cell renderer. Basically I create my own renderer which is triggered for JButton.class. That works well.
    Of course I do a addActionListener() on the JButton so something will happen when it is pressed.
    But the problem is that events aren't getting through to the button at all. The table is getting all the mouse events and is not passing them on.
    I'm sure I'm not the first to run into this. Any suggestions on how to get the events to the button, or otherwise deal with this problem?
    Thanks

    Those example are from 1998. I don't think they work anymore.
    Basically, if I could just find a way to get the JTable to send any kind of event to any class that I can write, I would be happy. That would do the trick.
    One possibility is to just use a mouse listener, although that seems like a very low-level way to do something which should be at a higher level. But that seems like the only way. Try as I might, I can't find a way to set an editor for these cells.
    I used table.setDefaultEditor(JButton.class, myTableCellEditor), and I tried to set it on the column, too, but myTableCellEditor never gets called in response to events.
    Any other approaches?
    Thanks

  • JTable Cell Renderer problems

    Hi,
    I ahve created a cellRenderer so that it renders jCheckBox in the cell. However, the check boxes are intended for use by boolean values, and thats how they work best. However the problem i am having is that i want to render the boxes into cells that have either a 1 or 0. I have managed to do this and display the correct state of the box but when i want to make changes i have to change the value 0 or 1 inorder to change the check box. Is there a way i can select or deselect the checkbox when editing???
    Thanks in advance
    Rudy

    Hi,
    I am going to change my table model to try and make it more generic. However before i do so and mess everything up i would like to know if the following will work.
    1. I will pass to the constructor names of columns that i want to be rendered as checkboxes.
    2. I will then get the column index of that column using
    public int getColumnIndex(String name) {
        for(int i = 0; i < columnNames.length; i++){
          if(columnNames.trim().equals(name)){
    return i;
    return -1; //if fails
    3. In my get columnclass which is implemented as follows (from java demo)
    public Class getColumnClass(int column) {
        int type;
        try {
          type = metaData.getColumnType(column + 1);
        catch (SQLException e) {
          return super.getColumnClass(column);
        switch (type) {
          case Types.CHAR:
          case Types.VARCHAR:
          case Types.LONGVARCHAR:
            return String.class;
          case Types.BIT:
            return Boolean.class;
          case Types.TINYINT:
          case Types.SMALLINT:
          case Types.INTEGER:
            return Integer.class;
          case Types.BIGINT:
            return Long.class;
          case Types.FLOAT:
          case Types.DOUBLE:
            return Double.class;
          case Types.DATE:
          case Types.TIMESTAMP:
            return java.sql.Date.class;
          default:
            return Object.class;
    }I can add a condition that "if column (int) == the indexes if previously retrieved then return boolean class"
    With this work columns that has either Y, N or null or 0, 1, or null???
    I may change the columnNames to a vector so that i can use the contains method and therefore not need to find the index.
    Will this work???
    Thanks
    Rudy

Maybe you are looking for

  • [Forum FAQ] How to disable Microsoft account default sign-in behavior when accessing Microsoft website on Windows 8.1

    Scenario By default it will sign in with current Microsoft account, if a user accesses Microsoft website (www.live.com, www.bing.com, etc.) with Microsoft account on Windows 8.1. This article describes how to disable this default sigh-in behavior if

  • ORA-00600 error when registering XML Schema

    Dear Mark! 1. Database version: 9.2.0.3.0 2. Operating system: Windows XP Prof 3. XML Schema: (it's too big to be posted_ 4. Instance document: no 5. SQL statement used to register the schema DBMS_XMLSCHEMA.registerSchema(schemaURL => aSchemaURL, sch

  • CREDIT DAYS

    Dear Experts As we have come across the problem of CR.Days in SAP. We have found that, if customer having limit of 1 million and 30 CR.Days . SAP will allowed to buy the goods till 1 million even though if customer has not paid more than 30 days.  Th

  • Cannot open the content file with code supplied to load Lion

    Dear sirs, I boat a new MacBook Pro on the 24th. of July. The seller in the store promised me that deserve a free Lion. I followed instruction i got and contacted Apple with all necessary information as well an invoice - i received from you a referen

  • Turn off security exceptions in Internet Explorer

    When I want to read files with my applet using IE, I always get a security exception, though I've granted everything possible for unsigned applets !!! please help!!! any help is appreciated. cu oliver