gpt4 book ai didi

java - JDialog不可移动窗体JTable单元格编辑器

转载 作者:行者123 更新时间:2023-12-02 05:06:13 36 4
gpt4 key购买 nike

我自定义了 JTable 单元格编辑器,以便允许从 JDialog 框架输入数据。我为此使用了一个可编辑的组合框,我为组合框添加了一个 ActionListener 来显示对话框。

我的 JDialog 可见,但我想让它不可移动,这样用户就无法移动它。

这是到目前为止我的代码,

package VIEW;

import VIEW.statManager.SearchProduitEvent;
import VIEW.statManager.SearchProduitEventListener;
import java.awt.Component;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import javax.swing.table.TableCellEditor;
import javax.swing.text.JTextComponent;


public class ProduitCellEditor extends AbstractCellEditor implements TableCellEditor,ActionListener, SearchProduitEventListener {

private JComboBox combo;
private SearchProduitUi searchProduitUi;
private String value = "value";
public ProduitCellEditor() {

combo = new JComboBox();
combo.setEditable(true);
combo.setActionCommand("combo");

searchProduitUi = new SearchProduitUi();
searchProduitUi.setSearchProduitEventListener(this);
searchProduitUi.setSize(500,300);
searchProduitUi.setLocationRelativeTo(combo);
}


@Override
public Object getCellEditorValue() {
return value;
}

@Override
public Component getTableCellEditorComponent(JTable jtable, Object o, boolean bln, int i, int i1) {
return combo;
}


public void actionPerformed(ActionEvent event) {
Point comboPosition = combo.getLocationOnScreen();
searchProduitUi.setLocationRelativeTo(combo);
searchProduitUi.setLocation(comboPosition.x ,comboPosition.y + combo.getHeight());
searchProduitUi.setVisible(true);

}

@Override
public void searchDialogEventOccured(SearchProduitEvent ev) {
value = ev.getProduit().getDesignation();
fireEditingStopped();

}
}

最佳答案

JDialog#setUndecorated 将删除框架装饰,包括关闭/最小化/最大化控件,并使用户无法移动窗口。

enter image description here

import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class Test1 {

public static void main(String[] args) {
new Test1();
}

public Test1() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JPanel content = new JPanel(new GridBagLayout());
content.setBorder(new EmptyBorder(8, 8, 8, 8));
JLabel label = new JLabel("Hello world");
content.add(label);

JDialog dialog = new JDialog();
dialog.setUndecorated(true);
dialog.setTitle("Testing");
dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialog.setContentPane(content);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
}
});
}

}

关于java - JDialog不可移动窗体JTable单元格编辑器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27769384/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com