Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make JLabel Copyable?

Tags:

java

swing

jlabel

I have a question about making JLabels Copyable. I have a drop down menu which takes the imput and displays it as a JLabel, and the label changes when the menu changes. However, I want to make the JLabel copyable. I have heard this is not possible, so i changed the labels to strings and outputted it as a textarea. But when i do this, the string doesnt change when i select a new choice from the drop down menu. Im new to java so be as descriptive as possible please, thank you.

Here are some bits of my code. I really just need a way to make the JTextArea/JLabel copyable and have it be able to change

String[] players = {"Nearest Player", "All Players", "Random Player"};
JComboBox<String> player = new JComboBox<String>(players);
JLabel playernumb = new JLabel ("@p");
JLabel playerprompt = new JLabel("Target Player:");

JTextPane box = new JTextPane();

public static void main(String[] args) {
    mc frame = new mc();
    frame.setVisible(true);
}



player.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            if (e.getSource() == player){
                JComboBox temp1 = (JComboBox)e.getSource();
                String playertxt = (String)temp1.getSelectedItem();
                switch (playertxt){
                    case "Nearest Player": playernumb.setText("@p");
                        break;
                    case "All Players": playernumb.setText("@a");
                        break;
                    case "Random Player": playernumb.setText("@r");
                        break;
                    default: break;
                }
            }
        }
    });


    add(playerprompt);
    add(player);
    box.insertComponent(playernumb);
like image 859
user3371415 Avatar asked Jan 12 '23 02:01

user3371415


1 Answers

Just use this code fragment to make it look like a JTextPane look like a JLabel.

JTextPane f = new JTextPane();
f.setContentType("text/html"); // let the text pane know this is what you want
f.setEditable(false); // as before
f.setBackground(null); // this is the same as a JLabel
f.setBorder(null); // remove the border

Courtesy of this question: Selecting text from a JLabel?

like image 142
Skylion Avatar answered Jan 17 '23 23:01

Skylion