• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Using JTables with Radio Buttons

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I ve used a Jtable to display the contents of the DB . Is It possible for me to give radio buttons for all the rows in JTable?

I ve used a button to display the JTable. I ve given my code below for reference

P.S. This s d first time am using Jtable so kindly excuse if i had not used JTables properly..


Thanks in Advance
Baradh

tablebtn.addActionListener(new ActionListener()
{

@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent ae)
{


Vector<Vector><Object>> data = new Vector<Vector><Object>>();
Connection con = null;
String url = "jdbc:mysql://192.168.1.63:3306/******";
String db = "******";
String driver = "com.mysql.jdbc.Driver";
String user = "****";
String pass = "*****";


try
{

Class.forName(driver);
con = DriverManager.getConnection(url, user, pass);
String sql ="SELECT Slno, FIRSTNAME , LASTNAME ,CITY,STATE,COUNTRY ROM addressbook WHERE Status='ACTIVE'";

PreparedStatement st = con.prepareStatement(sql);
ResultSet rs = st.executeQuery(sql);
java.sql.ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
System.out.println(sql);
Vector<String> columnNames = new Vector<String>();
p5.removeAll();
for (int i = 1; i <= columns; i++)
{
columnNames.addElement( d.getColumnName(i));

}
System.out.println(columnNames);


while (rs.next())
{
Vector<Object> row = new Vector<Object>(columns);

for (int j = 1; j <= columns; j++)
{
row.addElement(rs.getObject(j));
}

data.addElement(row);
System.out.println(row);
}

rs.close();
System.out.println("am still here2");
p5.setVisible(false);
JTable table = new JTable(data, columnNames);
JScrollPane scroll = new JScrollPane (table,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setVerticalScrollBar(new JScrollBar());
scroll.setHorizontalScrollBar(new JScrollBar());
table.setAutoResizeMode (JTable.AUTO_RESIZE_OFF);

p5.add(table);
p5.add(scroll);
p5.setVisible(true);
table.show(true);
table.revalidate();
//table.repaint();
table.enable(true);
System.out.println("after table");

}

catch(ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
System.out.println("Exception" +cnfe);

}

catch(SQLException sqle)
{
sqle.printStackTrace();
System.out.println("Exception" +sqle);
}
}


});
 
Sheriff
Posts: 22818
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A few hints:

- Use Code Tags. You can edit your post to add them

- don't suppress deprecation warnings, instead look at the API for the replacements. In this case, table.show(true) should be replaced by table.setVisible(true) and table.enable(true) should be replaced by table.setEnabled(true)

- I take it that "d" is an existing table model. Don't use its names, use the names from "md" instead:
This will also prevent a possible error; TableModel indexes start at 0 whereas JDBC starts at 1, as does your loop.

- don't create new JScrollBars for the JScrollPane, it will create them itself already. You then throw these auto-generated scroll bars away to replace them with another set of newly created scroll bars.

- don't recreate the JTable and JScrollPane. Instead, create a new DefaultTableModel and set that for the existing table which then becomes a field:


Now to the original question. Do you want a radio button at the start of each row that allows you to select only one row? That will require a bit of work involving cell renderers and cell editors, and there already is a selection mechanism for JTable. First, specify to only allow one row to be selected:
Then you can click on the entire JTable and it will change the selection. You can then retrieve that selection as follows:
Setting the selection is a bit harder but not very much:
And there you have it; single row selection straight out of the box.
 
I don't always make ads but when I do they're tiny
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic