• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTable not displayed when mouse clicked

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
I have one JPanel containing 5 JTables, and then I have another JPanel
containing one JTable. If I select a certain cell in MainTable.java,
I want to display one of the 5 JTables of TestTable.java. I tried it,
but nothing's being displayed. Here's the code. It compiles and runs, but when I run MainTable.java, and I click the particular cell, nothing happens. Please have a look if anyone has the time.
Thanx a mil
C

TestTable.java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;

public class TestTable extends JFrame{
JTable[] table = new JTable[5];
TableModel[] model = new TableModel[5];
JPanel pane = new JPanel();
public TestTable(){
super();
for(int k=0;k<5;k++){
model[k] = new AbstractTableModel(){
public int getColumnCount () {
return 5;
}
public int getRowCount () {
return 5;
}
public Object getValueAt (int row, int col) {
return new Integer (row * col);
}
};
table[k] = new JTable (model[k]);
table[k].setColumnSelectionAllowed(true);
table[k].setRowSelectionAllowed(true);
pane.add(table[k]);
}
setContentPane(pane);
}
public static void main(String[] args){
TestTable f = new TestTable();
f.pack();
f.setBounds(new Rectangle(20,20,500,500));
f.setVisible(true);
}}
//End of TestTable.java
MainTable.java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;

public class MainTable extends JFrame implements MouseListener{
JTable maintable = new JTable(20,5);
JPanel panel = new JPanel();
TestTable test = new TestTable();
public MainTable(){
super();
maintable.setPreferredScrollableViewportSize(new Dimension (300,300));
maintable.setRowSelectionAllowed(true);
maintable.setColumnSelectionAllowed(true);
maintable.addMouseListener(this);
panel.add(maintable);
setContentPane(panel);
}
public void mouseClicked(MouseEvent me){
int row = maintable.rowAtPoint(me.getPoint());
int col = maintable.columnAtPoint(me.getPoint());
//if we select the second cell in the first column
//I want to display the second jtable of TestTable.java
if(row==1&&col==0){
test.table[1].setVisible(true);
}
}
public void mousePressed(MouseEvent me){}
public void mouseReleased(MouseEvent me){}
public void mouseEntered(MouseEvent me){}
public void mouseExited(MouseEvent me){}
public static void main(String[] args){
MainTable mt = new MainTable();
mt.pack();
mt.setBounds(new Rectangle(40,40,400,400));
mt.setVisible(true);
}}
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to the Swing forum.
 
Candi Smith
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Candi Smith:
Hi all
I have one JPanel containing 5 JTables, and then I have another JPanel
containing one JTable. If I select a certain cell in MainTable.java,
I want to display one of the 5 JTables of TestTable.java. I tried it,
but nothing's being displayed. Here's the code. It compiles and runs, but when I run MainTable.java, and I click the particular cell, nothing happens. Please have a look if anyone has the time.
Thanx a mil
C

TestTable.java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;

public class TestTable extends JFrame{
JTable[] table = new JTable[5];
TableModel[] model = new TableModel[5];
JPanel pane = new JPanel();
public TestTable(){
super();
for(int k=0;k<5;k++){
model[k] = new AbstractTableModel(){
public int getColumnCount () {
return 5;
}
public int getRowCount () {
return 5;
}
public Object getValueAt (int row, int col) {
return new Integer (row * col);
}
};
table[k] = new JTable (model[k]);
table[k].setColumnSelectionAllowed(true);
table[k].setRowSelectionAllowed(true);
pane.add(table[k]);
}
setContentPane(pane);
}
public static void main(String[] args){
TestTable f = new TestTable();
f.pack();
f.setBounds(new Rectangle(20,20,500,500));
f.setVisible(true);
}}
//End of TestTable.java
MainTable.java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;

public class MainTable extends JFrame implements MouseListener{
JTable maintable = new JTable(20,5);
JPanel panel = new JPanel();
TestTable test = new TestTable();
public MainTable(){
super();
maintable.setPreferredScrollableViewportSize(new Dimension (300,300));
maintable.setRowSelectionAllowed(true);
maintable.setColumnSelectionAllowed(true);
maintable.addMouseListener(this);
panel.add(maintable);
setContentPane(panel);
}
public void mouseClicked(MouseEvent me){
int row = maintable.rowAtPoint(me.getPoint());
int col = maintable.columnAtPoint(me.getPoint());
//if we select the second cell in the first column
//I want to display the second jtable of TestTable.java
if(row==1&&col==0){
test.table[1].setVisible(true);
}
}
public void mousePressed(MouseEvent me){}
public void mouseReleased(MouseEvent me){}
public void mouseEntered(MouseEvent me){}
public void mouseExited(MouseEvent me){}
public static void main(String[] args){
MainTable mt = new MainTable();
mt.pack();
mt.setBounds(new Rectangle(40,40,400,400));
mt.setVisible(true);
}}

 
reply
    Bookmark Topic Watch Topic
  • New Topic