The Artima Developer Community
Sponsored Link

Java Answers Forum
How can I set the rows in a JTable

1 reply on 1 page. Most recent reply: Jan 8, 2004 6:37 AM by mausam

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
loftty

Posts: 1
Nickname: lol
Registered: Jan, 2004

How can I set the rows in a JTable Posted: Jan 6, 2004 11:05 AM
Reply to this message Reply
Advertisement
Hello All,

I am having trouble setting the rows in my JTable.

I want the user to be able to set how many rows there are in the table. ie.
When the use types in 30 in the text field I want the JTable rows to adjust and have 30 rows going downwards.

Could anyone please help me?
please look at my code in that I have so far.
Thanks
Lol


import javax.swing.*;
import javax.swing.table.TableModel;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.*;
import javax.swing.table.DefaultTableModel;

public class si1 extends javax.swing.JFrame implements ActionListener {
JTextField name = new JTextField(15);
JTextField name1 = new JTextField(15);
public si1() {
super("DataBase Loader");
setSize(1025,740);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
JPanel pane1 = new JPanel();
JPanel pane2 = new JPanel();
JPanel pane3 = new JPanel();
JPanel pane4 = new JPanel();
pane.setLayout(new GridLayout(20,1));
pane1.setLayout(new BorderLayout());
int j=10;
String[][] data = new String[j][2];
for (int k=0; k<j; k++){
String[] row = {"",""};
data[k] = row;
}
String[] columnNames = {"First Name", "Last Name"};
final JTable perstab = new JTable();//(data, columnNames);
DefaultTableModel model2 = new DefaultTableModel(data, columnNames);
perstab.setModel(model2);
perstab.setGridColor(Color.yellow);
perstab.setPreferredScrollableViewportSize(new Dimension(500,500));
JScrollPane scrollPane = new JScrollPane(perstab);
pane1.add(new JPanel(), BorderLayout.EAST);
JButton btn = new JButton("What are the names?");
btn.addActionListener(this);
btn.putClientProperty("DATABASE", perstab);
pane1.add(new JPanel().add(btn), BorderLayout.SOUTH);
pane2.add(name);
pane3.add(name1);
pane.add(pane2);
pane.add(pane3);
pane1.add(pane, BorderLayout.WEST);
pane4.add(scrollPane);
pane1.add(pane4, BorderLayout.CENTER);
setContentPane(pane1);
name1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(perstab.getModel().getClass());
((DefaultTableModel)perstab.getModel()).setColumnCount(Integer.parseInt(name1.g etText()));
}
}
);


show();
}
public static void main(String[] args) {
si1 frame = new si1();
frame.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
JTable table = (JTable)((JButton)e.getSource()).getClientProperty("DATABASE");
TableModel model = table.getModel();
int count = model.getRowCount();
String[] firstnames = new String[count];
String[] lastnames = new String[count];
for (int i=0; i < count; i++) {
firstnames[i] = (String)model.getValueAt(i, 0);
System.out.println("first name at row " + i + ": " + firstnames[i]);
lastnames[i] = (String)model.getValueAt(i, 1);
System.out.println("lastname name at row " + i + ": " + lastnames[i]);
}
}
}


mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: How can I set the rows in a JTable Posted: Jan 8, 2004 6:37 AM
Reply to this message Reply
Say User entered 1(read userRowNumber), u want row one of table to be selected.

Use this code.

//check userRowNumber exist and then select row as.
if(userRowNumber>table.getRowCount())
	userRowNumber=table.getRowCount();
 
table.addRowSelectionInterval(userRowNumber-1,userRowNumber-1);// as first row in jtable is read as 0 in java
 

Flat View: This topic has 1 reply on 1 page
Topic: String manipulations again Previous Topic   Next Topic Topic: Please Help - Array of objects ( classes)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use