can't get jlist to view in table

HI Everyone,

Im a newbie at java swing and I was attempting to put a jlist in a column however it is just showing me the position. Can anyone help on this, its fairly simple probably. I just been looking at this all day however, lol...

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

import javax.swing.event.*;

import javax.swing.table.*;

public class TableRowColumn extends JFrame

{

private final static String LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

JTable table;

DefaultTableModel model;

JPanel buttonPanel;

JButton button;

Vector tmp=new Vector();

JList some=new JList();

public TableRowColumn()

{

// Create table

tmp=new Vector();

String[] sdata2={"stock1","stock2"};

//tmp.addElement(sdata2);

some=new JList(sdata2);

Object[][] data = { {"1",some }, {"2", "C"}, {"3", "D"} };

String[] columnNames = {"Number","Letter"};

model = new DefaultTableModel(data, columnNames);

table = new JTable(model);

table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

// Add table and a Button panel to the frame

JScrollPane scrollPane = new JScrollPane( table );

getContentPane().add( scrollPane );

buttonPanel = new JPanel();

getContentPane().add( buttonPanel, BorderLayout.SOUTH );

//

button = new JButton( "Add Row" );

buttonPanel.add( button );

button.addActionListener( new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

model.addRow( createRow() );

int row = table.getRowCount() - 1;

table.changeSelection(row, 0, false, false);

table.requestFocusInWindow();

}

});

//

button = new JButton( "Insert Row" );

buttonPanel.add( button );

button.addActionListener( new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

model.insertRow( 0, createRow() );

table.changeSelection(0, 0, false, false);

table.requestFocusInWindow();

}

});

//

button = new JButton( "Empty Row" );

buttonPanel.add( button );

button.addActionListener( new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

model.setRowCount( model.getRowCount() + 1 );

int row = table.getRowCount() - 1;

table.changeSelection(row, 0, false, false);

table.requestFocusInWindow();

}

});

//

button = new JButton( "Add Column" );

buttonPanel.add( button );

button.addActionListener( new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String header = "Col" + (table.getColumnCount() + 1);

model.addColumn( header );

table.requestFocusInWindow();

}

});

//

button = new JButton( "Add Column & Data" );

buttonPanel.add( button );

button.addActionListener( new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String header = "Col" + (table.getColumnCount() + 1);

int rows = table.getRowCount();

String[] values = new String[rows];

for (int j = 0; j < rows; j++)

{

values[j] = Integer.toString(j);

}

model.addColumn( header, values );

table.requestFocusInWindow();

}

});

//

button = new JButton( "Add Column - No Reordering" );

buttonPanel.add( button );

button.addActionListener( new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

// Use this method when you don't want existing columns

// to be rebuilt from the model.

// (ie. moved columns will not be reordered)

table.setAutoCreateColumnsFromModel( false );

String header = "Col" + (table.getColumnCount() + 1);

model.addColumn( header );

// AutoCreate is turned off so create table column here

TableColumn column = new TableColumn( table.getColumnCount() );

column.setHeaderValue( header );

table.addColumn( column );

// These won't work once setAutoCreate... has been set to false

buttonPanel.getComponent(3).setEnabled( false );

buttonPanel.getComponent(4).setEnabled( false );

table.requestFocusInWindow();

}

});

//

button = new JButton( "Remove Last Column" );

buttonPanel.add( button );

button.addActionListener( new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

int columns = model.getColumnCount();

if (columns > 0)

{

if (!table.getAutoCreateColumnsFromModel())

{

int view =

table.convertColumnIndexToView(columns - 1);

TableColumn column =

table.getColumnModel().getColumn(view);

table.getColumnModel().removeColumn( column );

}

model.setColumnCount( columns - 1 );

}

table.requestFocusInWindow();

}

});

}

private Object[] createRow()

{

Object[] newRow = new Object[2];

int row = table.getRowCount() + 1;

newRow[0] = Integer.toString( row );

newRow[1] = LETTERS.substring(row-1, row);

return newRow;

}

public static void main(String[] args)

{

TableRowColumn frame = new TableRowColumn();

frame.setDefaultCloseOperation( EXIT_ON_CLOSE );

frame.pack();

frame.setLocationRelativeTo( null );

frame.setVisible(true);

}

}

[5591 byte] By [h2opologirly69a] at [2007-11-27 5:53:24]
# 1

Use [url http://forum.java.sun.com/help.jspa?sec=formatting]code formatting[/url] when posting code.

The default table renderer uses the toString functrion to display the Object in the cell.

If you want to display a list inside a cell you will have to write your own renderer.

Read in the tutorial Concepts: Editors and Renderers

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#editrender

Rodney_McKaya at 2007-7-12 15:46:22 > top of Java-index,Desktop,Core GUI APIs...
# 2

Still haveing trouble should I use ListCellRenderer?

class mycelrenderer extends JBoxList implements ListCellRenderer{

public mycellrenderer()

{

//put something here?

}

public Component getListCellRendererComponent(JList list)

{

//What I put here?

}

}

h2opologirly69a at 2007-7-12 15:46:22 > top of Java-index,Desktop,Core GUI APIs...
# 3
you want a renderer for a table, use a TableCellRenderer
bsampieria at 2007-7-12 15:46:22 > top of Java-index,Desktop,Core GUI APIs...
# 4

like this?class blahRenderer implements TableCellRenderer {

public blahRenderer() {

//what in here?

}

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,

boolean hasFocus, int row, int column) {

//in here put what?

}

}

h2opologirly69a at 2007-7-12 15:46:22 > top of Java-index,Desktop,Core GUI APIs...
# 5

Yes, but you forgot to extend JList or JBoxList (whatever that is).

You would return this from the getTableCellRendererComponent method.

Of course, you would have to initialize the JList to whatever.

You also need to keep in mind that a cell renderer is just that: a renderer. It's like taking a screenshot of the component and drawing it into the table cell. It doesn't put an actual list component in the cell. So you aren't going to be able to just click on list items. You'd have to swap for a cell editor that is the same list to do that. Or do some fancy mouse event handling tricks.

It's a royal pain, but you might want to rethink doing it.

bsampieria at 2007-7-12 15:46:22 > top of Java-index,Desktop,Core GUI APIs...
# 6
okay illl abandon that... how about a column of checkbuttons?
h2opologirly69a at 2007-7-12 15:46:22 > top of Java-index,Desktop,Core GUI APIs...
# 7

Same thing. You need a cell renderer, probably a JPanel that contains the checkboxes or whatever. But again, you run into the same issue: It's just a picture of checkboxes, not actual checkboxes.

It's not impossible to do, nor to deal with making them clickable, one way or another. But neither is it very simple.

I'm sure there's plenty of examples around the forums, if you look around.

Message was edited by:

bsampieri

bsampieria at 2007-7-12 15:46:22 > top of Java-index,Desktop,Core GUI APIs...
# 8
> how about a column of checkbuttons? The tutorial link you where given above has a working example. So how about reading the tutorial. There already is a default renderer and editor for a Boolean object, which is displayed as a check box in the table.
camickra at 2007-7-12 15:46:22 > top of Java-index,Desktop,Core GUI APIs...