2

I want to change my JLabel when I click a JButton. It sounds simple but can't really find a good piece of code. Here is part of my code:

                    final JButton continueGame = new JButton();
                    continueGame.setPreferredSize(new Dimension(1000, 30)); 
                    continueGame.setLocation(95, 45);
                    continueGame.setText("<html>Continue</html>");
                    continueGame.addActionListener(new ActionListener(){
                        @Override
                        public void actionPerformed(ActionEvent ev) {
                            panel.remove(continueGame);
                            SwingUtilities.updateComponentTreeUI(frameKontrastGame);
                                if(RandomNrJeden <= 50)
                                {
                                    JOptionPane.showMessageDialog(frameKontrastGame, "Eggs are not supposed to be green.");
                                    frameKontrastGame.setVisible(false);

                                    JFrame frameScenario2 = new JFrame();
                                    frameScenario2.setTitle("Scenario2");
                                    frameScenario2.setSize(1000,700);
                                    frameScenario2.setLocationRelativeTo(null);
                                    frameScenario2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


                                    JPanel panelScenario = new JPanel ();
                                    panel.setLayout(new GridLayout(2, 1));




                                    final JLabel tekst = new JLabel ();
                                    tekst.setText("<html>Część dialogu numer 1</html>");
                                    //JTextField dialog = new JTextField(20);
                                    //dialog.setText("<html>Eggs are not supposed to be green.</html>");


                                    JButton OdpPierwsza = new JButton ();
                                    OdpPierwsza.setText("<html>Opowiedź pierwsza</html>");
                                    OdpPierwsza.addActionListener (new ActionListener(){
                                        @Override
                                        public void actionPerformed(ActionEvent ev) {
                                        tekst.setText("<html>HERE I NEED A TEXT FROM FILE dialog.txt</html>");


                                        }
                                        });


                                    //panelScenario.add(dialog);
                                    panelScenario.add(tekst);
                                    panelScenario.add(OdpPierwsza);
                                    frameScenario2.getContentPane().add(panelScenario);

                                    frameScenario2.setVisible(true);

                                }

(If the brackets are wrong that's because its not the whole code.)

So:

  • Where is the "HERE I NEED A TEXT FROM FILE dialog.txt" I need some kind of reader. The best one will be the one which reads text line by line. I just cant find how to write it.
  • I need to add the JLabel to the JPanel.
tshepang
  • 12,111
  • 21
  • 91
  • 136
Akarasu
  • 45
  • 3
  • 7
  • Did you try with BufferedReader? – SSC Jun 03 '13 at 11:45
  • 1
    just search "java read text from file" and you will find many answers and examples here on SO and in any search engine you choose. here is one post. check the accepted answer http://stackoverflow.com/questions/16104616/using-bufferedreader-to-read-text-file –  Jun 03 '13 at 11:46
  • 1
    You need to read the file with a BufferedReader br = new BufferedReader(new FileReader("your_file.txt") and read it line by line: String line; line = br.readLine(); while(line != null){ line = br.readLine; //and add the line to your label (better is a text area)} – marc3l Jun 03 '13 at 11:46

1 Answers1

1

You can read your file into just one string using

BufferedReader br = new BufferedReader(new FileReader("your_file.txt"));
String line = br.readLine();
ArrayList<String> listOfStrings = new ArrayList<>();
listOfString.add(line);

while(line != null)
{
   line = br.readLine();
   listOfString.add(line);
}

And now do a for-loop to iterate over the JList and add text to the JLabel. Better is a JTextArea.

marc3l
  • 2,525
  • 7
  • 34
  • 62