Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: netbeans JTable Jbutton

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Location
    -
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation netbeans JTable Jbutton

    I went through many links on the internet for Jtable Button ! since I am lil good with C# i thought It will be easy to switch to JAVA but I am having a hard time in solving one problem and I can do core Java and netbeans JDBC stuff but I don't understand how to append core java into netbeans ! It seems that core JAVA and netbeans JAVA coding methods differs

    I went through this coding which is in Java2SS site
    Button Table Example : Grid TableSwing ComponentsJava


    how to use this code for a netbeans project pls share ur ideas !

    ((DefaultTableModel)JTable.getModel()).getColumnName(0).r....

    I tried to use the aboce code but it gives me many errors !!! pls help
    Last edited by Mr.President; July 12th, 2010 at 01:22 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: netbeans JTable Jbutton

    it gives me many errors
    Can you copy and paste the full text of the error messages here?

  3. The Following User Says Thank You to Norm For This Useful Post:

    Mr.President (July 15th, 2010)

  4. #3
    Junior Member
    Join Date
    Jul 2010
    Location
    -
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: netbeans JTable Jbutton

     initComponents();
     
    //DefaultTableModel dm = new DefaultTableModel();
     
    //dm.setDataVector(new Object[][]{{"button 1", "foo"}, {"button 2", "bar"}}, new Object[]{"Button", "String"});
     
     jTable1.getColumn("Button").setCellRenderer(new ButtonRenderer());
     
     jTable1.getColumn("Button").setCellEditor(new ButtonEditor(new JCheckBox()));
     
    ((DefaultTableModel)jTable1.getModel()).addRow(new Object[] {"NX1001","Product name","Delete"});

    outPut :



    1. but I don't want the empty records ! I only need the last record which I added ?


    2. I assume tht the below function is the one which triggers or acts as a event ! how to get the row[0] value when the delete button is clicked
      public ButtonEditor(JCheckBox checkBox) {
        super(checkBox);
        button = new JButton();
        button.setOpaque(true);
        button.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            fireEditingStopped();
          }
        });
      }
    Last edited by Mr.President; July 14th, 2010 at 02:27 PM.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: netbeans JTable Jbutton

    the below function is the one which triggers or acts as a event
    Sort of. The actionPerformed() method is called when there is an Action Event on the button its a listener for.

    Are the errors, logic errors or what kind of error are they? Compile or runtime exceptions?

  6. The Following User Says Thank You to Norm For This Useful Post:

    Mr.President (July 15th, 2010)

  7. #5
    Junior Member
    Join Date
    Jul 2010
    Location
    -
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation Re: netbeans JTable Jbutton

    There were some coding errors I managed to solve'em and btw thx alot 4 replying !

    Now I am trying the Ttable button click event
    Below is the code which I copied from java2s now the code is working perfectly the next thing I am trying is when I click the delete button how am I gonna get the row[0] ID to execute a delete function ! and I have some other doubts which is related to the logic of this program I will ask'em lil lator ! any hint or suggestion on coding the delete button !?


    class ButtonRenderer extends JButton implements TableCellRenderer {
     
      public ButtonRenderer() {
        setOpaque(true);
      }
     
      public Component getTableCellRendererComponent(JTable table, Object value,
                       boolean isSelected, boolean hasFocus, int row, int column) {
        if (isSelected) {
          setForeground(table.getSelectionForeground());
          setBackground(table.getSelectionBackground());
        } else{
          setForeground(table.getForeground());
          setBackground(UIManager.getColor("Button.background"));
        }
        setText( (value ==null) ? "" : value.toString() );
        return this;
      }
    }
    class ButtonEditor extends DefaultCellEditor {
      protected JButton button;
      private String    label;
      private boolean   isPushed;
     
      public ButtonEditor(JCheckBox checkBox) {
        super(checkBox);
        button = new JButton();
        button.setOpaque(true);
        button.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            fireEditingStopped();
          }
        });
      }
     
        @Override
      public Component getTableCellEditorComponent(JTable table, Object value,
                       boolean isSelected, int row, int column) {
        if (isSelected) {
          button.setForeground(table.getSelectionForeground());
          button.setBackground(table.getSelectionBackground());
        } else{
          button.setForeground(table.getForeground());
          button.setBackground(table.getBackground());
        }
        label = (value ==null) ? "" : value.toString();
        button.setText( label );
        isPushed = true;
        return button;
      }
     
        @Override
      public Object getCellEditorValue() {
        if (isPushed)  {
    [COLOR="Red"]      //
          //
          JOptionPane.showMessageDialog(button ,label + ": Ouch!");
          // System.out.println(label + ": Ouch!");[/COLOR]
        }
        isPushed = false;
        return new String( label ) ;
      }
     
        @Override
      public boolean stopCellEditing() {
        isPushed = false;
        return super.stopCellEditing();
      }
     
        @Override
      protected void fireEditingStopped() {
        super.fireEditingStopped();
      }
    }
    Last edited by Mr.President; July 15th, 2010 at 12:50 AM.

  8. #6
    Junior Member
    Join Date
    Jul 2010
    Location
    -
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: netbeans JTable Jbutton

    help needed !

  9. #7
    Junior Member
    Join Date
    Jul 2010
    Location
    -
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: netbeans JTable Jbutton

    any 1 pls tell me how to code the action listener for the data grid ?

Similar Threads

  1. Add or remove a row on a jtable with Netbeans 6.0
    By Pieter in forum Java Theory & Questions
    Replies: 1
    Last Post: July 8th, 2010, 02:40 PM
  2. JTable in JScrollPane
    By alwayslearner in forum AWT / Java Swing
    Replies: 1
    Last Post: January 31st, 2010, 10:24 PM
  3. Printing a JTable
    By hundu in forum AWT / Java Swing
    Replies: 0
    Last Post: June 29th, 2009, 08:15 AM
  4. How to printing a Jtable
    By hundu in forum AWT / Java Swing
    Replies: 0
    Last Post: June 29th, 2009, 06:57 AM
  5. Printing JTable that retrieve data from the Database
    By hundu in forum AWT / Java Swing
    Replies: 3
    Last Post: June 28th, 2009, 01:50 PM