List of usage examples for javax.swing AbstractAction AbstractAction
public AbstractAction(String name)
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(new Dimension(500, 500)); JDialog dialog = new JDialog(frame, "Export", ModalityType.MODELESS); dialog.setSize(300, 300);/*from w ww . jav a 2 s . co m*/ JDialog dialog1 = new JDialog(dialog, "Export", ModalityType.APPLICATION_MODAL); dialog1.setSize(200, 200); frame.add(new JButton(new AbstractAction("Dialog") { @Override public void actionPerformed(ActionEvent e) { frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); dialog.setVisible(true); dialog1.setVisible(true); } })); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { DecimalFormat format = new DecimalFormat("####.##"); format.setMinimumFractionDigits(2);//www .j a va 2s .c o m final JFormattedTextField field1 = new JFormattedTextField(format); final JFormattedTextField field2 = new JFormattedTextField(format); field1.setColumns(15); field2.setColumns(15); JButton btn = new JButton(new AbstractAction("Multiply by 2") { @Override public void actionPerformed(ActionEvent e) { Number value = (Number) field1.getValue(); if (value != null) { field2.setValue(2 * value.doubleValue()); } } }); JPanel panel = new JPanel(); panel.add(field1); panel.add(btn); panel.add(field2); JOptionPane.showMessageDialog(null, panel); }
From source file:Main.java
public static void main(String[] args) { int ROW_HEIGHT = 40; String[] TABLE_COLUMNS = { "Foo", "Bar" }; DefaultTableModel tableModel = new DefaultTableModel(TABLE_COLUMNS, 2); JTable table = new JTable(tableModel); table.setRowHeight(ROW_HEIGHT);//ww w .ja v a2 s . com JScrollPane scrollpane = new JScrollPane(table); JButton addRowBtn = new JButton(new AbstractAction("Add Row") { @Override public void actionPerformed(ActionEvent arg0) { tableModel.addRow(new String[] { "", "" }); } }); JPanel btnPanel = new JPanel(); btnPanel.add(addRowBtn); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(scrollpane, BorderLayout.CENTER); f.getContentPane().add(btnPanel, BorderLayout.PAGE_END); f.pack(); f.setLocationByPlatform(true); f.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { DefaultMutableTreeNode root = new DefaultMutableTreeNode("root"); DefaultTreeModel model = new DefaultTreeModel(root); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(new JScrollPane(new JTree(model))); f.getContentPane().add(new JButton(new AbstractAction("Add thousand children") { @Override//w w w . j a v a 2 s. co m public void actionPerformed(ActionEvent e) { int offset = root.getChildCount() + 1; for (int i = 0; i < 1000; i++) { DefaultMutableTreeNode child = new DefaultMutableTreeNode("Person " + (i + offset)); // model.insertNodeInto(child, root, root.getChildCount()); root.add(child); } model.nodeStructureChanged(root); } }), BorderLayout.PAGE_END); f.pack(); f.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame() { @Override/*w ww .jav a 2 s . com*/ public synchronized void setExtendedState(int state) { if (isUndecorated() && (state & MAXIMIZED_BOTH) == MAXIMIZED_BOTH) { super.setMaximizedBounds(new Rectangle(300, 300)); } super.setExtendedState(state); } }; frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(800, 600); frame.setUndecorated(true); frame.getContentPane().add(new JButton(new AbstractAction("Toggle maximize") { @Override public void actionPerformed(ActionEvent e) { int state = frame.getExtendedState(); if ((state & JFrame.MAXIMIZED_BOTH) == JFrame.MAXIMIZED_BOTH) { frame.setExtendedState(JFrame.NORMAL); } else { frame.setExtendedState(JFrame.MAXIMIZED_BOTH); } } }), BorderLayout.PAGE_END); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel buttonPanel = new JPanel(); buttonPanel.add(new JButton(new AbstractAction("Update") { @Override/* w w w . j a va2s.c om*/ public void actionPerformed(ActionEvent ae) { StyledDocument doc = (StyledDocument) textPane.getDocument(); SimpleAttributeSet style = new SimpleAttributeSet(); StyleConstants.setFontFamily(style, "Serif"); StyleConstants.setFontSize(style, size++); try { doc.insertString(doc.getLength(), " one two three", style); Dimension d = textPane.getPreferredSize(); Rectangle r = textPane.modelToView(textPane.getDocument().getLength()); d.height = r.y + r.height; textPane.setPreferredSize(d); } catch (BadLocationException e) { e.printStackTrace(); } frame.pack(); } })); frame.add(buttonPanel, BorderLayout.NORTH); textPane.setText("this is a test."); textPane.setBorder(new LineBorder(Color.BLACK)); frame.add(new JScrollPane(textPane)); frame.pack(); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] argv) { JTextComponent textcomp = new JTextArea(); final UndoManager undo = new UndoManager(); Document doc = textcomp.getDocument(); doc.addUndoableEditListener(new UndoableEditListener() { public void undoableEditHappened(UndoableEditEvent evt) { undo.addEdit(evt.getEdit()); }// w w w. j a v a2 s. co m }); textcomp.getActionMap().put("Undo", new AbstractAction("Undo") { public void actionPerformed(ActionEvent evt) { try { if (undo.canUndo()) { undo.undo(); } } catch (CannotUndoException e) { } } }); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new JScrollPane(textcomp)); frame.setSize(380, 320); frame.setLocationRelativeTo(null); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JProgressBar progressBar = new JProgressBar(); progressBar.setOpaque(false);/*www . j a v a 2s . com*/ progressBar.setUI(new GradientPalletProgressBarUI()); JPanel p = new JPanel(); p.add(progressBar); p.add(new JButton(new AbstractAction("Start") { @Override public void actionPerformed(ActionEvent e) { SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() { @Override public Void doInBackground() { int current = 0, lengthOfTask = 100; while (current <= lengthOfTask && !isCancelled()) { try { Thread.sleep(50); } catch (Exception ie) { return null; } setProgress(100 * current / lengthOfTask); current++; } return null; } }; worker.addPropertyChangeListener(new ProgressListener(progressBar)); worker.execute(); } })); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.getContentPane().add(p); frame.setSize(320, 240); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] argv) { JTextComponent textcomp = new JTextArea(); final UndoManager undo = new UndoManager(); Document doc = textcomp.getDocument(); doc.addUndoableEditListener(new UndoableEditListener() { public void undoableEditHappened(UndoableEditEvent evt) { undo.addEdit(evt.getEdit()); }// w w w . j a va 2 s . co m }); textcomp.getActionMap().put("Undo", new AbstractAction("Undo") { public void actionPerformed(ActionEvent evt) { try { if (undo.canUndo()) { undo.undo(); } } catch (CannotUndoException e) { } } }); textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo"); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new JScrollPane(textcomp)); frame.setSize(380, 320); frame.setLocationRelativeTo(null); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] argv) throws Exception { JTextComponent textcomp = new JTextArea(); final UndoManager undo = new UndoManager(); Document doc = textcomp.getDocument(); doc.addUndoableEditListener(new UndoableEditListener() { public void undoableEditHappened(UndoableEditEvent evt) { undo.addEdit(evt.getEdit()); }/*from w ww. ja va 2s . c om*/ }); textcomp.getActionMap().put("Undo", new AbstractAction("Undo") { public void actionPerformed(ActionEvent evt) { try { if (undo.canUndo()) { undo.undo(); } } catch (CannotUndoException e) { } } }); textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo"); textcomp.getActionMap().put("Redo", new AbstractAction("Redo") { public void actionPerformed(ActionEvent evt) { try { if (undo.canRedo()) { undo.redo(); } } catch (CannotRedoException e) { } } }); textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo"); }