Question

I tried to add Color to java.awt.Font's attributes like this:

font.getAttributes().put(TextAttribute.FOREGROUND, jColorChooser.getColor());

But I get the error

The method put(TextAttribute, capture#12-of ?) in the type Map is not applicable for the arguments (TextAttribute, Color)

The Font API says

This Font only recognizes keys defined in TextAttribute as attributes And FOREGROUND is a present in TextAttribute

Am I doing something wrong?

Was it helpful?

Solution

In fact, to change a font, you can't change directly its attributes, as Swing fonts are supposed to be immutable.

As a consequence, you have to call its Font#deriveFont(Map) method with a new attribute set. This will create a new font with the given attribute set and, as a consequence, new color.

OTHER TIPS

package fileHandling;

import java.io.File;

import javax.swing.JFileChooser;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

import com.lowagie.text.Font;

public class CreateNFiles extends JFrame implements ActionListener
{
    JLabel lblFileName,lblNoOfPagesInPdf,lblPathToSave,lblExtension,lblError,lblHeading;
    JTextField txtFileName,txtNoOfPagesInPdf,txtPathToSave,txtExtension;
    JButton btnSubmit,btnBrowse;
    JPanel mainPanel,gridPanel;
    GridBagConstraints gbc;
    JDialog jdBrowse;
    int total;
    String absoluteFileName;
    String fileName;

java.awt.Font textFont;

CreateNFiles()
{
    setLayout(new FlowLayout());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500,400);

    setLocationRelativeTo(null);
    setVisible(true);

    initializeObjects();
    addObjectsToFrame(this);
}

public void initializeObjects()
  {
    textFont=new java.awt.Font("TimesRoman",Font.BOLD + Font.ITALIC,15);

        lblFileName=new JLabel("File Name : ");
        lblFileName.setFont(textFont);
        lblNoOfPagesInPdf=new JLabel("No of Pages : ");
        lblNoOfPagesInPdf.setFont(textFont);
        lblPathToSave=new JLabel("Path To Save : ");
        lblPathToSave.setFont(textFont);
        lblExtension=new JLabel("Extension : ");
        lblExtension.setFont(textFont);
        lblError=new JLabel("");
        lblError.setVisible(false);
        lblHeading=new JLabel("~~ Blank File Automatic ~~");
        lblHeading.setFont(textFont);
        lblHeading.setForeground(Color.BLUE);
        lblHeading.setVisible(true);

        txtFileName=new JTextField(10);
        txtNoOfPagesInPdf=new JTextField(10);
        txtPathToSave=new JTextField(20);
        txtExtension=new JTextField(10);

        btnSubmit=new JButton("Submit");
        btnSubmit.addActionListener(this);
        btnBrowse=new JButton("Browse");
        btnBrowse.addActionListener(this);

        mainPanel=new JPanel(new BorderLayout());
        gridPanel=new JPanel(new GridBagLayout());

        gbc=new GridBagConstraints();
        gbc.insets=new Insets(10,10,10,10);

        jdBrowse=new JDialog();

  }
public void addObjectsToFrame(CreateNFiles frame)
{
      frame.setLayout(new FlowLayout());

      gbc.gridx=0;
      gbc.gridy=0;
      gridPanel.add(lblFileName,gbc);

      gbc.gridx=1;
      gbc.gridy=0;
      gridPanel.add(txtFileName,gbc);

      gbc.gridx=0;
      gbc.gridy=1;
      gridPanel.add(lblNoOfPagesInPdf,gbc);

      gbc.gridx=1;
      gbc.gridy=1;
      gridPanel.add(txtNoOfPagesInPdf,gbc);

      gbc.gridx=0;
      gbc.gridy=2;
      gridPanel.add(lblPathToSave,gbc);

      gbc.gridx=1;
      gbc.gridy=2;
      gbc.gridwidth=2;
      gridPanel.add(txtPathToSave,gbc);

      gbc.gridx=3;
      gbc.gridy=2;
      gbc.gridwidth=1;
      gridPanel.add(btnBrowse,gbc);

      gbc.gridx=0;
      gbc.gridy=3;
      gridPanel.add(lblExtension,gbc);

      gbc.gridx=1;
      gbc.gridy=3;
      gridPanel.add(txtExtension,gbc);

      gbc.gridx=1;
      gbc.gridy=4;
      gridPanel.add(btnSubmit,gbc);

      gbc.gridx=0;
      gbc.gridy=5;
      gbc.gridwidth=4;
      gridPanel.add(lblError,gbc);

      frame.add(lblHeading,BorderLayout.PAGE_START);
      mainPanel.add(gridPanel,BorderLayout.CENTER);
      frame.add(gridPanel);
      frame.setVisible(true);
      frame.setLocationRelativeTo(null);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
  public static void main(String[] args) 
  {
      try
        {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        }
        catch(Exception e)
        {
            System.out.println("Exception : "+e);
        }
          CreateNFiles obj=new CreateNFiles();


  }
    public void actionPerformed(ActionEvent ae) 
    {
         if (ae.getActionCommand().equals("Submit")) 
        {
          System.out.println("Button1 has been clicked");
          process();
        }

     else if (ae.getActionCommand().equals("Browse")) 
    {
        System.out.println("Button1 has been clicked");
        try
        {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setCurrentDirectory(new java.io.File("."));
            fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            fileChooser.setAcceptAllFileFilterUsed(false);

            int returnValue = fileChooser.showOpenDialog(null);
            if (returnValue == JFileChooser.APPROVE_OPTION) 
            {
                File selectedFile = fileChooser.getSelectedFile();
                txtPathToSave.setText(fileChooser.getSelectedFile().getAbsolutePath());
                System.out.println(fileChooser.getCurrentDirectory());
            }

        }
        catch(Exception ex) 
        {
            System.out.println(ex.getMessage());
        }

    }
}

public void process()
{
    try
    {
        String ext=null;
        String path;
        fileName=txtFileName.getText();
        total=Integer.parseInt(txtNoOfPagesInPdf.getText());   
        ext=txtExtension.getText();
        path=txtPathToSave.getText();

          for(int i=0;i<total*2;i++)
          {
              if(i%2==0)
              {
                  absoluteFileName=path+"\\"+fileName+(i/2)+'.'+ext;
              }
              else
              {
                  absoluteFileName=path+"\\"+fileName+(i/2)+"A"+"."+ext;
              }
              File file=new File(absoluteFileName);
              try
              {
                  file.createNewFile();
              }
              catch(Exception e)
              {
                  System.out.println(e.getMessage());
              }
              System.out.println("Parent :: "+file.getAbsolutePath());
              System.out.println("Parent :: "+file.getParent());
          }
    }
    catch(Exception ae)
    {
        System.out.println(ae.getMessage());
        lblError.setVisible(true);
        lblError.setText("Error :- "+ae.getMessage());
        lblError.setFont(textFont);
        lblError.setForeground(Color.red);
    }

}

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top