lawang code

lawang Code


Tanyakan kode disini

Cara memasukkan jButton ke dalam JTable

ditanyakan oleh ilham syaiful akbar 4 tahun yang lalu

permisi gan, maaf sebelumnya ane masih newbie, ane mau nanya cara masukin jButton kedalam JTable gimana?

mohon bantuanya gan..

Menampilkan 1-1 dari 1 hasil
handita okviyanto menjawab 4 tahun yang lalu :

Nih ane ambil dari stackoverflow ..

Cara menambahkan jButtonnya

   JTable table = new JTable(new JTableModel()); 
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true); 

TableCellRenderer buttonRenderer = new JTableButtonRenderer();
table.getColumn("Button1").setCellRenderer(buttonRenderer);
table.getColumn("Button2").setCellRenderer(buttonRenderer);

Contoh table modelnya seperti ini

public static class JTableModel extends AbstractTableModel {
    private static final long serialVersionUID = 1L;
    private static final String[] COLUMN_NAMES = new String[] {"Id", "Stuff", "Button1", "Button2"};
    private static final Class<?>[] COLUMN_TYPES = new Class<?>[] {Integer.class, String.class, JButton.class,  JButton.class};

    @Override public int getColumnCount() {
        return COLUMN_NAMES.length;
    }

    @Override public int getRowCount() {
        return 4;
    }

    @Override public String getColumnName(int columnIndex) {
        return COLUMN_NAMES[columnIndex];
    }

    @Override public Class<?> getColumnClass(int columnIndex) {
        return COLUMN_TYPES[columnIndex];
    }

    @Override public Object getValueAt(final int rowIndex, final int columnIndex) {
            /*Adding components*/
        switch (columnIndex) {
            case 0: return rowIndex;
            case 1: return "Text for "+rowIndex;
            case 2: // fall through
           /*Adding button and creating click listener*/
            case 3: final JButton button = new JButton(COLUMN_NAMES[columnIndex]);
                    button.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent arg0) {
                            JOptionPane.showMessageDialog(JOptionPane.getFrameForComponent(button), 
                                    "Button clicked for row "+rowIndex);
                        }
                    });
                    return button;
            default: return "Error";
        }
    }   
}

Terus kalau di click seperti ini pake listenernya..

private static class JTableButtonMouseListener extends MouseAdapter {
    private final JTable table;

    public JTableButtonMouseListener(JTable table) {
        this.table = table;
    }

    public void mouseClicked(MouseEvent e) {
        int column = table.getColumnModel().getColumnIndexAtX(e.getX()); // get the coloum of the button
        int row    = e.getY()/table.getRowHeight(); //get the row of the button

                /*Checking the row or column is valid or not*/
        if (row < table.getRowCount() && row >= 0 && column < table.getColumnCount() && column >= 0) {
            Object value = table.getValueAt(row, column);
            if (value instanceof JButton) {
                /*perform a click event*/
                ((JButton)value).doClick();
            }
        }
    }
}

Kemudian JTableCellRenderer pake ini nih..

private static class JTableButtonRenderer implements TableCellRenderer {        
    @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        JButton button = (JButton)value;
        return button;  
    }
} 




Anda harus login terlebih dahulu untuk memberikan komentar

Recent Tag

X 6
X 48
X 14
X 10
X 1
X 5
X 4
X 18
X 10

Bantuan

Help

Feedback

Help