473,659 Members | 2,683 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How event listener can be user with rendered combobox

151 New Member
Hi,

How Eventlistner can be used with rendred combo box. I got one example of combobox in table as follows .

/*
* Copyright (c) 1995 - 2008 Sun Microsystems, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Sun Microsystems nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package components;

/*
* TableRenderDemo .java requires no other files.
*/

import javax.swing.Def aultCellEditor;
import javax.swing.JCo mboBox;
import javax.swing.JFr ame;
import javax.swing.JPa nel;
import javax.swing.JSc rollPane;
import javax.swing.JTa ble;
import javax.swing.tab le.AbstractTabl eModel;
import javax.swing.tab le.DefaultTable CellRenderer;
import javax.swing.tab le.TableCellRen derer;
import javax.swing.tab le.TableColumn;
import java.awt.Compon ent;
import java.awt.Dimens ion;
import java.awt.GridLa yout;

/**
* TableRenderDemo is just like TableDemo, except that it
* explicitly initializes column sizes and it uses a combo box
* as an editor for the Sport column.
*/
public class TableRenderDemo extends JPanel {
private boolean DEBUG = false;

public TableRenderDemo () {
super(new GridLayout(1,0) );

JTable table = new JTable(new MyTableModel()) ;
table.setPrefer redScrollableVi ewportSize(new Dimension(500, 70));
table.setFillsV iewportHeight(t rue);

//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(tab le);

//Set up column sizes.
initColumnSizes (table);

//Fiddle with the Sport column's cell editors/renderers.
setUpSportColum n(table, table.getColumn Model().getColu mn(2));

//Add the scroll pane to this panel.
add(scrollPane) ;
}

/*
* This method picks good column sizes.
* If all column heads are wider than the column's cells'
* contents, then you can just use column.sizeWidt hToFit().
*/
private void initColumnSizes (JTable table) {
MyTableModel model = (MyTableModel)t able.getModel() ;
TableColumn column = null;
Component comp = null;
int headerWidth = 0;
int cellWidth = 0;
Object[] longValues = model.longValue s;
TableCellRender er headerRenderer =
table.getTableH eader().getDefa ultRenderer();

for (int i = 0; i < 5; i++) {
column = table.getColumn Model().getColu mn(i);

comp = headerRenderer. getTableCellRen dererComponent(
null, column.getHeade rValue(),
false, false, 0, 0);
headerWidth = comp.getPreferr edSize().width;

comp = table.getDefaul tRenderer(model .getColumnClass (i)).
getTableCellRen dererComponent(
table, longValues[i],
false, false, 0, i);
cellWidth = comp.getPreferr edSize().width;

if (DEBUG) {
System.out.prin tln("Initializi ng width of column "
+ i + ". "
+ "headerWidt h = " + headerWidth
+ "; cellWidth = " + cellWidth);
}

column.setPrefe rredWidth(Math. max(headerWidth , cellWidth));
}
}

public void setUpSportColum n(JTable table,
TableColumn sportColumn) {
//Set up the editor for the sport cells.
JComboBox comboBox = new JComboBox();
comboBox.addIte m("Snowboarding ");
comboBox.addIte m("Rowing");
comboBox.addIte m("Knitting") ;
comboBox.addIte m("Speed reading");
comboBox.addIte m("Pool");
comboBox.addIte m("None of the above");
sportColumn.set CellEditor(new DefaultCellEdit or(comboBox));

//Set up tool tips for the sport cells.
DefaultTableCel lRenderer renderer =
new DefaultTableCel lRenderer();
renderer.setToo lTipText("Click for combo box");
sportColumn.set CellRenderer(re nderer);
}

class MyTableModel extends AbstractTableMo del {
private String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian "};
private Object[][] data = {
{"Mary", "Campione",
"Snowboardi ng", new Integer(5), new Boolean(false)} ,
{"Alison", "Huml",
"Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath",
"Knitting", new Integer(2), new Boolean(false)} ,
{"Sharon", "Zakhour",
"Speed reading", new Integer(20), new Boolean(true)},
{"Philip", "Milne",
"Pool", new Integer(10), new Boolean(false)}
};

public final Object[] longValues = {"Sharon", "Campione",
"None of the above",
new Integer(20), Boolean.TRUE};

public int getColumnCount( ) {
return columnNames.len gth;
}

public int getRowCount() {
return data.length;
}

public String getColumnName(i nt col) {
return columnNames[col];
}

public Object getValueAt(int row, int col) {
return data[row][col];
}

/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class getColumnClass( int c) {
return getValueAt(0, c).getClass();
}

/*
* Don't need to implement this method unless your table's
* editable.
*/
public boolean isCellEditable( int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col < 2) {
return false;
} else {
return true;
}
}

/*
* Don't need to implement this method unless your table's
* data can change.
*/
public void setValueAt(Obje ct value, int row, int col) {
if (DEBUG) {
System.out.prin tln("Setting value at " + row + "," + col
+ " to " + value
+ " (an instance of "
+ value.getClass( ) + ")");
}

data[row][col] = value;
fireTableCellUp dated(row, col);

if (DEBUG) {
System.out.prin tln("New value of data:");
printDebugData( );
}
}

private void printDebugData( ) {
int numRows = getRowCount();
int numCols = getColumnCount( );

for (int i=0; i < numRows; i++) {
System.out.prin t(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.prin t(" " + data[i][j]);
}
System.out.prin tln();
}
System.out.prin tln("--------------------------");
}
}

/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGU I() {
//Create and set up the window.
JFrame frame = new JFrame("TableRe nderDemo");
frame.setDefaul tCloseOperation (JFrame.EXIT_ON _CLOSE);

//Create and set up the content pane.
TableRenderDemo newContentPane = new TableRenderDemo ();
newContentPane. setOpaque(true) ; //content panes must be opaque
frame.setConten tPane(newConten tPane);

//Display the window.
frame.pack();
frame.setVisibl e(true);
}

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.Swi ngUtilities.inv okeLater(new Runnable() {
public void run() {
createAndShowGU I();
}
});
}
}


I dont know how to use the combbox individually in each cell to get the value of selected item for slected combox in table. Any example will help me a lot.


Thanks in advance.
Dec 24 '09 #1
1 3536
Man4ish
151 New Member
How to get the information about particular item selected for selected checkbox when used in jTable.
Dec 26 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
20772
by: Jeff Thies | last post by:
I have this IE specific bit of code for finding the originating node: var obj=window.event.srcElement; How do I do that cross browser (Opera, NS, Safari...)? Is there a standard DOM method? I seem to recall NS worked something like this: onmousedown=handleThat;
4
1774
by: Newbie | last post by:
Is it possible to set up an event handler or something else so that when *any* link on the page is clicked it 'fires-up', executes some JS and then continues to process the link that was clicked? (Without having JS or 'onClick' added to each & every link?) I've looked everywhere but can't find out how, or if it's even possible via Javascript... Regards.
17
4870
by: abs | last post by:
My element: <span onclick="alert('test')" id="mySpan">test</span> Let's say that I don't know what is in this span's onclick event. Is it possible to add another action to this element's onclick event ? I've tried something like this: oncl = document.getElementById('mySpan').onclick oncl = oncl + '\n;alert(\'added\')' document.getElementById('mySpan').onclick = oncl
0
2946
by: Demetri | last post by:
I have created a web control that can be rendered as either a linkbutton or a button. It is a ConfirmButton control that allows a developer to force a user to confirm if they intended to click it such as when they do a delete. Everything is great. By and large it will be used in my repeater controls using the command event when the user clicks on it and so that event is working great. My issue is the Click event. When the control is...
6
9154
by: Steve Teeples | last post by:
I have been perplexed by how to best treat an event that spans different classes. For example, I have a form which a user inputs data. I want to broadcast that data via an event to another class (seen globally) having a data structure which saves that form data to disk. Whenever the form updates the data I'd like to broadcast the information and have it saved in my global data structure. The perplexing thing for me though is the...
0
2070
by: Kamilche | last post by:
''' event.py An event manager using publish/subscribe, and weakrefs. Any function can publish any event without registering it first, and any object can register interest in any event, even if it doesn't exist yet. The event manager uses weakrefs, so lists of listeners won't stop them
10
3264
by: fusillo | last post by:
Hi, i've tried a test code about the Event Listener API but i've some problem about detaching the element firing the event with IE. Here's my code: <html> <head> <style type="text/css"> ..cliccabile {background-color: #FFCCCC; cursor:pointer;} ..testo {color: #009966;} </style>
6
19256
by: Daz | last post by:
Hello everyone, I would like to open a child window from the parent, and add an onload event listener to the child window which will tell the parent when the document has loaded. As far as I know, this shouldn't be an issue, but I just can't get it to work. The script only needs to work with Firefox/Mozilla, so XP code isn't an issue. I have tried to open a window like so.
6
10809
by: tbrown | last post by:
I have a combobox with items like this: {one,two,three}. The selected index is 0, so "one" appears in the combobox text. When the user drops down the list, and selects "two", for example, I modify the Items collection to be {two,one,three} and now want "two" to appear in the combobox text. However, the combobox text is now blank. the is apparently somehow the result of having changed the combobox.Items collection. If, trying to fix...
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8332
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7356
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.