开发者

How to modify JLabel on JTabbedPane tab?

So say you had a JTabbedPane. And on each of those tabs, you have a JPanel with a JLabel, and a JButton. How can you modify that JLabel's text after it's already been added to the pa开发者_高级运维ne?


You can use jLabelN.setText("New Text"); as usual. It does not matter where the label is added to. You just need your variable jLabelN to be accessible.


You can easily write getters and setters for your GUI components like a (just some snippets)

public class A extends JPanel{

    JLabel aLabel=new JLabel();//field

    public A(){ 

     this.add(aLabel);

//GUI init here...
  }



    public void setLabelText(String text)
    {
      this.aLabel.setText(text);
    }

    public String getLabelText(){return this.aLabel.getText(); }
...
}

Lets call it now as...

public class B extends JFrame
{
A a=new A(); //
A a1=new A();

JTabbedPane tp=new JTabbedPane();

public B()
{

a.setLabelText("Hello World!");
a1.setLabelText("Hello World Again!");

tb.add("tab 0", a);
tb.add("tab 1", a1);
//    ...
}

public void actionPerformed(ActionEvent e)
{
  /*a button clicked...
    Lets get the tab label text value */

  JOptionPane.showMessageDialog(this,"tab 0 label text is: "+this.a.getLabelText());
  this.a.setLabelText("Have a good Java Coding");
}

}

Using getters and setters it is a standard way of objects data interaction

Good luck

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜