Add JComponent to applet's pane - java

I am trying to add an JComponent (label, for instance) to the applet pane when the button is pressed. I have the following piece of code:
public class TestApplet extends JApplet
{
#Override
public void init()
{
setLayout(new FlowLayout());
JButton bt = new JButton("hit it");
bt.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent ae)
{
// getContentPane().setFont(null);
getContentPane().add(new JLabel("to the right"));
}
});
add(bt);
}
}
This does not make the label visible, unless I uncomment getContentPane().setFont(null);
Please advise how should I display the label properly. Thanks in advance.

getContentPane().add(new JLabel("to the right"));
this.revalidate();

Related

Open another panel inside another panel after pressing button

I have been getting an error saying that i didn't add some methods(the action performed) but i already did. I'm having trouble opening the panel2.
public class panel1 extends JPanel implements ActionListener(){
private panel2 p2=new panel2();
private JButton button;
public panel1(){
button=new JButton("open panel2");
add(button,BorderLayout.BEFORE_FIRST_LINE);
button.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae) {
add(p2);
}
});
}
}
Consider these changes:
public class panel1 extends JPanel implements ActionListener(){
private panel2 p2=new panel2();
private JButton button;
public panel1(){
button=new JButton("open panel2");
add(button,BorderLayout.BEFORE_FIRST_LINE);
button.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae) {
//Add readability: Where to add?
panel1.this.add(p2);
}
});
}
//THIS HERE makes your panel1 implment ActionListener
#Override
public void actionPerformed(ActionEvent ae) {
}
}
Please also note commom Java naming conventions - Class names should start with uppercase letter (Panel1 extends JPanel)
You can check the below code for replacing jpanel without switching jframe.
contentPanel.removeAll();
contentPanel.repaint();
contentPanel.revalidate();
contentPanel.add(//add your panel here);
contentPanel.repaint();
contentPanel.revalidate();

How do i add an action to a JFrame JButton?

I have a JFrame gui im making and I need help.
Im trying to find out how to add a action to my button.
as in using it in a "if" statement or make i print something out when you push it.
thanks!
code:
package agui;
import javax.swing.*;
public class Agui extends JFrame {
public Agui() {
setTitle("My Gui");
setSize(400, 400);
JButton button = new JButton("click me");
JPanel panel = new JPanel();
panel.add(button);
this.getContentPane().add(panel);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Agui a = new Agui();
}
}
You want the "addActionListener" method, something like:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("You clicked the button");
}
});

how to paint images on clicking button in frame?

I've made two buttons in frame .I want to know how to display different images on clicking different buttons?
is there another way out or i have to make panel?I am at beginner stage
package prac;
import java.awt.*;
import java.awt.event.*;
public class b extends Frame implements ActionListener{
String msg;
Button one,two;
b()
{ setSize(1000,500);
setVisible(true);
setLayout(new FlowLayout(FlowLayout.LEFT));
one=new Button("1");
two=new Button("2");
add(one);
add(two);
one.addActionListener(this);
two.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
msg=e.getActionCommand();
if(msg.equals("1"))
{
msg="Pressed 1";
}
else
msg="pressed 2";
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,100,300);
}
public static void main(String s[])
{
new b();
}
}
Use JLabel and change the icon when button is clicked.
Some points:
call setVisible(true) in the end after adding all the component.
use JFrame#pack() method that automatically fit the components in the JFrame based on component's preferred dimension instead of calling JFrame#setSize() method.
sample code:
final JLabel jlabel = new JLabel();
add(jlabel);
final Image image1 = ImageIO.read(new File("resources/1.png"));
final Image image2 = ImageIO.read(new File("resources/2.png"));
JPanel panel = new JPanel();
JButton jbutton1 = new JButton("Show first image");
jbutton1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
jlabel.setIcon(new ImageIcon(image1));
}
});
JButton jbutton2 = new JButton("Show second image");
jbutton2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
jlabel.setIcon(new ImageIcon(image2));
}
});
panel.add(jbutton1);
panel.add(jbutton2);
add(panel, BorderLayout.NORTH);

adding a ScrollPane in a GridLayout doesn't resizes the JTextarea in a JPane when changing size of JFrame

I want to add a grid to the current GridLayout with a filled JTextArea component.
It works fine, but as mentioned in the title the JTextArea won't resizes itself
after changing the size of frame.
I have already localized the error:
text.setLineWrap(true); //<--- here is the problem!!enter code here
in the method createGui.
Here is my Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class GridLayoutExample {
private JFrame frame = new JFrame();
private JPanel panel = new JPanel();
private JScrollPane pane;
final JTextArea text = new JTextArea("Click me!\nClick me!\nClick me!\nClick me!\nClick me!");
GridLayoutExample() { createGui(); } //constrcutor
private JPanel createGui () {
panel.setLayout(new GridLayout(0, 2,0,0));
panel.add( new JLabel("Hello"));
//set style of first row, second column
text.setLineWrap(true); //<--- here is the problem!!
text.setEditable(true);
//enable mouse listening and allow mouse action
doMouseActions();
panel.add(text);
return panel;
}
void addNewRow() {
panel.add(new JLabel("Test1"));
panel.add(new JTextField("Test"));
}//addNewRow
void showFrame() {
pane = new JScrollPane(panel);
frame.add(pane);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}//showframe
void doMouseActions() {
text.addMouseListener(new MouseListener() {
#Override
public void mouseClicked(MouseEvent arg0) {
addNewRow();
}
#Override
public void mouseEntered(MouseEvent arg0) {
text.setBackground(Color.YELLOW);
}
#Override
public void mouseExited(MouseEvent arg0) {
text.setBackground(Color.WHITE);
}
#Override
public void mousePressed(MouseEvent arg0) {}
#Override
public void mouseReleased(MouseEvent arg0) {}
});
} // end of mouse action
public static void main(String[] args) {
GridLayoutExample test = new GridLayoutExample();
test.showFrame();
}// end of main
}// class GridLayoutExampleenter code here
On the one hand It works fine when I disable line wrapping, on the other hand I need
it to get the line breaks.
So any suggestions to do it better?
Many thanks in advance!
You are adding components at run time. In such cases you need to ensure the container gets revalidated:
void addNewRow() {
panel.add(new JLabel("Test1"));
panel.add(new JTextField("Test"));
panel.revalidate();
panel.repaint();
}//addNewRow
Also, you should create and access swing components only in the event dispatch thread. See here for more.

How to declare just one

I have the following working code with two buttons as event listeners. One button to get and display a Panel panel1 and the other to get and display another Panel panel2 removing the existing displayed panel. I created two actionPerformed methods for each button to carry out each other's tasks. I just want to make one to shorten the code but I don't know how to detect which button in a panel is displaying at compile time. Any help will be appreciated.
//Switching between panels (screens)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ScreenDemo extends JFrame {
private JPanel panel1, panel2;
private JButton btn1, btn2;
JLabel label1 = new JLabel("Screen 1");
JLabel label2 = new JLabel("Screen 2");
public ScreenDemo() {
createPanel(); //Created at Line 14
addPanel(); //Created at Line 28
}
private void createPanel() {
//Panel for first screen
panel1 = new JPanel(new FlowLayout());
btn1 = new JButton("Move to Screen 2");
btn1.addActionListener(new addScreen1ButtonListener());
//Panel for second screen
panel2 = new JPanel(new FlowLayout());
btn2 = new JButton("Move to Screen 1");
btn2.addActionListener(new addScreen2ButtonListener());
}
private void addPanel() {
panel1.add(label1);
panel1.add(btn1);
panel2.add(label2);
panel2.add(btn2);
add(panel1); //Add the first screen panel
}
class addScreen1ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
getContentPane().removeAll();
getContentPane().add(panel2);
repaint();
printAll(getGraphics()); //Prints all content
}
}
class addScreen2ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent ea) {
getContentPane().removeAll();
getContentPane().add(panel1);
repaint();
printAll(getGraphics()); //Prints all content
}
}
public static void main(String[] args) {
ScreenDemo screen = new ScreenDemo();
screen.setTitle("Switching Screens");
screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screen.setSize(500, 500);
screen.setVisible(true);
}
}
implement ActionListener for your class, and try this code
ScreenDemo extends JFrame implements ActionListener
...
btn1.addActionListener(this);
btn2.addActionListener(this);
...
#override
public void actionPerformed(ActionEvent ae) {
if(ae.getSource().equals(btn1)) {
...
}
else if(ae.getSource().equals(btn1)) {
...
}
}
you have to implements the class from ActionListener
than register JButton like : btn.addActionListener(this);
so close now, You just need to capture the event and get the source from where it is called like:
public void actionPerformed(ActionEvent ae) {
if(ae.getSource == btn1){
//shuffle panel from btn1
}
if(ae.getSource == btn2) {
//shuffle panel from btn2
}
}

Categories

Resources