JDialog unmovable形式JTable单元格编辑器

时间:2015-01-04 19:21:23

标签: java swing jtable tablecelleditor

我已自定义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();

      }
  }

1 个答案:

答案 0 :(得分:2)

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);
            }
        });
    }

}