Dragging rows in JTable
Can anybody tell me how to drag and drop rows in JTable?
if someone could give me a code snippet (Or) refer to an example, i will be very happy !.\
Thanks
Shriram
By the way, I just tested this assumption of mine, and found I was wrong if you're using default JTable drag and drop.
However, I'm using my own implementation. I notice in a simple program without any drag and drop, I get the behavior I mentioned above, so I'm guessing this IS default JTable behavior, which is then modified by the built-in drag and drop support.
So the trick will be, how to do what built-in drag and drop is doing, without using built-in drag and drop. :-)
- Tim
Similar Messages
-
How to set background color of row in JTable
Hi,I want to set different background color to rows in JTable according to some value in the this row.
eg.
no name isGood
1 aaa yes (this row's background color is red)
2 bbb no (this row's background color is blue)
3 ccc yes (this row's background color is red)
4 ddd yes (this row's background color is red)
5 eee no (this row's background color is blue)
thanksthanks!*_*
-
How to set background color in row of JTable ?
i am new in java please tell me about How to set background color in row of JTable ? please example code. Thnak you.
Here is an example: http://www.javaworld.com/javaworld/javaqa/2001-09/03-qa-0928-jtable.html
For more info on how to use tables read the swing tutorial: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
ICE -
How to hide row in JTable?
Hi all,
How to hide some specific rows in JTable for user view filtering purpose?
ThanksTry to use the Table Model.
The "getValueAt" Methode decide what to Display.
So a simple "if" command can hide the complete row - or a single Statement.
public Object getValueAt(int row, int col) {
ArrayList al = new ArrayList();
StueliTeil tabellenzeile = (StueliTeil) getDaten().get(row);
switch (col) {
case 0 :
return tabellenzeile.getUmfang();
case 1 :
return tabellenzeile.getTakt();
... -
Inserting row in JTable (runtime)
i need to inserting row in JTable in runtime so tell me what it do
dear farhanaj ,
try this code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;
/* <applet code=tableadd.class width=200 height=200>
</applet>
public class tableadd extends JApplet implements ActionListener
Object[] data = new Object[5];
DefaultTableModel defaulttablemodel = new DefaultTableModel();
JTable jtable=new JTable(defaulttablemodel);
JPanel jpanel= new JPanel();
private JPanel topPanel,jpPanel;
public JTable table;
JButton jbutton1 = new JButton("create new row");
JButton jbutton2 = new JButton("create new col");
//JButton jbutton1=new Jbutton("r"),jbutton2=new Jbutton("c");
public tableadd()
for(int column=0 ; column<5 ; column++)
defaulttablemodel.addColumn("column" + column);
for(int row=0 ; row<5;row++)
for(int column=0 ;column<5;column++)
data[column]="cell" row "," + column;
defaulttablemodel.addRow(data);
//getContentPane().add(new JScrollPane(jtable) ,BorderLayout.CENTER);
//getContentPane().add(new JPanel() ,BorderLayout.SOUTH);
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(jtable , v, h);
getContentPane().add(jsp, BorderLayout.CENTER);
topPanel = new JPanel();
jpPanel=new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add(topPanel,BorderLayout.NORTH);
getContentPane().add(jpPanel,BorderLayout.SOUTH);
jpPanel.add(jbutton1);
jpPanel.add(jbutton2);
jbutton1.addActionListener(this);
jbutton2.addActionListener(this);
public void actionPerformed(ActionEvent e)
if (e.getSource() == jbutton1)
int numberrows = defaulttablemodel.getRowCount();
int numbercolumns=defaulttablemodel.getColumnCount();
Object[] data = new Object[numbercolumns];
for(int column=0 ; column<numbercolumns;column++)
data[column]="cell" numberrows "," +column;
defaulttablemodel.addRow(data);
else
if (e.getSource() ==jbutton2)
int numberrows=defaulttablemodel.getRowCount();
int numbercolumns=defaulttablemodel.getColumnCount();
defaulttablemodel.addColumn("column" + numbercolumns);
for(int row=0; row<numberrows ; row++)
defaulttablemodel.setValueAt("cell" row "," +numbercolumns ,row , numbercolumns);
System.out.println(row);
//jtable.sizeColumnsToFit(0); -
How to set different font for a particular row in jtables?
How to set different font size and font type for a particular row in jtable?
More than enough sample code here:
[http://java.sun.com/docs/books/tutorial/uiswing/components/table.html]
db -
How to select a row in Jtable at runtime
how to select a row in Jtable at runtime.
use
setRowSelectionInterval(int fromRowIndex, int toRowIndex);example if your table has 10 rows then u want to select the rows from 4 to 8 then use
setRowSelectionInterval(3, 7);if you want to select just one row for example 5 then use
setRowSelectionInterval(5, 5); -
How to remove a row from JTable
Hi!
I'm used to remove rows from JTables getting the model and doing a removeRow(num) like this:
((DefaultTableModel)jTable1.getModel()).removeRow(0);
But with ADF and JDeveloper the model says it's a JUTableBinding.JUTableModel but its not accessible.
How to remove a row in Jdeveloper 10.1.3.4.0?Or maybe is just better to refresh data in the jTable but I do not know either like doing it.
-
How to apply different colors to specific rows in JTable
hi,
Anybody could tell me the way of colorising the rows in jtable.. I want to apply different colors to diff rows..
If i get a piece of code that could help me a lot..
thanks in advance,
Sapnayou'll find the answer at http://www2.gol.com/users/tame/swing/examples/SwingExamples.html
-
Hello I am new To JAVA SWING
I have 2 questions related to JTable
1) How do i remove selected row in JTable ...for instance, I click on the row to delete (It is selected) , later I press the delete button, and then row is removed .... any hints ?
2) If I would like to save the rows in a JTable into a database , .... How can iterate through the rows in the JTable..each row .... the rows are saved in a List object...and a List Object is what I pass as a parameter in a class method in order to be saved into Database.
Thanks,
deibys1) use the removeRow(...) method of the DefaultTableModel
20 use table.getModel().getValueAt(...) to get the data from the model and create your list Object. -
Problem in adding/deleting rows in JTable
I am trying to add /remove rows from JTable whose first column is JButton and others are JComboBox's.If no rows are selected,new row is added at the end.If user selects some row & then presses insert button,new row is added below it.Rows can only be deleted if user has made some selection.Kindly help me,where i am making mistake.If any function is to be used.My code is as follows....
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.border.*;
public class JButtonTableExample extends JFrame implements ActionListener{
JComboBox mComboLHSType = new JComboBox();
JComboBox mComboRHSType = new JComboBox();
JLabel mLabelLHSType = new JLabel("LHS Type");
JLabel mLabelRHSType = new JLabel("RHS Type");
JButton mButtonDelete = new JButton("Delete");
JButton mButtonInsert = new JButton("Insert");
JPanel mPanelButton = new JPanel();
JPanel mPanelScroll = new JPanel();
JPanel mPanelCombo = new JPanel();
DefaultTableModel dm ;
JTable table;
int currentRow = -1;
static int mSelectedRow = -1;
public JButtonTableExample()
super( "JButtonTable Example" );
makeForm();
setSize( 410, 222 );
setVisible(true);
private void makeForm()
this.getContentPane().setLayout(null);
mPanelCombo.setLayout(null);
mPanelCombo.setBorder(new LineBorder(Color.red));
mPanelCombo.setBounds(new Rectangle(1,1,400,30));
mLabelLHSType.setBounds(new Rectangle(26,5,71,22));
mComboLHSType.setBounds(new Rectangle(83,5,100,22));
mLabelRHSType.setBounds(new Rectangle(232,5,71,22));
mComboRHSType.setBounds(new Rectangle(292,5,100,22));
mPanelCombo.add(mLabelLHSType,null);
mPanelCombo.add(mComboLHSType,null);
mPanelCombo.add(mLabelRHSType,null);
mPanelCombo.add(mComboRHSType,null);
mPanelScroll.setLayout(null);
mPanelScroll.setBorder(new LineBorder(Color.blue));
mPanelScroll.setBounds(new Rectangle(1,28,400,135));
mPanelButton.setLayout(null);
mPanelButton.setBorder(new LineBorder(Color.green));
mPanelButton.setBounds(new Rectangle(1,165,400,30));
mButtonInsert.setBounds(new Rectangle(120,5,71,22));
mButtonDelete.setBounds(new Rectangle(202,5,71,22));
mButtonDelete.addActionListener(this);
mButtonInsert.addActionListener(this);
mPanelButton.add(mButtonDelete,null);
mPanelButton.add(mButtonInsert,null);
dm = new DefaultTableModel();
//dm.setDataVector(null,
//new Object[]{"Button","Join","LHS","Operator","RHS"});
dm.setDataVector(new Object[][]{{"","","","",""}},
new Object[]{"","Join","LHS","Operator","RHS"});
table = new JTable(dm);
table.getTableHeader().setReorderingAllowed(false);
table.setRowHeight(25);
int columnWidth[] = {20,45,95,95,95};
TableColumnModel modelCol = table.getColumnModel();
for (int i=0;i<5;i++)
modelCol.getColumn(i).setPreferredWidth(columnWidth);
//modelCol.getColumn(0).setCellRenderer(new ButtonRenderer());
//modelCol.getColumn(0).setCellEditor(new ButtonEditor(new JCheckBox()));
modelCol.getColumn(0).setCellRenderer(new ButtonCR());
modelCol.getColumn(0).setCellEditor(new ButtonCE(new JCheckBox()));
modelCol.getColumn(0).setResizable(false);
setUpJoinColumn(modelCol.getColumn(1));
setUpLHSColumn(modelCol.getColumn(2));
setUpOperColumn(modelCol.getColumn(3));
setUpRHSColumn(modelCol.getColumn(4));
JScrollPane scroll = new JScrollPane(table);
scroll.setBounds(new Rectangle(1,1,400,133));
mPanelScroll.add(scroll,null);
this.getContentPane().add(mPanelCombo,null);
this.getContentPane().add(mPanelScroll,null);
this.getContentPane().add(mPanelButton,null);
}//end of makeForm()
public void actionPerformed(ActionEvent ae)
if (ae.getSource() == mButtonInsert)
//int currentRow = table.getSelectedRow();
currentRow = ButtonCE.selectedRow;
System.out.println("Before Insert CURRENT ROW"+currentRow);
if(currentRow == -1)
int rowCount = dm.getRowCount();
//mSelectedRow = rowCount-1;
//table.clearSelection();
dm.insertRow(rowCount,new Object[]{"","","","",""});
currentRow = -1;
ButtonCE.selectedRow = -1;
else
table.clearSelection();
dm.insertRow(currentRow+1,new Object[]{"","","","",""});
currentRow = -1;
ButtonCE.selectedRow = -1;
//System.out.println("After INSERT CURRENT ROW"+currentRow);
if(ae.getSource() == mButtonDelete)
//int currentRow = table.getSelectedRow();
currentRow = ButtonCE.selectedRow;
System.out.println("Before DELETE CURRENT ROW"+currentRow);
if(currentRow != -1)
dm.removeRow(currentRow);
table.clearSelection();
currentRow = -1;
ButtonCE.selectedRow = -1;
//System.out.println("Selected Row"+mSelectedRow);
else
JOptionPane.showMessageDialog(null, "Select row first", "alert", JOptionPane.ERROR_MESSAGE);
//System.out.println("DELETE CURRENT ROW"+currentRow);
public void setUpJoinColumn(TableColumn joinColumn)
//Set up the editor for the sport cells.
JComboBox comboBox = new JComboBox();
comboBox.addItem("AND");
comboBox.addItem("OR");
comboBox.addItem("NOT");
joinColumn.setCellEditor(new DefaultCellEditor(comboBox));
//Set up tool tips for the sport cells.
DefaultTableCellRenderer renderer =
new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
joinColumn.setCellRenderer(renderer);
//Set up tool tip for the sport column header.
TableCellRenderer headerRenderer = joinColumn.getHeaderRenderer();
if (headerRenderer instanceof DefaultTableCellRenderer) {
((DefaultTableCellRenderer)headerRenderer).setToolTipText(
"Click the sport to see a list of choices");
public void setUpLHSColumn(TableColumn LHSColumn)
//Set up the editor for the sport cells.
JComboBox comboBox = new JComboBox();
comboBox.addItem("Participant1");
comboBox.addItem("Participant2");
comboBox.addItem("Variable1");
LHSColumn.setCellEditor(new DefaultCellEditor(comboBox));
//Set up tool tips for the sport cells.
DefaultTableCellRenderer renderer =
new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
LHSColumn.setCellRenderer(renderer);
//Set up tool tip for the sport column header.
TableCellRenderer headerRenderer = LHSColumn.getHeaderRenderer();
if (headerRenderer instanceof DefaultTableCellRenderer) {
((DefaultTableCellRenderer)headerRenderer).setToolTipText(
"Click the sport to see a list of choices");
public void setUpOperColumn(TableColumn operColumn)
//Set up the editor for the sport cells.
JComboBox comboBox = new JComboBox();
comboBox.addItem("=");
comboBox.addItem("!=");
comboBox.addItem("Contains");
operColumn.setCellEditor(new DefaultCellEditor(comboBox));
//Set up tool tips for the sport cells.
DefaultTableCellRenderer renderer =
new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
operColumn.setCellRenderer(renderer);
//Set up tool tip for the sport column header.
TableCellRenderer headerRenderer = operColumn.getHeaderRenderer();
if (headerRenderer instanceof DefaultTableCellRenderer) {
((DefaultTableCellRenderer)headerRenderer).setToolTipText(
"Click the sport to see a list of choices");
public void setUpRHSColumn(TableColumn rhsColumn)
//Set up the editor for the sport cells.
JComboBox comboBox = new JComboBox();
comboBox.addItem("Variable1");
comboBox.addItem("Constant1");
comboBox.addItem("Constant2");
rhsColumn.setCellEditor(new DefaultCellEditor(comboBox));
//Set up tool tips for the sport cells.
DefaultTableCellRenderer renderer =
new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
rhsColumn.setCellRenderer(renderer);
//Set up tool tip for the sport column header.
TableCellRenderer headerRenderer = rhsColumn.getHeaderRenderer();
if (headerRenderer instanceof DefaultTableCellRenderer) {
((DefaultTableCellRenderer)headerRenderer).setToolTipText(
"Click the sport to see a list of choices");
public static void main(String[] args) {
JButtonTableExample frame = new JButtonTableExample();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
//Button as a renderer for the table cells
class ButtonCR implements TableCellRenderer
JButton btnSelect;
public ButtonCR()
btnSelect = new JButton();
btnSelect.setMargin(new Insets(0,0,0,0));
public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int column)
if (column != 0) return null; //meany !!!
//System.out.println("Inside renderer########################Selected row");
//btnSelect.setText(value.toString());
//btnSelect.setIcon(new ImageIcon("capsigma.gif"));
return btnSelect;
}//end fo ButtonCR
//Default Editor for table
class ButtonCE extends DefaultCellEditor implements ActionListener
JButton btnSelect;
JTable table;
//Object val;
static int selectedRow = -1;
public ButtonCE(JCheckBox whoCares)
super(whoCares);
//this.row = row;
btnSelect = new JButton();
btnSelect.setMargin(new Insets(0,0,0,0));
btnSelect.addActionListener(this);
setClickCountToStart(1);
public Component getTableCellEditorComponent(JTable table,Object value,boolean isSelected,int row,int column)
if (column != 0) return null; //meany !!!
this.selectedRow = row;
this.table = table;
table.clearSelection();
System.out.println("Inside getTableCellEditorComponent");
return btnSelect;
//public Object getCellEditorValue()
//return val;
public void actionPerformed(ActionEvent e)
// Your Code Here...
System.out.println("Inside actionPerformed");
System.out.println("Action performed Row selected "+selectedRow);
btnSelect.setIcon(new ImageIcon("capsigma.gif"));
}//end of ButtonCEHi,
All the thing you have to do is to return a boolean for the column. JTable will use a checkbox (as default) to show boolean values. -
Please I need help
How can I hide a row in Jtable.
For example if you push a button hide first row.
ThanksOne simple way you can do is:
just remove this row but keep it somewhere so that you may add it back when you want later. :-) -
Dear friends,
When i select a whole row in JTable it gets selected but the background color of the row selected does not change. i.e. Its not highlighted as in MS-Excel.
How can i overcome this problem?
Kindly reply
Thanks.Hi,
Thanx for ur inputs. This is what the code i wrote. When u run this code,
If we select row1 column1 the cells highlighted will be on row 2, 3 and 4. If we select row1 column 2 the selected cells will be on row 1, 3 and 4. I want the row to be selected fully when we select coumn 1 only.
For eg. if i select row 200 from column 1 the 200 th row should only be fully highlighted
Anyway i'll try the above code and check
Here is the code
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class table1 extends JFrame
// Variables declaration
private JTable jTable1;
private JScrollPane jScrollPane1;
private JPanel contentPane;
// End of variables declaration
public table1()
super();
this.setVisible(true);
private void initializeComponent()
jTable1 = new JTable();
jScrollPane1 = new JScrollPane();
contentPane = (JPanel)this.getContentPane();
jTable1.setModel(new DefaultTableModel(4, 4));
jScrollPane1.setViewportView(jTable1);
contentPane.setLayout(null);
addComponent(contentPane, jScrollPane1, 58,65,301,207);
this.setTitle("table1 - extends JFrame");
this.setLocation(new Point(0, 0));
this.setSize(new Dimension(473, 539));
private void addComponent(Container container,Component c,int x,int
y,int width,int height)
c.setBounds(x,y,width,height);
container.add(c);
} Thanks again -
Query regarding updating rows in JTable
Query regarding updating rows in JTable
Hello,
I have a JTable with 6 columns and 1000s of rows (which are data read from flat files)
I can select 1 or more rows and change the values of the
columns. each time I do this I need to update the values
in the flat file.
Currently I assign the updated Jtable values to a vector
Vector rowVector = (Vector)defaultModel.getDataVector();
then I iterate over the vector and compare the values with the (old) data
in the JTable.
for(int rowCount = 0; rowCount<rowVector.size(); rowCount++){
Vector v = (Vector)rowVector.elementAt(rowCount);
//smsList is the Vector that contains the old JTable values
for(int i=0; i<smsList.size(); i++){
//If colums values have been changed; add that
//vector value to another vector
selectedsmsList.add(smsList.get(i));
for(int i=0; i<selectedsmsList.size(); i++){
//Update the values in the flat file
}This works fine except that it takes ages to iterate over the updated vecor and un-updated,old vector; is there any way to directly get the list of rows that were updated in the jtable; so that I can directly do an I/O operation to update the jtablke values?Just a suggestion.
You could add a listener and use a vector of booleans to keep track of the rows that have been changed. You could then iterate through this boolean vector and update the changed rows.
See
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#modelchange
Don't know whether this will be helpful.
Regards, Darryl -
Problem adding columns and rows in JTable
hi, hope somebody can help me
i have some problems when i add columns and later i try to set number of rows in JTable, ocurr some exceptions
my code is something like thisJTable tabla = new JTable();
TableColumn tc = new TableColumn();
tc.setHeaderValue("col1");
tc.setPreferredWidth(20);
tabla.getColumnModel().addColumn(tc);up to here everything goes well, my table shows the column, but when i add this line((DefaultTableModel)tabla.getModel()).setNumRows(1);ocurrs the next exception
Exception occurred during event dispatching:
java.lang.ArrayIndexOutOfBoundsException: 0 >= 0....
any ideas????oks, that would work when the JTable is created, but now we say that I require a method which adds a new row to the JTable a some moment of execution
Look some of my code
i have this in a JFrame...
JTable tabla = new JTable();
DefaultTableModel tm = new DefaultTableModel();
tabla.setModel(tm);
agrCol("Col1", 80);
agrCol("Col2", 160);
agrFil();
agrFil();
agrCol(String col, int lon) {
tm.addColumn(col);
tabla.getColumn(col).setPreferedWith(lon);
agrFil() {
tm.setRowCount(tabla.getRowCount() + 1);
the jtable shows the 2 columns and 2 rows, but the setPreferedWith work only in the last col, i'm confused
Maybe you are looking for
-
Hello: I got this brand new printer two days ago. I setup the printer on all my laptops & desktop. (mac & pc). Web service is enable, Apps is on and so is e-print. There is no problem at all printing from my computers or from [email protected] The p
-
An individual submitted a form and then tried to resubmit the form with more information and was not able to. Any ideas? It was from the same email address and now the email button did not respond.
-
Visto che il thread italiano sul richiao ipod nano è diventato molto popolare, e che tante persone non hanno tempo/voglia di leggere tutti i post, cerco di ricapitolare in questo nuovo thread alcune delle domande più chieste, con relativa risposta! H
-
Upgrade: Function Module does not exist
Hi Experts, transaction FOIM is giving DUMP in ECC. It shows a message Function module "/CUKREQ/RECN_EVENT_FMOD2" not found. This function module exist in 4.6c. Will anyone suggest what to do? Regards: Vipin
-
On a newly installed compiler, Visual Studio 2013, I get some linker error: Sample program (for test): #include <stdio.h> int main () printf("Help"); return 0; I get some linker errors: Error 2 error LNK2001: unresolved external symbol __imp_printf F