Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Tim Cooke
Campbell Ritchie
Ron McLeod
Junilu Lacar
Liutauras Vilda
Sheriffs:
Paul Clapham
Jeanne Boyarsky
Henry Wong
Saloon Keepers:
Tim Moores
Tim Holloway
Stephan van Hulst
Piet Souris
Carey Brown
Bartenders:
Jesse Duncan
Frits Walraven
Mikalai Zaikin
Forum:
Swing / AWT / SWT
How to disable the button in JTable
Naveen Kumar
Ranch Hand
Posts: 35
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi All,
I have JPanel.
In my JPanel I have one JTable and One JButton.(Button Name is Continue)
I wish to have a table rows such a way that, first column should have
button (
in all rows
..) and rest are all
string
columns (
in all rows
.)
If I click the continue Button on the JPanel I want to disable all the buttons in the JTable.
Any body please help me.
Regards
Naveen
Craig Wood
Ranch Hand
Posts: 1535
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import java.awt.*; import java.awt.event.*; import java.util.EventObject; import javax.swing.*; import javax.swing.table.*; public class TableButton implements ActionListener { JTable table; public void actionPerformed(ActionEvent e) { JButton editorButton = (JButton)e.getSource(); String ac = editorButton.getActionCommand(); System.out.println("ac = " + ac); } private JScrollPane getContent() { table = new JTable(getModel()); table.setCellSelectionEnabled(true); int height = (new JButton(" ")).getPreferredSize().height; table.setRowHeight(height); TableColumnModel colModel = table.getColumnModel(); colModel.getColumn(0).setCellRenderer(new ButtonRenderer()); colModel.getColumn(0).setCellEditor(new ButtonEditor(this)); return new JScrollPane(table); } private AbstractTableModel getModel() { return new AbstractTableModel() { public int getColumnCount() { return 3; } public int getRowCount() { return 6;} public boolean isCellEditable(int row, int col) { return col == 0; } public Object getValueAt(int row, int col) { if(col == 0) return "Button " + (row + 1) + (col + 1); return String.valueOf(row + 1) + (col + 1); } }; } private JPanel getLastPanel() { JToggleButton toggle = new JToggleButton("Disable Buttons"); toggle.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JToggleButton tb = (JToggleButton)e.getSource(); TableColumn col= table.getColumnModel().getColumn(0); ButtonRenderer renderer = (ButtonRenderer)col.getCellRenderer(); ButtonEditor editor = (ButtonEditor)col.getCellEditor(); boolean enable; if(tb.isSelected()) { enable = false; tb.setText("Enable Buttons"); } else { enable = true; tb.setText("Disable Buttons"); } renderer.setEnabled(enable); editor.setEnabled(enable); table.repaint(); } }); JPanel panel = new JPanel(); panel.add(toggle); return panel; } public static void main(String[] args) { TableButton test = new TableButton(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(test.getContent()); f.getContentPane().add(test.getLastPanel(), "Last"); f.setSize(340,300); f.setLocation(200,200); f.setVisible(true); } } class ButtonRenderer implements TableCellRenderer { JButton button; boolean enabled = true; public ButtonRenderer() { button = new JButton(); } public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { String text = (String)value; button.setText(text); button.setEnabled(enabled); return button; } public void setEnabled(boolean enabled) { this.enabled = enabled; } } class ButtonEditor extends AbstractCellEditor implements TableCellEditor, ActionListener { JButton button; boolean enabled = true; int clickCountToStart = 1; public ButtonEditor(ActionListener al) { button = new JButton(); button.addActionListener(this); button.addActionListener(al); } public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { String text = (String)value; button.setText(text); button.setActionCommand(text); button.setEnabled(enabled); return button; } public void setEnabled(boolean enabled) { this.enabled = enabled; } public Object getCellEditorValue() { return button.getText(); } public boolean isCellEditable(EventObject anEvent) { if (anEvent instanceof MouseEvent) { return ((MouseEvent)anEvent).getClickCount() >= clickCountToStart; } return true; } public void actionPerformed(ActionEvent e) { super.stopCellEditing(); } }
Naveen Kumar
Ranch Hand
Posts: 35
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi Craig Wood,
Thank You Very Much...!
Your code is very help full to my scenario.
Regards
Naveen
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
JTable ........
JTable
Problem in printing a JTable
MultiComponent JTable
Jtable
More...