如何刷新Tableview中的特定行或单元格。
下面是刷新整个表格的代码行。
tableView.refresh()
下面是刷新表格中特定列的代码行。
tableView.getColumns().get(0).setVisible(false);
tableView.getColumns().get(0).setVisible(true);
如何刷新特定行或特定单元格?
答案 0 :(得分:0)
您不需要刷新行或TableView。最好将单元格绑定到放入其中的类的属性。
From the javadocs for TableView:
您可以通过定义您的类来实现此目的:
TableView<Person> table = new TableView<Person>();
首先,需要定义一个TableView实例,如下所示:
ObservableList<Person> teamMembers = getTeamMembers();
table.setItems(teamMembers);
定义基本表后,我们接下来关注数据模型。如上所述,对于此示例,我们将使用ObservableList。我们可以立即将这样的列表直接设置到TableView中,如下所示:
ObservableList<Person> teamMembers = ...;
table.setItems(teamMembers);
TableColumn<Person,String> firstNameCol = new TableColumn<Person,String>("First Name");
firstNameCol.setCellValueFactory(new PropertyValueFactory("firstName"));
TableColumn<Person,String> lastNameCol = new TableColumn<Person,String>("Last Name");
lastNameCol.setCellValueFactory(new PropertyValueFactory("lastName"));
table.getColumns().setAll(firstNameCol, lastNameCol);
如果设置了这样的项目,只要teamMembers列表发生更改,TableView就会自动更新。如果在实例化TableView之前项目列表可用,则可以将其直接传递给构造函数。
此时我们现在连接了一个TableView来观察teamMembers的observableList。现在缺少的成分是分割模型中包含的数据并在一个或多个TableColumn实例中表示它的方法。要创建一个两列的TableView来显示firstName和lastName属性,我们按如下方式扩展最后一个代码示例:
firstNameCol.setCellValueFactory(new Callback<CellDataFeatures<Person, String>, ObservableValue<String>>() {
public ObservableValue<String> call(CellDataFeatures<Person, String> p) {
// p.getValue() returns the Person instance for a particular TableView row
return p.getValue().firstNameProperty();
}
});
}
使用上面显示的代码,我们已经完全定义了创建TableView实例所需的最小属性。运行此代码(假设人员ObservableList已正确创建)将导致TableView显示为firstName和lastName两列。不会显示Person类的任何其他属性,因为没有定义TableColumns。
如果您的课程没有属性且您不想实现它们,您可以像这样创建自己的自定义CellValueFactory:
public class BrowserScreen extends GuiScreen {
private BrowserView view;
private Browser browser;
public BrowserScreen(BrowserView view, Browser browser) {
this.view = view;
this.browser = browser;
}
int browserWidth = 1260;
int browserHeight = (int) (browserWidth * (float) height / (float) width);
@Override
public void initGui() {
browserHeight = (int) (browserWidth * (float) height / (float) width);
browser.setSize(browserWidth, browserHeight);
view.setSize(browserWidth, browserHeight);
view.setVisible(true);
}
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
if (browser == null || view == null) return;
LightWeightWidget component = (LightWeightWidget) view.getComponent(0);
BufferedImage image = (BufferedImage) component.getImage();
if (image == null) return;
//Bind captured image to opengl
int width = image.getWidth();
int height = image.getHeight();
int[] pixels = new int[width * height];
image.getRGB(0, 0, width, height, pixels, 0, width);
ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * 3); //4 for RGBA, 3 for RGB
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int pixel = pixels[y * width + x];
buffer.put((byte) ((pixel >> 16) & 0xFF)); // Red component
buffer.put((byte) ((pixel >> 8) & 0xFF)); // Green component
buffer.put((byte) (pixel & 0xFF)); // Blue component
//buffer.put((byte) ((pixel >> 24) & 0xFF)); // Alpha component. Only for RGBA
}
}
buffer.flip(); //FOR THE LOVE OF GOD DO NOT FORGET THIS
// You now have a ByteBuffer filled with the color data of each pixel.
// Now just create a texture ID and bind it. Then you can load it using
// whatever OpenGL method you want, for example:
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, width, height, 0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, buffer);
//Draw the quad
float pictureRatio = (float) width / (float) height;
float screenRatio = (float) this.width / (float) this.height;
int x = 0;
int y = 0;
if (pictureRatio > screenRatio) {
height = (int) (this.width * ((float) height / (float) width));
y = this.height / 2 - height / 2;
} else {
width = (int) (this.height * ((float) width / (float) height));
x = this.width / 2 - width / 2;
}
Gui.drawScaledCustomSizeModalRect(x, y, 0, 0, width, height, this.width, this.height, width, height);
}
@Override
public void handleMouseInput() throws IOException {
if (browser == null) return;
if (Mouse.isButtonDown(-1)) {
BrowserMouseEvent.BrowserMouseEventBuilder builder = new BrowserMouseEvent.BrowserMouseEventBuilder();
builder.setEventType(MOUSE_PRESSED)
.setButtonType(BrowserMouseEvent.MouseButtonType.PRIMARY)
.setClickCount(1)
.setModifiers(new BrowserKeyEvent.KeyModifiersBuilder().mouseButton().build());
browser.forwardMouseEvent(builder.build());
System.out.println("Left clicked");
}
int i = Mouse.getEventDWheel();
if (i != 0) {
if (i > 1) {
i = 6;
}
if (i < -1) {
i = -6;
}
BrowserMouseEvent.BrowserMouseEventBuilder builder = new BrowserMouseEvent.BrowserMouseEventBuilder();
builder.setEventType(MOUSE_WHEEL)
.setScrollBarPixelsPerLine(25)
.setScrollType(WHEEL_BLOCK_SCROLL)
.setUnitsToScroll(i);
browser.forwardMouseEvent(builder.build());
System.out.println("Scrolled: " + i);
}
super.handleMouseInput();
}
@Override
protected void mouseReleased(int mouseX, int mouseY, int state) {
if (browser == null) return;
BrowserMouseEvent.BrowserMouseEventBuilder builder = new BrowserMouseEvent.BrowserMouseEventBuilder();
builder.setEventType(MOUSE_RELEASED)
.setButtonType(BrowserMouseEvent.MouseButtonType.PRIMARY)
.setClickCount(1)
.setModifiers(BrowserKeyEvent.KeyModifiers.NO_MODIFIERS);
browser.forwardMouseEvent(builder.build());
System.out.println("Released");
}
@Override
public void onGuiClosed() {
dispose();
}
private boolean dispose() {
if (browser != null) {
if (!browser.isDisposed()) browser.dispose();
browser = null;
}
if (view != null) {
if (view.isEnabled()) {
view.setEnabled(false);
view = null;
return true;
}
view = null;
}
return false;
}
public static BrowserScreen open(String url) {
Locale.setDefault(Locale.CHINA);
Browser browser = new Browser(BrowserType.LIGHTWEIGHT);
BrowserPreferences preferences = browser.getPreferences();
preferences.setTransparentBackground(true);
browser.setPreferences(preferences);
BrowserView view = new BrowserView(browser);
final BrowserScreen screen = new BrowserScreen(view, browser);
browser.loadURL(url);
return screen;
}
}