Programming Articles - Page 2639 of 3366

C-style parser for command line options in Python

Rishi Raj
Updated on 30-Jul-2019 22:30:26

355 Views

Python’s sys module provides access to any command-line arguments via the sys.argv. sys.argv is the list of command-line arguments and sys.argv[0] is the program ie. the script name.Save following code as args.pyimport sys print ('argument list', sys.argv)Execute above script from command line as follows:C:\python37>python args.py 11 22 argument list ['args.py', '11', '22']The getopt module has funcions toparse the command line arguments in sys.argv. It supports the same conventions as the Unix getopt() function (including the special meanings of arguments of the form ‘-‘ and ‘--‘).API is designed to be familiar to users of the C getopt() function.getopt(args, shortopts, longopts=[])Parses command ... Read More

Draw a border around an undecorated JFrame in Java

Samual Sam
Updated on 30-Jul-2019 22:30:26

806 Views

At first, set an undecorated frame −setUndecorated(true);Now draw a border −getRootPane().setBorder (.createMatteBorder(3, 3, 3, 3, Color.ORANGE));The following is an example to draw a border around an undecorated JFrame −Exampleimport java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; public class SwingDemo extends JFrame {    JLabel label = new JLabel("Welcome!", JLabel.CENTER);    public SwingDemo() {       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       setSize(new Dimension(500, 300));       add(label, BorderLayout.CENTER);       setUndecorated(true);       getRootPane().setBorder(          BorderFactory.createMatteBorder(3, 3, 3, 3, Color.ORANGE));       setVisible(true);    }    public static void main(String[] args) {       new SwingDemo();    } }Output

How to create modeless and model JDialog in Java?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

597 Views

MODELESS TypeThe following is an example to set JDialog with Modality type MODELESS −Exampleimport java.awt.Cursor; import java.awt.Dialog.ModalityType; import java.awt.Dimension; import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       frame.setSize(new Dimension(600, 400));       JDialog dialog = new JDialog(frame, "New", ModalityType.MODELESS);       dialog.setSize(300, 300);       frame.add(new JButton(new AbstractAction("Click to generate") {          @Override          public void actionPerformed(ActionEvent e) {             frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));   ... Read More

How to set default button for JFrame in Java?

Samual Sam
Updated on 30-Jul-2019 22:30:26

1K+ Views

To set default button for JFrame, use the setDefaultButton() method −JFrame frame = new JFrame(); frame.getRootPane().setDefaultButton(button);The following is an example to set default button for JFrame −Exampleimport java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.JButton; import javax.swing.JFrame; public class SwingDemo {    public static void main(String args[]) {       JButton button = new JButton("Demo Button!");       JFrame frame = new JFrame();       frame.setSize(500, 300);       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       frame.getRootPane().setDefaultButton(button);       button.setMnemonic(KeyEvent.VK_A);       button.addActionListener(new ActionListener() {          public void actionPerformed(ActionEvent ae) {         ... Read More

How to set minimum size limit for a JFrame in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

3K+ Views

Use the setMinimumSize() method to set the minimum size limit for a JFrame −JFrame frame = new JFrame(); frame.setMinimumSize(new Dimension(500, 300));The following is an example to set minimum size limit for a JFrame −Exampleimport java.awt.Color; import java.awt.Dimension; import javax.swing.JButton; import javax.swing.JFrame; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       JButton button = new JButton("Close!");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       frame.setContentPane(button);       button.addActionListener(e -> {          frame.dispose();       });       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       frame.setMinimumSize(new Dimension(500, ... Read More

Get JFrame window size information in Java

Samual Sam
Updated on 30-Jul-2019 22:30:26

3K+ Views

To get JFrame window size information, you can use the following −environment.getMaximumWindowBounds();For Screen Size −config.getBounds()For Frame Size −frame.getSize());The following is an example to get JFrame window size information −Exampleimport java.awt.GraphicsConfiguration; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.Rectangle; import javax.swing.JFrame; public class SwingDemo {    public static void main(String[] args) {       GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();       Rectangle bounds = environment.getMaximumWindowBounds();       System.out.println("Screen Bounds = " + bounds);       GraphicsDevice device = environment.getDefaultScreenDevice();       GraphicsConfiguration config = device.getDefaultConfiguration();       System.out.println("Screen Size = " + config.getBounds());       JFrame frame ... Read More

How can we clear all selections in Java Swing JList?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

689 Views

To clear all selections, use the List clearSelection() method in Java −JList list = new JList(sports); list.clearSelection();Above, the elements in Sports array is a String array −String sports[]= { "Cricket", "Football", "Hockey", "Rugby"};The following is an example to clear all selection in JList −Examplepackage my; import java.awt.event.*; import java.awt.*; import javax.swing.*; class SwingDemo extends JFrame {    static JFrame frame;    static JList list;    public static void main(String[] args) {       frame = new JFrame("JList Demo");       SwingDemo s = new SwingDemo();       JPanel panel = new JPanel();       String sports[]= ... Read More

How to select the second index in Java JList?

Samual Sam
Updated on 30-Jul-2019 22:30:26

402 Views

To select the second index, use the setSelectedIndex() method −JList new JList(sports); list.setSelectedIndex(2);The following is an example to select the second index in Java JList −Examplepackage my; import java.awt.event.*; import java.awt.*; import javax.swing.*; class SwingDemo extends JFrame {    static JFrame frame;    static JList list;    public static void main(String[] args) {       frame = new JFrame("JList Demo");       SwingDemo s = new SwingDemo();       JPanel panel = new JPanel();       String sports[]= { "Cricket","Football","Hockey","Rugby"};       list = new JList(sports);       list.setSelectedIndex(2);       panel.add(list);       frame.add(panel);       frame.setSize(400,400);       frame.setVisible(true);    } }Output

Java Program to create JRadioButton from text

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

138 Views

The following is an example to create JRadioButton from text −Examplepackage my; import java.awt.FlowLayout; import java.awt.event.KeyEvent; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JRadioButton; public class SwingDemo {    public static void main(String[] args) {       JRadioButton radio1 = new JRadioButton("Cricket");       JRadioButton radio2 = new JRadioButton("Football");       ButtonGroup group = new ButtonGroup();       group.add(radio1);       group.add(radio2);       radio1.setSelected(true);       JFrame frame = new JFrame();       frame.setLayout(new FlowLayout());       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       frame.add(new JLabel("Fav Sports:"));       frame.add(radio1);     ... Read More

How to add background Image to JFrame in Java

Samual Sam
Updated on 30-Jul-2019 22:30:26

16K+ Views

To add background image to JFrame, use the getImage() method of the Image class −Image img = Toolkit.getDefaultToolkit().getImage("E:\rahul.jpg");Now, draw the image −public void paintComponent(Graphics g) {    super.paintComponent(g);    g.drawImage(img, 0, 0, null); }The following is an example to add Background Image to JFrame −Exampleimport java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; import java.io.IOException; import javax.swing.JPanel; public class SwingDemo extends javax.swing.JFrame {    Image img = Toolkit.getDefaultToolkit().getImage("E:\rahul.jpg");    public SwingDemo() throws IOException {       this.setContentPane(new JPanel() {          @Override          public void paintComponent(Graphics g) {             super.paintComponent(g);     ... Read More

Advertisements