Example usage for javax.swing JLabel setSize

List of usage examples for javax.swing JLabel setSize

Introduction

In this page you can find the example usage for javax.swing JLabel setSize.

Prototype

public void setSize(Dimension d) 

Source Link

Document

Resizes this component so that it has width d.width and height d.height .

Usage

From source file:Main.java

public static void main(String[] args) {
    String s = "The quick brown fox jumps over the lazy dog!";
    BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();
    FontMetrics fm = g.getFontMetrics();
    Rectangle2D b = fm.getStringBounds(s, g);
    System.out.println(b);//from  ww w. j  av  a  2 s  . c om
    bi = new BufferedImage((int) b.getWidth(), (int) (b.getHeight() + fm.getDescent()),
            BufferedImage.TYPE_INT_RGB);
    g = bi.getGraphics();
    g.drawString(s, 0, (int) b.getHeight());

    JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi)));

    // Technique 3 - JLabel
    JLabel l = new JLabel(s);
    l.setSize(l.getPreferredSize());
    bi = new BufferedImage(l.getWidth(), l.getHeight(), BufferedImage.TYPE_INT_RGB);
    g = bi.getGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, 400, 100);
    l.paint(g);

    JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi)));
}

From source file:Main.java

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            BufferedImage image = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB);
            Graphics2D imageGraphics = image.createGraphics();
            GradientPaint gp = new GradientPaint(20f, 20f, Color.red, 380f, 280f, Color.orange);
            imageGraphics.setPaint(gp);/*  w  w  w  . j a va2  s. com*/
            imageGraphics.fillRect(0, 0, 400, 300);

            JLabel textLabel = new JLabel("java2s.com");
            textLabel.setSize(textLabel.getPreferredSize());

            Dimension d = textLabel.getPreferredSize();
            BufferedImage bi = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_ARGB);
            Graphics g = bi.createGraphics();
            g.setColor(new Color(255, 200, 255, 128));
            g.fillRoundRect(0, 0, bi.getWidth(f), bi.getHeight(f), 15, 10);
            g.setColor(Color.black);
            textLabel.paint(g);
            Graphics g2 = image.getGraphics();
            g2.drawImage(bi, 20, 20, f);

            ImageIcon ii = new ImageIcon(image);
            JLabel imageLabel = new JLabel(ii);

            f.getContentPane().add(imageLabel);
            f.pack();

            f.setVisible(true);
        }
    });
}

From source file:JColorChooserWithCustomPreviewPanel.java

public static void main(String[] a) {

    final JLabel previewLabel = new JLabel("I Love Swing", JLabel.CENTER);
    previewLabel.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 48));
    previewLabel.setSize(previewLabel.getPreferredSize());
    previewLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 1, 0));

    JColorChooser colorChooser = new JColorChooser();
    colorChooser.setPreviewPanel(previewLabel);

    JDialog d = colorChooser.createDialog(null, "", true, colorChooser, null, null);

    d.setVisible(true);//from  ww  w.  j  av  a 2s .  c o m
}

From source file:MainClass.java

public static void main(String[] a) {

    final JColorChooser colorChooser = new JColorChooser();
    final JLabel previewLabel = new JLabel("I Love Swing", JLabel.CENTER);
    previewLabel.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 48));
    previewLabel.setSize(previewLabel.getPreferredSize());
    previewLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 1, 0));
    colorChooser.setPreviewPanel(previewLabel);

    ActionListener okActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("OK Button");
            System.out.println(colorChooser.getColor());
        }//from   w w  w .  j a v a  2s . c o  m
    };

    ActionListener cancelActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("Cancel Button");
        }
    };

    final JDialog dialog = JColorChooser.createDialog(null, "Change Button Background", true, colorChooser,
            okActionListener, cancelActionListener);

    dialog.setVisible(true);
}

From source file:logica_controladores.controlador_estadistica.java

public static void grafica_reorden(JPanel panel_grafica_orden, Inventario inventario, JLabel lbLinea) {
    XYSeries serie_2 = null;// w  w w .jav  a2  s  . c o  m
    XYDataset datos;
    JFreeChart linea;

    serie_2 = new XYSeries("graficas relacion gastos-reorden");

    for (int i = 0; i < inventario.getGastos().size(); i++) {
        serie_2.add(inventario.getGastos().get(i).getReorden(), inventario.getGastos().get(i).getGastos());
    }
    datos = new XYSeriesCollection(serie_2);
    linea = ChartFactory.createXYLineChart("grafica representativa de reordenes por corrida", "punto de orden",
            "gastos", datos, PlotOrientation.VERTICAL, true, true, true);
    BufferedImage graficoLinea = linea.createBufferedImage(panel_grafica_orden.getWidth(),
            panel_grafica_orden.getHeight());
    lbLinea.setSize(panel_grafica_orden.getSize());
    lbLinea.setIcon(new ImageIcon(graficoLinea));
    panel_grafica_orden.updateUI();
}

From source file:Main.java

public Main() {
    mainPanel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
    mainPanel.setLayout(null);/*from  www  . ja v  a2 s  .  c  o  m*/

    MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
    for (int i = 0; i < LABEL_STRINGS.length; i++) {
        JLabel label = new JLabel(LABEL_STRINGS[i], SwingConstants.CENTER);
        label.setSize(new Dimension(LBL_WIDTH, LBL_HEIGHT));
        label.setOpaque(true);
        Random random = new Random();
        label.setLocation(random.nextInt(WIDTH - LBL_WIDTH), random.nextInt(HEIGHT - LBL_HEIGHT));
        label.setBackground(
                new Color(150 + random.nextInt(105), 150 + random.nextInt(105), 150 + random.nextInt(105)));
        label.addMouseListener(myMouseAdapter);
        label.addMouseMotionListener(myMouseAdapter);

        mainPanel.add(label);
    }
}

From source file:de.iew.imageupload.widgets.ContentPane.java

public void registerImageFile(File file) {
    try {/*  w  w w .  jav a2  s . c  om*/
        ImageIcon imageIcon = new ImageIcon(file.getAbsolutePath());
        JLabel imageLabel = new JLabel(imageIcon, JLabel.CENTER);
        imageLabel.setPreferredSize(new Dimension(100, 100));
        imageLabel.setSize(new Dimension(100, 100));
        imageLabel.addMouseListener(this.imageMouseClickHandler);
        imageLabel.setBackground(Color.WHITE);
        imageLabel.setBorder(new EmptyBorder(10, 10, 10, 10));
        imageLabel.putClientProperty("Image File", file);

        this.imageGridPane.add(imageLabel);
        this.imageGridPane.revalidate();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:net.sf.firemox.ui.component.SplashScreen.java

/**
 * Create a new instance of this class./*from w w  w .ja v  a 2s.  c  o  m*/
 * 
 * @param filename
 *          the picture filename.
 * @param parent
 *          the splash screen's parent.
 * @param waitTime
 *          the maximum time before the screen is hidden.
 */
public SplashScreen(String filename, Frame parent, int waitTime) {
    super(parent);
    getContentPane().setLayout(null);
    toFront();
    final JLabel l = new JLabel(new ImageIcon(filename));
    final Dimension labelSize = l.getPreferredSize();
    l.setLocation(0, 0);
    l.setSize(labelSize);
    setSize(labelSize);

    final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    setLocation(screenSize.width / 2 - labelSize.width / 2, screenSize.height / 2 - labelSize.height / 2);

    final JLabel mp = new JLabel(IdConst.PROJECT_DISPLAY_NAME);
    mp.setLocation(30, 305);
    mp.setSize(new Dimension(300, 30));

    final JLabel version = new JLabel(IdConst.VERSION);
    version.setLocation(235, 418);
    version.setSize(new Dimension(300, 30));

    final JTextArea disclaimer = new JTextArea();
    disclaimer.setEditable(false);
    disclaimer.setLineWrap(true);
    disclaimer.setWrapStyleWord(true);
    disclaimer.setAutoscrolls(true);
    disclaimer.setFont(MToolKit.defaultFont);
    disclaimer.setTabSize(2);

    // Then try and read it locally
    Reader inGPL = null;
    try {
        inGPL = new BufferedReader(new InputStreamReader(MToolKit.getResourceAsStream(IdConst.FILE_LICENSE)));
        disclaimer.read(inGPL, "");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(inGPL);
    }

    final JScrollPane disclaimerSPanel = new JScrollPane();
    disclaimerSPanel.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    disclaimerSPanel.setViewportView(disclaimer);
    disclaimerSPanel.setLocation(27, 340);
    disclaimerSPanel.setPreferredSize(new Dimension(283, 80));
    disclaimerSPanel.setSize(disclaimerSPanel.getPreferredSize());

    getContentPane().add(disclaimerSPanel);
    getContentPane().add(version);
    getContentPane().add(mp);
    getContentPane().add(l);

    final int pause = waitTime;
    final Runnable waitRunner = new Runnable() {
        public void run() {
            try {
                Thread.sleep(pause);
                while (!bKilled) {
                    Thread.sleep(200);
                }
            } catch (InterruptedException e) {
                // Ignore this error
            }
            setVisible(false);
            dispose();
            MagicUIComponents.magicForm.toFront();
        }
    };

    // setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            setVisible(false);
            dispose();
            if (MagicUIComponents.magicForm != null)
                MagicUIComponents.magicForm.toFront();
        }
    });
    addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                setVisible(false);
                dispose();
                MagicUIComponents.magicForm.toFront();
            }
        }
    });
    setVisible(true);
    start(waitRunner);
}

From source file:edu.clemson.cs.nestbed.client.gui.MoteConfigPanel.java

public MoteConfigPanel(MoteTestbedAssignment mtbAssignment, ProgramManager programManager, int projDepConfID,
        MoteManager moteManager, MoteTypeManager moteTypeManager, MoteDeploymentConfigurationManager mdcManager)
        throws RemoteException, NotBoundException, MalformedURLException {

    this.programManager = programManager;
    this.mtbAssignment = mtbAssignment;
    this.projDepConfID = projDepConfID;
    this.mote = moteManager.getMote(mtbAssignment.getMoteID());
    this.moteType = moteTypeManager.getMoteType(mote.getMoteTypeID());
    this.mdcManager = mdcManager;
    this.moteDepConfig = mdcManager.getMoteDeploymentConfiguration(projDepConfID, mote.getID());
    this.radioChangeListener = new RadioChangeListener();
    this.radioSpinner = new JSpinner(
            new SpinnerNumberModel(MAX_RADIO_POWER, MIN_RADIO_POWER, MAX_RADIO_POWER, RADIO_POWER_INCR));

    setIcon(new ImageIcon(moteType.getImage()).getImage());

    if (this.moteDepConfig != null) {
        this.program = programManager.getProgram(moteDepConfig.getProgramID());
        radioSpinner.setValue(moteDepConfig.getRadioPowerLevel());
    } else {//from  w ww .j  a  v a 2 s.c  om
        radioSpinner.setEnabled(false);
    }
    radioSpinner.setToolTipText("Radio Power Level");

    Dimension labelSize = new Dimension(LABEL_WIDTH, LABEL_HEIGHT);
    JLabel addressLabel = new JLabel("" + mtbAssignment.getMoteAddress());
    addressLabel.setSize(labelSize);
    addressLabel.setPreferredSize(labelSize);

    Dimension spinnerSize = new Dimension(SPINNER_WIDTH, SPINNER_HEIGHT);
    radioSpinner.setSize(spinnerSize);
    radioSpinner.setPreferredSize(spinnerSize);
    radioSpinner.addChangeListener(radioChangeListener);

    setToolTipText(getToolTipString());
    addMouseListener(new MotePanelMouseListener());

    add(addressLabel);
    addressLabel.setLocation(2, 0);

    add(radioSpinner);
    radioSpinner.setLocation(getWidth() - SPINNER_WIDTH - 2, getHeight() - SPINNER_HEIGHT - 2);

    mdcManager.addRemoteObserver(new MoteDeploymentConfigObserver());
    new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE, new ProgramDropTarget(), true);
}

From source file:net.sf.taverna.t2.activities.rshell.views.RshellConfigurationPanel.java

private Component createSettingsPanel() {
    JPanel settingsPanel = new JPanel(new GridBagLayout());

    GridBagConstraints labelConstraints = new GridBagConstraints();
    labelConstraints.weightx = 0.0;// w w  w  . j av a2 s.com
    labelConstraints.gridx = 0;
    labelConstraints.gridy = 0;
    labelConstraints.fill = GridBagConstraints.NONE;
    labelConstraints.anchor = GridBagConstraints.LINE_START;

    GridBagConstraints fieldConstraints = new GridBagConstraints();
    fieldConstraints.weightx = 1.0;
    fieldConstraints.gridx = 1;
    fieldConstraints.gridy = 0;
    fieldConstraints.fill = GridBagConstraints.HORIZONTAL;

    GridBagConstraints buttonConstraints = new GridBagConstraints();
    buttonConstraints.weightx = 1.0;
    buttonConstraints.gridx = 1;
    buttonConstraints.gridy = 2;
    buttonConstraints.fill = GridBagConstraints.NONE;
    buttonConstraints.anchor = GridBagConstraints.WEST;

    Dimension dimension = new Dimension(0, 0);

    hostnameField = new JTextField();
    JLabel hostnameLabel = new JLabel("Hostname");
    hostnameField.setSize(dimension);
    hostnameLabel.setSize(dimension);
    hostnameLabel.setLabelFor(hostnameField);
    JsonNode connectionSettings = getJson().path("connection");

    hostnameField.setText(connectionSettings.path("hostname").asText());

    portField = new JTextField();
    JLabel portLabel = new JLabel("Port");
    portField.setSize(dimension);
    portLabel.setSize(dimension);
    portLabel.setLabelFor(portField);
    portField.setText(
            Integer.toString(connectionSettings.path("port").asInt(RshellTemplateService.DEFAULT_PORT)));

    // "Set username and password" button
    ActionListener usernamePasswordListener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (credManagerUI == null) {
                credManagerUI = new CredentialManagerUI(credentialManager);
            }
            credManagerUI.newPasswordForService(
                    URI.create("rserve://" + hostnameField.getText() + ":" + portField.getText())); // this is used as a key for the service in Credential Manager
        }
    };
    JButton setHttpUsernamePasswordButton = new JButton("Set username and password");
    setHttpUsernamePasswordButton.setSize(dimension);
    setHttpUsernamePasswordButton.addActionListener(usernamePasswordListener);

    keepSessionAliveCheckBox = new JCheckBox("Keep Session Alive");
    keepSessionAliveCheckBox.setSelected(connectionSettings.path("keepSessionAlive")
            .asBoolean(RshellTemplateService.DEFAULT_KEEP_SESSION_ALIVE));

    settingsPanel.add(hostnameLabel, labelConstraints);
    labelConstraints.gridy++;
    settingsPanel.add(hostnameField, fieldConstraints);
    fieldConstraints.gridy++;

    settingsPanel.add(portLabel, labelConstraints);
    labelConstraints.gridy++;
    settingsPanel.add(portField, fieldConstraints);
    fieldConstraints.gridy++;

    settingsPanel.add(setHttpUsernamePasswordButton, buttonConstraints);
    buttonConstraints.gridy++;

    fieldConstraints.gridy++;
    settingsPanel.add(keepSessionAliveCheckBox, fieldConstraints);
    fieldConstraints.gridy++;

    return settingsPanel;
}