Example usage for javax.swing JLabel setText

List of usage examples for javax.swing JLabel setText

Introduction

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

Prototype

@BeanProperty(preferred = true, visualUpdate = true, description = "Defines the single line of text this component will display.")
public void setText(String text) 

Source Link

Document

Defines the single line of text this component will display.

Usage

From source file:QandE.MyDemo2.java

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread./*from w  w w  .  j  a  v  a 2 s.  c o m*/
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("MyDemo2");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenu menu = new JMenu("Menu");
    JMenuBar mb = new JMenuBar();
    mb.add(menu);
    frame.setJMenuBar(mb);

    //Add a label with bold italic font.
    JLabel label = new JLabel("My Demo");
    frame.getContentPane().add(BorderLayout.CENTER, label);
    if (true) {
        label.setFont(label.getFont().deriveFont(Font.ITALIC | Font.BOLD));
    } else {
        //another way of doing it, but not as appropriate since 
        //setFont is faster than using HTML.
        label.setText("<html><i>My Demo</i></html>");
    }
    label.setHorizontalAlignment(JLabel.CENTER);

    //Display the window, making it a little bigger than it really needs to be.
    frame.pack();
    frame.setSize(frame.getWidth() + 100, frame.getHeight() + 50);
    frame.setVisible(true);
}

From source file:ua.com.fielden.platform.example.swing.booking.BookingChartPanelExample.java

private static IScheduleChangeEventListener<AbstractEntity<?>> createScheduleChangedListener(
        final JLabel taskBar) {
    return new IScheduleChangeEventListener<AbstractEntity<?>>() {

        @Override//w ww  .j a  v  a  2s .  c o  m
        public void scheduleChanged(final ScheduleChangedEvent<AbstractEntity<?>> event) {
            taskBar.setText(
                    event.getEntity() + ": (" + event.getSeries().getScheduleEntity().getFrom(event.getEntity())
                            + ", " + event.getSeries().getScheduleEntity().getTo(event.getEntity()) + ")");
        }

    };
}

From source file:com.heliosdecompiler.bootstrapper.Bootstrapper.java

private static HeliosData loadHelios() throws IOException {
    System.out.println("Finding Helios implementation");

    HeliosData data = new HeliosData();

    boolean needsToDownload = !IMPL_FILE.exists();
    if (!needsToDownload) {
        try (JarFile jarFile = new JarFile(IMPL_FILE)) {
            ZipEntry entry = jarFile.getEntry("META-INF/MANIFEST.MF");
            if (entry == null) {
                needsToDownload = true;//from   ww w  . j  a v a 2  s.c  o m
            } else {
                Manifest manifest = new Manifest(jarFile.getInputStream(entry));
                String ver = manifest.getMainAttributes().getValue("Implementation-Version");
                try {
                    data.buildNumber = Integer.parseInt(ver);
                    data.version = manifest.getMainAttributes().getValue("Version");
                    data.mainClass = manifest.getMainAttributes().getValue("Main-Class");
                } catch (NumberFormatException e) {
                    needsToDownload = true;
                }
            }
        } catch (IOException e) {
            needsToDownload = true;
        }
    }
    if (needsToDownload) {
        URL latestJar = new URL(LATEST_JAR);
        System.out.println("Downloading latest Helios implementation");

        FileOutputStream out = new FileOutputStream(IMPL_FILE);
        HttpURLConnection connection = (HttpURLConnection) latestJar.openConnection();
        if (connection.getResponseCode() == 200) {
            int contentLength = connection.getContentLength();
            if (contentLength > 0) {
                InputStream stream = connection.getInputStream();
                byte[] buffer = new byte[1024];
                int amnt;
                AtomicInteger total = new AtomicInteger();
                AtomicBoolean stop = new AtomicBoolean(false);

                Thread progressBar = new Thread() {
                    public void run() {
                        JPanel panel = new JPanel();
                        panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

                        JLabel label = new JLabel();
                        label.setText("Downloading latest Helios build");
                        panel.add(label);

                        GridLayout layout = new GridLayout();
                        layout.setColumns(1);
                        layout.setRows(3);
                        panel.setLayout(layout);
                        JProgressBar pbar = new JProgressBar();
                        pbar.setMinimum(0);
                        pbar.setMaximum(100);
                        panel.add(pbar);

                        JTextArea textArea = new JTextArea(1, 3);
                        textArea.setOpaque(false);
                        textArea.setEditable(false);
                        textArea.setText("Downloaded 00.00MB/00.00MB");
                        panel.add(textArea);

                        JFrame frame = new JFrame();
                        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                        frame.setContentPane(panel);
                        frame.pack();
                        frame.setLocationRelativeTo(null);
                        frame.setVisible(true);

                        while (!stop.get()) {
                            SwingUtilities.invokeLater(
                                    () -> pbar.setValue((int) (100.0 * total.get() / contentLength)));

                            textArea.setText("Downloaded " + bytesToMeg(total.get()) + "MB/"
                                    + bytesToMeg(contentLength) + "MB");
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException ignored) {
                            }
                        }
                        frame.dispose();
                    }
                };
                progressBar.start();

                while ((amnt = stream.read(buffer)) != -1) {
                    out.write(buffer, 0, amnt);
                    total.addAndGet(amnt);
                }
                stop.set(true);
                return loadHelios();
            } else {
                throw new IOException("Content-Length set to " + connection.getContentLength());
            }
        } else if (connection.getResponseCode() == 404) { // Most likely bootstrapper is out of date
            throw new RuntimeException("Bootstrapper out of date!");
        } else {
            throw new IOException(connection.getResponseCode() + ": " + connection.getResponseMessage());
        }
    }

    return data;
}

From source file:net.pms.external.ExternalFactory.java

private static void doUpdate(JLabel update, String text) {
    if (update == null) {
        return;/*w w w .  j a  va 2  s. co m*/
    }

    update.setText(text);
}

From source file:net.rptools.maptool.util.AssetExtractor.java

public static void extract() throws Exception {
    new Thread() {
        @Override//from   ww  w .j  av  a2  s. c  om
        public void run() {
            JFileChooser chooser = new JFileChooser();
            chooser.setMultiSelectionEnabled(false);
            chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            if (chooser.showOpenDialog(null) != JFileChooser.APPROVE_OPTION) {
                return;
            }
            File file = chooser.getSelectedFile();
            File newDir = new File(file.getParentFile(),
                    file.getName().substring(0, file.getName().lastIndexOf('.')) + "_images");

            JLabel label = new JLabel("", JLabel.CENTER);
            JFrame frame = new JFrame();
            frame.setTitle("Campaign Image Extractor");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setSize(400, 75);
            frame.add(label);
            SwingUtil.centerOnScreen(frame);
            frame.setVisible(true);
            Reader r = null;
            OutputStream out = null;
            PackedFile pakfile = null;
            try {
                newDir.mkdirs();

                label.setText("Loading campaign ...");
                pakfile = new PackedFile(file);

                Set<String> files = pakfile.getPaths();
                XStream xstream = new XStream();
                int count = 0;
                for (String filename : files) {
                    count++;
                    if (filename.indexOf("assets") < 0) {
                        continue;
                    }
                    r = pakfile.getFileAsReader(filename);
                    Asset asset = (Asset) xstream.fromXML(r);
                    IOUtils.closeQuietly(r);

                    File newFile = new File(newDir, asset.getName() + ".jpg");
                    label.setText("Extracting image " + count + " of " + files.size() + ": " + newFile);
                    if (newFile.exists()) {
                        newFile.delete();
                    }
                    newFile.createNewFile();
                    out = new FileOutputStream(newFile);
                    FileUtil.copyWithClose(new ByteArrayInputStream(asset.getImage()), out);
                }
                label.setText("Done.");
            } catch (Exception ioe) {
                MapTool.showInformation("AssetExtractor failure", ioe);
            } finally {
                if (pakfile != null)
                    pakfile.close();
                IOUtils.closeQuietly(r);
                IOUtils.closeQuietly(out);
            }
        }
    }.start();
}

From source file:components.ListDialogRunner.java

public static JPanel createUI() {
    //Create the labels.
    JLabel intro = new JLabel("The chosen name:");
    final JLabel name = new JLabel(names[1]);
    intro.setLabelFor(name);//from  ww w.  java2  s  . c o  m

    //Use a wacky font if it exists. If not, this falls
    //back to a font we know exists.
    name.setFont(getAFont());

    //Create the button.
    final JButton button = new JButton("Pick a new name...");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String selectedName = ListDialog.showDialog(frame, button, "Baby names ending in O:",
                    "Name Chooser", names, name.getText(), "Cosmo  ");
            name.setText(selectedName);
        }
    });

    //Create the panel we'll return and set up the layout.
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
    panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 10, 20));
    intro.setAlignmentX(JComponent.CENTER_ALIGNMENT);
    name.setAlignmentX(JComponent.CENTER_ALIGNMENT);
    button.setAlignmentX(JComponent.CENTER_ALIGNMENT);

    //Add the labels to the content pane.
    panel.add(intro);
    panel.add(Box.createVerticalStrut(5)); //extra space
    panel.add(name);

    //Add a vertical spacer that also guarantees us a minimum width:
    panel.add(Box.createRigidArea(new Dimension(150, 10)));

    //Add the button.
    panel.add(button);

    return panel;
}

From source file:ua.com.fielden.platform.example.swing.booking.BookingChartPanelExample.java

private static IBookingChartMouseEventListener<VehicleEntity, BookingEntity> createMouseListener(
        final JLabel taskBar, final Date now) {
    return new IBookingChartMouseEventListener<VehicleEntity, BookingEntity>() {

        private final Duration defaultDuration = Duration.standardDays(1);

        @Override//from  w  w  w  .j  a  v a 2 s .c  o  m
        public void mouseClick(final BookingMouseEvent<VehicleEntity, BookingEntity> event) {
            if (event.getTask() == null && event.getSourceEvent().getClickCount() == 2
                    && event.getX().after(now)) {
                addNewSubTask(event);
            } else if (event.getTask() != null) {
                taskBar.setText(
                        event.getTask().getEntity() + ", " + event.getTask().getSubEntity().getBookingStart());
            }
        }

        private void addNewSubTask(final BookingMouseEvent<VehicleEntity, BookingEntity> event) {
            final BookingTask<VehicleEntity, BookingEntity> firstBefore = event.getSource()
                    .getFirstTaskBefore(bookingSeries, event.getX(), event.getY());
            final BookingTask<VehicleEntity, BookingEntity> firstAfter = event.getSource()
                    .getFirstTaskAfter(bookingSeries, event.getX(), event.getY());
            final Date firstBeforeDate = firstBefore != null && firstBefore.getTo() != null
                    && firstBefore.getTo().after(now) ? firstBefore.getTo() : now;
            final Date firstAfterDate = firstAfter != null && firstAfter.getFrom() != null
                    ? firstAfter.getFrom()
                    : new DateTime(event.getX().getTime()).plus(defaultDuration).toDate();
            DateTime start = null;
            DateTime end = null;
            if (firstAfterDate.getTime() - firstBeforeDate.getTime() >= defaultDuration.getMillis()) {
                start = new DateTime(event.getX().getTime() - defaultDuration.getMillis() / 2);
                if (start.isBefore(firstBeforeDate.getTime())) {
                    start = new DateTime(firstBeforeDate.getTime());
                }
                end = start.plus(defaultDuration);
                if (end.isAfter(firstAfterDate.getTime())) {
                    end = new DateTime(firstAfterDate.getTime());
                    start = end.minus(defaultDuration);
                }
            } else {
                start = new DateTime(firstBeforeDate.getTime());
                end = new DateTime(firstAfterDate.getTime());
            }
            final BookingEntity booking = new BookingEntity().//
            setVehicleEntity(event.getSource().getEntity(event.getY())).//
            setBookingStart(start.toDate()).setBookingFinish(end.toDate());
            event.getSource().addTask(bookingSeries, event.getY(), booking);
        }

        @Override
        public void mouseMove(final BookingMouseEvent<VehicleEntity, BookingEntity> event) {
            // TODO Auto-generated method stub

        }

    };
}

From source file:edu.ku.brc.specify.plugins.ipadexporter.iPadDBExporterPlugin.java

/**
 * @param lbl//  ww w . ja v  a 2 s .  co m
 * @param text
 */
public static void setErrorMsg(final JLabel lbl, final String text) {
    lbl.setText(String.format("<html><font color='red'>%s</font>", text));
}

From source file:ua.com.fielden.platform.example.swing.schedule.ScheduleChartPanelExample.java

private static IScheduleChartMouseEventListener<AbstractEntity<?>> createMoseListener(final JLabel taskBar) {
    return new IScheduleChartMouseEventListener<AbstractEntity<?>>() {

        @Override//w  ww.j av  a  2 s.c o m
        public void mouseClick(final ScheduleMouseEvent<AbstractEntity<?>> event) {
            System.out.println("==========mouse clicked==========");
            System.out.println("Coordinates: " + event.getX() + " : " + event.getY());
            if (event.getEntity() != null) {
                System.out.println(
                        "Entity: " + event.getEntity() + " Series name: " + event.getSeries().getName());
            }
            System.out.println("=================================");

        }

        @Override
        public void mouseMove(final ScheduleMouseEvent<AbstractEntity<?>> event) {
            taskBar.setText("Coordinates: " + event.getX() + " : " + event.getY());
        }

    };
}

From source file:Main.java

public Main() {
    Icon icon = UIManager.getIcon("html.pendingImage");
    JTabbedPane jtb = new JTabbedPane();

    JPanel jplInnerPanel1 = createInnerPanel("Tab 1: Tooltip and Icon");
    jtb.addTab("One", icon, jplInnerPanel1, "Tab 1");
    jtb.setSelectedIndex(0);//from   w ww.j ava 2s . c  o m

    JPanel jplInnerPanel2 = createInnerPanel("Tab 2: Icon only");
    jtb.addTab("Two", icon, jplInnerPanel2);

    JPanel jplInnerPanel3 = createInnerPanel("Tab 3: Tooltip and Icon");
    jtb.addTab("Three", icon, jplInnerPanel3, "Tab 3");

    JPanel jplInnerPanel4 = createInnerPanel("Tab 4: Text only");
    jtb.addTab("Four", jplInnerPanel4);

    menu.add(new JMenuItem("Item 1"));
    menu.add(new JMenuItem("Item 2"));

    JLabel tab4Label = new JLabel();
    tab4Label.setText("Four");
    jtb.setTabComponentAt(3, tab4Label);
    tab4Label.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseReleased(MouseEvent e) {
            maybeShowPopup(e);
        }

        @Override
        public void mousePressed(MouseEvent e) {
            maybeShowPopup(e);
        }

        private void maybeShowPopup(MouseEvent e) {
            jtb.getModel().setSelectedIndex(3);
            if (e.isPopupTrigger()) {
                menu.show(e.getComponent(), e.getX(), e.getY());
            }
        }
    });
    setLayout(new GridLayout());
    add(jtb);
}