Different component in different cell of a jTable column

F

frafel

I need to insert in the cells of a particular jTable column, different
components; a good example could be:

In a table with two columns and three rows, referring to the second
column, I would like to put in the first cell (0, 1) a check box; in
the second cell (1, 1) a combobox with the items "one", "two", "three"
and I will left the third cell (2, 1) ready to accept a string from
the user (means that here I don't need to insert any one component).

I have seen that using the following snippets of code:

JCheckBox mycomp = new JCheckBox("prova", true);
mycomp.setVisible(rootPaneCheckingEnabled);
mycomp.setEnabled(true);
DefaultCellEditor editor = new DefaultCellEditor(mycomp);
TableColumnModel tcm = jTable1.getColumnModel();
tcm.getColumn(1).setCellEditor(editor);

or

String[] mio = {"uno","due","tre","quattro"};
JComboBox mycomp = new JComboBox(mio);
mycomp.setVisible(rootPaneCheckingEnabled);
mycomp.setEnabled(true);
DefaultCellEditor editor = new DefaultCellEditor(mycomp);
TableColumnModel tcm = jTable1.getColumnModel();
tcm.getColumn(1).setCellEditor(editor);

I can see the checkbox or combobox; but that components appears in all
the cells of the second column.

How I can change my code to reach my objective?

Thank you from Franco in Italy
 
A

Andrew Thompson

frafel said:
I need to insert in the cells of a particular jTable

Do you mean JTable? I am unfamiliar with jTable.
..column, different
components; a good example could be:

In a table with two columns and three rows, referring to the second
column, I would like to put in the first cell (0, 1) a check box; in
the second cell (1, 1) a combobox with the items "one", "two", "three"
and I will left the third cell (2, 1) ready to accept a string from
the user (means that here I don't need to insert any one component).

While that is very descriptive, I do not think it is a
good example of why you'd want to do anything
so silly.

Are these JCheckBox, JComboBox and JTextField
in any way related?

If so, it would be more intuitive to have them in a table
*row.* If not, they should probably not be in a table
*at all,* but instead put in a GridLayout.

--
Andrew Thompson
http://www.physci.org/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200712/1
 
F

frafel

Do you mean JTable? I am unfamiliar with jTable.



While that is very descriptive, I do not think it is a
good example of why you'd want to do anything
so silly.

Are these JCheckBox, JComboBox and JTextField
in any way related?

If so, it would be more intuitive to have them in a table
*row.* If not, they should probably not be in a table
*at all,* but instead put in a GridLayout.

Yes I means JTable!

Any way my job is the following;

In a two columns table I will insert rows where each coople of columns
are respectively a question and a answer; it is possible that the
answer in some cases must be a string, in some other case the answer
must be selected from the items of a combobox, in other cases the
answer must be a true/false selection.

I fill the table programmatically with the questions (in the first
column) and I would prepare the cells of the second column with the
particular component the question implies. After received the answers,
I need to clear all the cells of the table and prepare it again with
new questions and the compomponents implied.

That is my need so I hope you can understant now my preceding message.

Please help me. Thank you. Franco.
 
D

Daniele Futtorovic

Any way my job is the following;

In a two columns table I will insert rows where each coople of columns
are respectively a question and a answer; it is possible that the
answer in some cases must be a string, in some other case the answer
must be selected from the items of a combobox, in other cases the
answer must be a true/false selection.

I fill the table programmatically with the questions (in the first
column) and I would prepare the cells of the second column with the
particular component the question implies. After received the answers,
I need to clear all the cells of the table and prepare it again with
new questions and the compomponents implied.

That is my need so I hope you can understant now my preceding message.

Please help me. Thank you. Franco.

Firstly, you should learn more about
- _TableCellRenderers_:
<http://java.sun.com/javase/6/docs/api/javax/swing/table/TableCellRenderer.html>
- _TableCellEditors_:
<http://java.sun.com/javase/6/docs/api/javax/swing/table/TableCellEditor.html>

As their names imply, the former are used to /render/ (viz. visualize) a
table's cell, and the latter are used when it comes to /editing/ that
cell (as opposed to your original post where you spoke only of editors).
The method these interfaces define get the coordinates of the cell to be
rendered/edited passed as arguments; and based on these you will be able
to return components according to your needs.

You most probably won't be able to use the DefaultCellRenderer for your
task.

I suggest you study the informations provided on this page:
<http://java.sun.com/docs/books/tutorial/uiswing/components/table.html>

A properly formulated google search will also be likely to yield further
help. Something like "java jtable custom renderer editor" :)scratch: did
I really have to specify that?)

df.
 
D

Daniele Futtorovic

Firstly, you should learn more about
- _TableCellRenderers_:
<http://java.sun.com/javase/6/docs/api/javax/swing/table/TableCellRenderer.html>

- _TableCellEditors_:
<http://java.sun.com/javase/6/docs/api/javax/swing/table/TableCellEditor.html>


As their names imply, the former are used to /render/ (viz. visualize) a
table's cell, and the latter are used when it comes to /editing/ that
cell (as opposed to your original post where you spoke only of editors).
The method these interfaces define get the coordinates of the cell to be
rendered/edited passed as arguments; and based on these you will be able
to return components according to your needs.

You most probably won't be able to use the DefaultCellRenderer for your
task.

I suggest you study the informations provided on this page:
<http://java.sun.com/docs/books/tutorial/uiswing/components/table.html>

A properly formulated google search will also be likely to yield further
help. Something like "java jtable custom renderer editor" :)scratch: did
I really have to specify that?)

df.

s/(Default)(CellRenderer)/$1Table$2/g
 
A

Andrew Thompson

frafel said:
...
Yes I means JTable!

Thanks for clearing that up.
Any way my job is the following;

In a two columns table I will insert rows where each coople of columns
are respectively a question and a answer; it is possible that the
answer in some cases must be a string, in some other case the answer
must be selected from the items of a combobox, in other cases the
answer must be a true/false selection.

Here is a 'tabular' arrangement of questions and answers.

<sscce>
import java.awt.*;
import javax.swing.*;

class QuestionTable {

public static Component getAnswerComponent(Object value) {
if (value instanceof String) {
return new JTextField((String)value, 20);
} else if (value instanceof Boolean) {
return new JCheckBox(
"",
((Boolean) value).booleanValue());
} else {
Integer[] values = (Integer[])value;
return new JComboBox(values);
}
}

public static void main(String[] args)
{
final Object[][] data =
{
{
"What is your name?",
""
},
{
"What is the meaning of life?",
new Integer[]
{
new Integer(21),
new Integer(42),
new Integer(63)
},
},
{
"Is this a table?",
new Boolean(false)
}
};

Runnable r = new Runnable() {
public void run() {
JPanel mainPanel =
new JPanel( new BorderLayout(5,5) );
JPanel questionPanel =
new JPanel( new GridLayout(0,1,5,5) );
JPanel answerPanel =
new JPanel( new GridLayout(0,1,5,5) );
mainPanel.add(
questionPanel,
BorderLayout.CENTER );
mainPanel.add(
answerPanel,
BorderLayout.EAST );

for (int ii=0; ii<data.length; ii++) {
questionPanel.add(
new JLabel((String)data[ii][0]) );
answerPanel.add(
getAnswerComponent( data[ii][1] ) );
}

JOptionPane.showMessageDialog(
null,
mainPanel,
"Questionnare",
JOptionPane.QUESTION_MESSAGE);
}
};
EventQueue.invokeLater(r);
}
}
</sscce>

--
Andrew Thompson
http://www.physci.org/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200712/1
 
F

frafel

Thanks Andrew (and also Daniele.

I understand what you suggest me; probably I will be able to use part
of your code but I beleive that will be hard for me as was hard to get
suggestions from a lot of sample I have seen on the network.

That is because (sorry I forget to tell before) I am using Netbeans
IDE to create my program, so I have yet a Frame (named Frame1), inside
it a panel, a scollpane and finally a table (named jTable1).

So my need is only to create checkbox or combobox (required from the
question) in the cells of the second column ot the jTable1 table (in
case of string answer the cells of the table are yet ready.

I am not sure if my situation requires a more complicated code or if
the code needed will be simpler.

So, I vill try to follow your suggestions but it's necessary to put
the string/checkbox/combobox in the cells of my table wich is yet
created (jTable1).

I must use a table (not panels) because the number o questions could
be high and so a table into a scrollbar can contain any number of
questions/answers.

If you have some other time to spend for me, please consider what I
said here and try to suggest some code to insert checkboxes or
combobox in some cell of my yet created table. Thanks from Franco


frafel said:
..
Yes I means JTable!

Thanks for clearing that up.
Any way my job is the following;
In a two columns table I will insert rows where each coople of columns
are respectively a question and a answer; it is possible that the
answer in some cases must be a string, in some other case the answer
must be selected from the items of a combobox, in other cases the
answer must be a true/false selection.

Here is a 'tabular' arrangement of questions and answers.

<sscce>
import java.awt.*;
import javax.swing.*;

class QuestionTable {

public static Component getAnswerComponent(Object value) {
if (value instanceof String) {
return new JTextField((String)value, 20);
} else if (value instanceof Boolean) {
return new JCheckBox(
"",
((Boolean) value).booleanValue());
} else {
Integer[] values = (Integer[])value;
return new JComboBox(values);
}
}

public static void main(String[] args)
{
final Object[][] data =
{
{
"What is your name?",
""
},
{
"What is the meaning of life?",
new Integer[]
{
new Integer(21),
new Integer(42),
new Integer(63)
},
},
{
"Is this a table?",
new Boolean(false)
}
};

Runnable r = new Runnable() {
public void run() {
JPanel mainPanel =
new JPanel( new BorderLayout(5,5) );
JPanel questionPanel =
new JPanel( new GridLayout(0,1,5,5) );
JPanel answerPanel =
new JPanel( new GridLayout(0,1,5,5) );
mainPanel.add(
questionPanel,
BorderLayout.CENTER );
mainPanel.add(
answerPanel,
BorderLayout.EAST );

for (int ii=0; ii<data.length; ii++) {
questionPanel.add(
new JLabel((String)data[ii][0]) );
answerPanel.add(
getAnswerComponent( data[ii][1] ) );
}

JOptionPane.showMessageDialog(
null,
mainPanel,
"Questionnare",
JOptionPane.QUESTION_MESSAGE);
}
};
EventQueue.invokeLater(r);
}}

</sscce>
 
A

Andrew Thompson

frafel said:
Thanks Andrew (and also Daniele.

Your future *lack* of top-posting, will be thanks enough.
I must use a table (not panels) because the number o questions could
be high and so a table into a scrollbar can contain any number of
questions/answers.

That is completely irrelevant. A JPanel can be put into a JScrollPane
(at the bottom of.. along with.. blah, blah..) just as easily as a JTable can.


--
Andrew Thompson
http://www.physci.org/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200712/1
 
F

frafel

That is completely irrelevant. A JPanel can be put into a JScrollPane
(at the bottom of.. along with.. blah, blah..) just as easily as a JTable can.

--

It is difficult for me to explain wy I need ABSOLUTELY a jTable, so my
need remain to insert components IN A CELL OF A JTABLE. And I was yet
able to do this; but effectively I was able to insert the same
combobox or checkbox in ALL the cells of a column, nor different
components in different cells of the same column.

As you can see in my first message I used the statement:

tcm.getColumn(1).setCellEditor(editor)

but in this way ALL the column appears with the same component.

To use different editors in different cells I need to use a statemente
like:

tcm.getCell(i, j).SetCellEditor(editor) -->(but as you know this
method does not exists).

So please let me know if this is possible in such other way.

ONLY if this is IMPOSSIBLE TO REALIZE I will consider to change the
entire architecture of my program to use couples of panels in a
scrollbox instead of my table.

I thamks you for the help and patience, but please ask me to use a
different architecture ONLY for LACK of possibility of using the cells
of my table.

Greetings. Franco.
 
A

Andrew Thompson

frafel said:
...
It is difficult for me to explain wy I need ABSOLUTELY a jTable,

- If you can, it might raise my motivation to help find a solution.
- Please refrain form SHOUTING at us. If there is something
you need to stress, put it in *bold* or /italic/.
- It is *JTable* - please try to remember that! It is hard enough
solving technical problems on a mult-national forum without
people being lazy/sloppy about the proper nomenclature and
class names.

...
I thamks you for the help and patience, but please ask me to use a
different architecture ONLY for LACK of possibility of using the cells
of my table.

(shrugs) I don't know. I played with a JTable example for
about 20 minutes looking for the effect you were after, and
ran into exactly the same problems you did.

At that point, I decided to abandon trying to 'beat JTable
into submission'* and investigate other means to get the
layout.

* Which is often a hint that you are using the wrong
strategy, or GUI component, for the job.
 
D

Daniele Futtorovic

- If you can, it might raise my motivation to help find a solution.
- Please refrain form SHOUTING at us. If there is something
you need to stress, put it in *bold* or /italic/.

CAPS /is/ a way of stressing. It's not like they wrote a whole sentence
allcaps.
- It is *JTable* - please try to remember that! It is hard enough
solving technical problems on a mult-national forum without
people being lazy/sloppy about the proper nomenclature and
class names.
True.


(shrugs) I don't know. I played with a JTable example for
about 20 minutes looking for the effect you were after, and
ran into exactly the same problems you did.

At that point, I decided to abandon trying to 'beat JTable
into submission'* and investigate other means to get the
layout.

* Which is often a hint that you are using the wrong
strategy, or GUI component, for the job.

I would say that using a custom component, like you, Andrew, suggest, or
'beating JTable into submission' should amount to roughly the same level
of difficulty.

If they use JTable, the OP *will* have to write a custom renderer and
(most probably) a custom editor.
If they use a custom component, the OP will have to implement some of
the functionality JTable would have handled, like scrolling, themselves.

COnsequently, while the difficulty would be distributed differently, I'd
say it would amount to the same level. I wouldn't discourage from using
JTable from the get go, especially since the OP is obviously still left
with a lot to learn about GUI developement.
I think a combination of proper javax.swing.table.TableModel,
javax.swing.table.TableCellRenderer and
javax.swing.table.TableCellEditor would be an appropriate way to fulfill
the task at hand (regardless of the fact that I personally MAY prefer
using a custom component).

Here is, once more, a link for the tutorials about JTable:
<http://java.sun.com/docs/books/tutorial/uiswing/components/table.html>

df.
 
R

Roger Lindsjö

frafel said:
I need to insert in the cells of a particular jTable column, different
components; a good example could be:

In a table with two columns and three rows, referring to the second
column, I would like to put in the first cell (0, 1) a check box; in
the second cell (1, 1) a combobox with the items "one", "two", "three"
and I will left the third cell (2, 1) ready to accept a string from
the user (means that here I don't need to insert any one component).

Something like this? Note, I have only implemented the renderer part, so
if you try to edit any field it will be treated as text from then on.
Also, I din't find a nice way to reuse the combo box, perhaps by
implementing a model for it?

<sscce>
import java.awt.Component;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableCellRenderer;

public class TJableTest {

public static void main(String... args) {
JTable table = new JTable(
new Object[][] {{true}, {false},
{new Object[] {"one", "two"}},
{"text field"}},
new Object[] {"Header"});

table.setDefaultRenderer(Object.class, new MyTableCellRenderer());

JFrame frame = new JFrame("JTable");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(table);
frame.pack();
frame.setVisible(true);
}

public static class MyTableCellRenderer extends
DefaultTableCellRenderer {

private JCheckBox check = new JCheckBox();
private JTextField text = new JTextField();
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
if ( value instanceof Boolean ) {
check.setSelected((Boolean) value);
return check;
} else if (value instanceof Object[]){
JComboBox combo = new JComboBox((Object[]) value);
return combo;
} else {
text.setText(value.toString());
return text;
}
}
}
}
</sscce>

//Roger Lindsjö
 
F

frafel

CAPS /is/ a way of stressing. It's not like they wrote a whole sentence
allcaps.

Yes I will try this way. I am not well informed about the use of the
forum, so this kind of suggestions are appreciated. If you can suggest
a good link where I can find a tutorial on using usenet in a good way,
please let me now it.

.........

I would say that using a custom component, like you, Andrew, suggest, or
'beating JTable into submission' should amount to roughly the same level
of difficulty.

Ok. At this point I will try to find an alternative to JTable and
I will start following what yet suggest me Andrew.
If they use a custom component, the OP will have to implement some of
the functionality JTable would have handled, like scrolling, themselves.

In fact I know that in this way I will need to solve other problems.


Now I will need sure a lot of days to work on my program. Sure I will
use
all the suggestions received from the group and I ask only:

'Can I replay again to someone of the group still after i.e. ten days
from now to talk again about this problem?'

or after a lot of time it's necessary to create a new post with the
same
argument?

Finally, please excuse me about my english, I never studied it so I am
sure that you will found in it a lot of mistakes.

Also excuse me if I ask sometime obviously thinks, but I am using
Java
about a year only. Any way I am a very old programmer (I start my
programming life in the year 1959 on the IBM mainframes, and I am 72
years old!)


Thanks. Franco
 
L

Lew

frafel said:
Yes I will try this way. I am not well informed about the use of the
forum, so this kind of suggestions are appreciated. If you can suggest
a good link where I can find a tutorial on using usenet in a good way,
please let me now it.

In this very forum, every five days, David Alex Lamb posts,
"comp.lang.java.{help,programmer} - what they're for (mini-FAQ 2006-03-31)",
with information about the newsgroup and links to information on how to use it.

One suggestion is to spend time reading the newsgroup. Doing that, you
discover things like that post, and observe how people interact.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top