|
-
Dec 29th, 2001, 05:54 AM
#1
Thread Starter
Lively Member
repaint JTable
Hi,
I have a new problem.
I can draw a JTable and a JButton in a window.
The table has 5 columns and 5 rows.
This is what I want to do.
When the button is clicked, the table should have 4 columns and 4 rows.
Who can help me with this??
Thanks for reading my problem.
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class GridBagWindow extends JFrame {
boolean inAnApplet = true;
final boolean shouldFill = true;
final boolean shouldWeightX = true;
JTable table;
public GridBagWindow() {
// new Data
JButton NewDataButton = new JButton("This button should change the layout of the Table");
NewDataButton.setMnemonic(KeyEvent.VK_I);
NewDataButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//************************//
//THIS DOES NOT WORK !!!!
// WHO CAN FIX THIS ?????
//************************//
table = new JTable(4, 4);
table.repaint();
}
});
table = new JTable(5, 5);
table.setRowSelectionAllowed(true);
table.setPreferredScrollableViewportSize(new Dimension(800, 200));
Container contentPane = getContentPane();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
contentPane.setLayout(gridbag);
if (shouldFill) {
c.fill = GridBagConstraints.HORIZONTAL;
}
if (shouldWeightX) {
c.weightx = 0.5;
}
c.gridx = 0;
c.gridy = 0;
gridbag.setConstraints(NewDataButton, c);
contentPane.add(NewDataButton);
c.gridx = 0;
c.gridy = 1;
gridbag.setConstraints(table, c);
contentPane.add(table);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this window.
gridbag.setConstraints(scrollPane, c);
getContentPane().add(scrollPane);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (inAnApplet) {
dispose();
} else {
System.exit(0);
}
}
});
}
public static void main(String args[]) {
GridBagWindow window = new GridBagWindow();
window.inAnApplet = false;
window.setTitle("Change the Table layout ...");
window.pack();
window.setVisible(true);
}
}
-
Dec 29th, 2001, 11:52 AM
#2
The reason your code doesn't work is that all your code is doing is changing where your table variable is pointing. In Swing, when you remove the pointer to a drawn object, tha object still paints itself, you just no longer have control over it. Try this instead...
Code:
NewDataButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
contentPane.remove(table);
table = new JTable(4, 4);
contentPane.add(table);
table.repaint();
}
});
It may not place it where you want it, but you can work that out.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Dec 29th, 2001, 01:13 PM
#3
Thread Starter
Lively Member
Sorry, it does not work ...
Who can make this work ???
Thanks for reading my problem.
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class GridBagWindow extends JFrame {
boolean inAnApplet = true;
final boolean shouldFill = true;
final boolean shouldWeightX = true;
JTable table;
Container contentPane;
GridBagLayout gridbag;
GridBagConstraints c;
public GridBagWindow() {
// new Data
JButton NewDataButton = new JButton("This button should change the layout of the Table");
NewDataButton.setMnemonic(KeyEvent.VK_I);
NewDataButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//************************//
//THIS DOES NOT WORK !!!!
// WHO CAN FIX THIS ?????
//************************//
contentPane.remove(table);
table = new JTable(4, 4);
c.gridx = 0;
c.gridy = 1;
gridbag.setConstraints(table, c);
contentPane.add(table);
table.repaint();
}
});
table = new JTable(5, 5);
table.setRowSelectionAllowed(true);
table.setPreferredScrollableViewportSize(new Dimension(800, 200));
contentPane = getContentPane();
gridbag = new GridBagLayout();
c = new GridBagConstraints();
contentPane.setLayout(gridbag);
if (shouldFill) {
c.fill = GridBagConstraints.HORIZONTAL;
}
if (shouldWeightX) {
c.weightx = 0.5;
}
c.gridx = 0;
c.gridy = 0;
gridbag.setConstraints(NewDataButton, c);
contentPane.add(NewDataButton);
c.gridx = 0;
c.gridy = 1;
gridbag.setConstraints(table, c);
contentPane.add(table);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this window.
gridbag.setConstraints(scrollPane, c);
getContentPane().add(scrollPane);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (inAnApplet) {
dispose();
} else {
System.exit(0);
}
}
});
}
public static void main(String args[]) {
GridBagWindow window = new GridBagWindow();
window.inAnApplet = false;
window.setTitle("Change the Table layout ...");
window.pack();
window.setVisible(true);
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|