我在JScrollpane內部有一個JTable。我無權訪問JScrollpane變量。但我有權訪問JTable。現在我怎樣才能使用JTable訪問JScrollpane。訪問其中包含JTable的JScrollpane
For Example -> mytable.getAncestor(...) or something?
我在JScrollpane內部有一個JTable。我無權訪問JScrollpane變量。但我有權訪問JTable。現在我怎樣才能使用JTable訪問JScrollpane。訪問其中包含JTable的JScrollpane
For Example -> mytable.getAncestor(...) or something?
如果你想從你的JTable的
JTable jTable = new JTable(rowData, colData);
JScrollPane scrollPane = new JScrollPane(jTable);
// now you have the ViewPort
JViewport parent = (JViewport)jTable.getParent();
JScrollPane enclosing = (JScrollPane)parent.getParent();
嘗試下面的代碼獲得JScrollPane的..
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRootPane;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
/**
*
* @author Patrick Ott <[email protected]>
* @version 1.0
*/
public class MainFrame extends JFrame {
private String[][] rowData =
{
{"Max", "Mustermann", "Musterhausen"},
{"Max", "Mustermann", "Musterhausen"},
{"Max", "Mustermann", "Musterhausen"},
{"Max", "Mustermann", "Musterhausen"},
{"Max", "Mustermann", "Musterhausen"},
{"Max", "Mustermann", "Musterhausen"},
{"Max", "Mustermann", "Musterhausen"},
{"Max", "Mustermann", "Musterhausen"},
{"Max", "Mustermann", "Musterhausen"},
{"Max", "Mustermann", "Musterhausen"},
{"Max", "Mustermann", "Musterhausen"},
{"Max", "Mustermann", "Musterhausen"},
{"Max", "Mustermann", "Musterhausen"},
{"Max", "Mustermann", "Musterhausen"},
{"Max", "Mustermann", "Musterhausen"},
{"Max", "Mustermann", "Musterhausen"},
{"Max", "Mustermann", "Musterhausen"},
{"Max", "Mustermann", "Musterhausen"},
{"Max", "Mustermann", "Musterhausen"},
{"Petra", "Mustermann", "Musterhausen"}
};
private String[] columnData =
{
"Firstname", "Lastname", "City"
};
private JTable jTable;
public MainFrame() {
jTable = new JTable(rowData, columnData);
jTable.setName("CRM Table");
}
public void createAndShowGui() {
this.setTitle("JTable in JScrollPane");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(new JLabel("CRM System"), BorderLayout.NORTH);
JScrollPane scrollPane = new JScrollPane(jTable);
this.getContentPane().add(scrollPane, BorderLayout.CENTER);
this.setSize(new Dimension(1024, 768));
this.setVisible(true);
Container parent = jTable.getParent().getParent();
JScrollPane enclosing = (JScrollPane)parent;
parent.remove(jTable);
parent.add(new JLabel("Test"));
// System.out.println(enclosing.getClass().getSimpleName());
}
}
帕特里克
你不知道我有多少愛你我只是想說我是f **國王感謝和+1。 – khandelwaldeval
假設的JTable放在JScrollPane中作爲內是常用的:
JTable table = ...;
JScrollPane scrollPane = new JScrollPane(table);
您可以訪問使用的getParent()獲取視野和使用的getParent包表中的滾動窗格()在視口上得到滾動窗格。
JScrollPane enclosingScrollPane = (JScrollPane) table.getParent().getParent()
在最壞的情況,如果你因爲某些原因表不知道是否在裏面滾動窗格或沒有,你將有鑄造前檢查的instanceof表」父組件的。
代碼不起作用... :(錯誤是 - > javax.swing.JViewport無法轉換爲javax.swing.JScrollPane – khandelwaldeval
對不起,我忘了關於視口。只需使用table.getParent()。getParent()來滾動窗格。我將更新答案 –
+1。對不起,帕特里克先生給了我比你早的正確答案,所以剔到了他 – khandelwaldeval
不知道你是怎麼講的,你可以得到從任何角落訪問當前JVM中的任何對象,問題是關於兩個downvotes not upvotes – mKorbel
...從事實開始JScrollPane被指定爲只嵌套一個對象 – mKorbel
@mKorbel那麼我怎樣才能從這個「一個對象」,即jTable? – khandelwaldeval