can any one tell me how to add a panel in a jtabbedPane whenever i am clicking on a "add" button.Its like google chrome new tab.But the thing is ,the generated panel must contains some default components.Thanks in advance.
Please see the code below. It shows you how to do what you need.
public class DemoApp {
private JTabbedPane tabPane = new JTabbedPane();
public DemoApp() {
initComponents();
}
private void initComponents() {
JFrame frame = new JFrame("Test");
frame.setSize(500, 400);
frame.setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
frame.getContentPane().add(panel);
JButton btn = new JButton("Add panel");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int index = tabPane.getTabCount() + 1;
JPanel newPanel = new JPanel();
newPanel.setLayout(new FlowLayout());
newPanel.add(new JLabel("Panel " + index));
tabPane.addTab("Tab " + index, newPanel);
}
});
panel.add(tabPane, BorderLayout.CENTER);
panel.add(btn, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new DemoApp();
}
}
Related
I am newbie to Java. My following code give me a blank window.
Anyone can help me with what is going on ?
I am thinking that the error is at the ActionListeners.
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("Listeners");
frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLayout(new BorderLayout());
JTextArea txtArea = new JTextArea();
HelloActionListener hlisten = new HelloActionListener(txtArea);
JButton bl = new JButton("TOP");
bl.addActionListener((ActionListener) hlisten);
JButton b2 = new JButton("LEFT");
ActionListener rightListener = (ActionEvent e) -> {
txtArea.setText("Yes,let's go Left");
};
b2.addActionListener(rightListener);
JButton b3 = new JButton("RIGHT");
b3.addActionListener((ActionEvent e) -> {
txtArea.setText("Sorry, we cant go Right");
});
JButton b4 = new JButton("Bottom");
b4.addActionListener((ActionListener) hlisten);
frame.add(bl, BorderLayout.PAGE_START);
frame.add(b2, BorderLayout.LINE_START);
frame.add(b3, BorderLayout.LINE_END);
frame.add(b4, BorderLayout.PAGE_END);
frame.add(txtArea, BorderLayout.CENTER);
frame.setVisible(true);
}
Say you want to put "Hello" in your text area when the top button is pressed. Then you need to define your HelloActionListener as below:
private static class HelloActionListener implements ActionListener {
private JTextArea textArea;
public HelloActionListener(JTextArea textArea) {
this.textArea = textArea;
}
#Override
public void actionPerformed(ActionEvent e) {
textArea.setText("Hello");
}
}
The rest of your code is kind of messy but should work, however, you don't need to make the frame to be visible twice.
UPDATE
But I do think your code should be something like this:
public class MyApplication extends JFrame {
public MyApplication() {
setTitle("Listeners");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JTextArea txtArea = new JTextArea();
HelloActionListener hlisten = new HelloActionListener(txtArea);
JButton bl = new JButton("TOP");
bl.addActionListener(hlisten);
JButton b2 = new JButton("LEFT");
ActionListener rightListener = e -> {
txtArea.setText("Yes,let's go Left");
};
b2.addActionListener(rightListener);
JButton b3 = new JButton("RIGHT");
b3.addActionListener(e -> {
txtArea.setText("Sorry, we cant go Right");
});
JButton b4 = new JButton("Bottom");
b4.addActionListener(hlisten);
add(bl, BorderLayout.PAGE_START);
add(b2, BorderLayout.LINE_START);
add(b3, BorderLayout.LINE_END);
add(b4, BorderLayout.PAGE_END);
add(txtArea, BorderLayout.CENTER);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new MyApplication();
frame.pack();
frame.setVisible(true);
});
}
private class HelloActionListener implements ActionListener {
private JTextArea textArea;
public HelloActionListener(JTextArea textArea) {
this.textArea = textArea;
}
#Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
textArea.setText("Hello from " + button.getText());
}
}
}
There is no need for the first frame.setVisible(true); in your code - you are calling that again right at the end. But your panels are all zero size. You need to call frame.pack(); before you set it visible. Note also that all of that (the GUI stuff) should be done from the Event Dispatch Thread, not from your main thread.
Hey everyone, I want to combine my classes and get it in
only one frame. Now I have 2 classes and I don't know how to group them.
The JSlider.
public class JSliderExample extends JFrame {
JSlider jsHorizontal;
JTextField jtf1;
public JSliderExample() {
jsHorizontal = new JSlider(JSlider.HORIZONTAL, 0, 100, 50);
jtf1 = new JTextField(15);
jtf1.setEditable(false);
jtf1.setText("Horizontal value is " + jsHorizontal.getValue());
JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
panel.add(jsHorizontal);
panel.setBackground(Color.WHITE);
panel.add(jtf1);
panel.setBackground(Color.WHITE);
getContentPane().add(panel, BorderLayout.CENTER);
getContentPane().add(panel, BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(300, 400, 400, 300);
setVisible(true);
setBackground(Color.WHITE);
}
class JSliderHandler implements ChangeListener {
public void stateChanged(ChangeEvent ce) {
jtf1.setText("value is " + jsHorizontal.getValue());
}
}
And there are my buttons
.
public void createGUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JButton button2 = new JButton("PLAY");
button2.setActionCommand("Button PLAY was pressed!");
panel.add(button2);
textField = new JTextField();
textField.setColumns(23);
panel.add(textField);
ActionListener actionListener = new TestActionListener();
button1.addActionListener(actionListener);
button2.addActionListener(actionListener);
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(e.getActionCommand());
}
});
getContentPane().add(panel);
setPreferredSize(new Dimension(320, 100));
}
public class TestActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
textField.setText(e.getActionCommand());
}
}
In the end of programm I see 2 frames that consist of 2 classes.
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
TestFrame frame = new TestFrame();
frame.pack();
JSliderExample frame1 = new JSliderExample();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
If you don't want to see 2 JFrames, then don't create 2 JFrames. Why not make JPanels with all your classes above and not JFrames, and then in your main method, add your JPanels to the JFrame created within main. Simple.
So for example, instead of having JSliderExample extend JFrame, change it's name to SliderPanel and have it extend JPanel, and likewise with your JButton program. Then your main method could look something like:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
// your JSlider example class **that extends JPanel**
SliderPanel sliderPanel = new SliderPanel();
// your JButton example class **that extends JPanel**
ButtonPanel buttonPanel = new ButtonPanel():
JFrame frame = new JFrame("My GUI");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(sliderPanel, BorderLayout.PAGE_START);
frame.add(buttonPanel, BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null); // center GUI if you want
frame.setVisible(true);
}
});
}
For some reason I can't get the BorderLayout to set the way it's supposed to. Just would like to know where I'm going wrong.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ColorFactory extends JFrame
{
final int width = 500;
final int height = 300;
private JPanel buttonPanel;
private JPanel radioButtonPanel;
private JLabel msgChangeColor;
public ColorFactory()
{
setTitle("Color Factory");
setSize(width, height);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
createTopPanel();
add(buttonPanel, BorderLayout.NORTH);
createBottomPanel();
add(radioButtonPanel, BorderLayout.SOUTH);
msgChangeColor = new JLabel("Top buttons change the panel color and bottom radio buttons change the text color.");
add(msgChangeColor, BorderLayout.CENTER);
pack();
}
private void createTopPanel()
{
buttonPanel = new JPanel();
setLayout(new FlowLayout());
JButton redButton = new JButton("Red");
redButton.setBackground(Color.RED);
redButton.addActionListener(new ButtonListener());
redButton.setActionCommand("R");
JButton orangeButton = new JButton("Orange");
orangeButton.setBackground(Color.ORANGE);
orangeButton.addActionListener(new ButtonListener());
orangeButton.setActionCommand("O");
JButton yellowButton = new JButton("Yellow");
yellowButton.setBackground(Color.YELLOW);
yellowButton.addActionListener(new ButtonListener());
yellowButton.setActionCommand("Y");
buttonPanel.add(redButton);
buttonPanel.add(orangeButton);
buttonPanel.add(yellowButton);
}
private void createBottomPanel()
{
radioButtonPanel = new JPanel();
setLayout(new FlowLayout());
JRadioButton greenRadioButton = new JRadioButton("Green");
greenRadioButton.setBackground(Color.GREEN);
greenRadioButton.addActionListener(new RadioButtonListener());
greenRadioButton.setActionCommand("G");
JButton blueRadioButton = new JButton("Blue");
blueRadioButton.setBackground(Color.BLUE);
blueRadioButton.addActionListener(new RadioButtonListener());
blueRadioButton.setActionCommand("B");
JButton cyanRadioButton = new JButton("Cyan");
cyanRadioButton.setBackground(Color.CYAN);
cyanRadioButton.addActionListener(new RadioButtonListener());
cyanRadioButton.setActionCommand("C");
radioButtonPanel.add(greenRadioButton);
radioButtonPanel.add(blueRadioButton);
radioButtonPanel.add(cyanRadioButton);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String actionColor = e.getActionCommand();
if(actionColor.equals("R"))
{
buttonPanel.setBackground(Color.RED);
radioButtonPanel.setBackground(Color.RED);
}
if(actionColor.equals("O"))
{
buttonPanel.setBackground(Color.ORANGE);
radioButtonPanel.setBackground(Color.ORANGE);
}
if(actionColor.equals("Y"))
{
buttonPanel.setBackground(Color.YELLOW);
radioButtonPanel.setBackground(Color.YELLOW);
}
}
}
private class RadioButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String actionTextColor = e.getActionCommand();
if(actionTextColor.equals("G"))
{
msgChangeColor.setForeground(Color.GREEN);
}
if(actionTextColor.equals("B"))
{
msgChangeColor.setForeground(Color.BLUE);
}
if(actionTextColor.equals("C"))
{
msgChangeColor.setForeground(Color.CYAN);
}
}
}
public static void main(String[] args)
{
ColorFactory run = new ColorFactory();
run.setVisible(true);
}
}
The problem is you are changing the layout manager for the frame when you create your top and bottom panels...
private void createTopPanel() {
buttonPanel = new JPanel();
setLayout(new FlowLayout()); // <--- This is call setLayout on the frame
This is why it's dangerous to...
Extend from something like JFrame directly...
Dynamically build components
It's all to easy to lose context and start effecting components you didn't actually want to...
Another problem (besides the one posted by MadProgrammer) is that you add your components to the JFrame itself.
You should add content to the content pane of the frame which you can get by calling JFrame.getContentPane().
Example:
JFrame f = new JFrame("Test");
Container c = f.getContentPane();
c.add(new JButton("In Center"), BorderLayout.CENTER);
c.add(new JButton("At the Bottom"), BorderLayout.SOUTH);
c.add(new JButton("At the Top"), BorderLayout.NORTH);
c.add(new JButton("On the Left"), BorderLayout.WEST);
c.add(new JButton("On the Right"), BorderLayout.EAST);
You can set/change the content panel by calling JFrame.setContentPane(). The default content panel already has BorderLayout so you don't even need to change it nor to set a new panel.
I'm building my first Java swing GUI, but I cannot manage to have the layout as I wanted.
Can you help me to understand how the layout should be set to have the desired result (see image below)?
This is what I get (wrong):
and if I resize it manually I get the desired result:
Here is my code:
public class MainClass implements Runnable {
private JButton load = new JButton("Load..");
private JButton save = new JButton("Save..");
private JButton clear = new JButton("Clear");
private JLabel displayFile = new JLabel();
List<String> lines;
JFrame frame = new JFrame("SimpleDrawing");
public static void main(String[] args) {
MainClass maincl = new MainClass();
SwingUtilities.invokeLater(maincl);
}
#Override
public void run() {
DrawingArea area = new DrawingArea();
frame.setLayout(new FlowLayout());
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
//buttonPane.add(Box.createHorizontalGlue());
buttonPane.add(load);
//buttonPane.add(Box.createRigidArea(new Dimension(5,0)));
buttonPane.add(save);
//buttonPane.add(Box.createRigidArea(new Dimension(5,0)));
buttonPane.add(clear);
buttonPane.add(displayFile);
frame.add(buttonPane, BorderLayout.PAGE_START);
frame.add(area, BorderLayout.CENTER);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Your Frame has the FlowLayout.
frame.setLayout(new FlowLayout());
Give it a BorderLayout.
frame.setLayout(new BorderLayout());
Layout- Informations:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
The code : The user enters a value and the code returns the computed value in a frame with a scrollbar because the frame is too small to contain the 100 lines.
package Gui;
import java.awt.*;
import javax.swing.*;
public class Rekenen2 extends JFrame {
public Rekenen2() {
// setLayout(new GridLayout(3,1));
JPanel panel1 = new JPanel();
panel1.setBackground(Color.BLACK);
JButton jbtComputeButton = new JButton("Compute");
JPanel panel2 = new JPanel();
panel2.setBackground(Color.BLACK);
JPanel panel3 = new JPanel();
final JTextField jtxInputTextField = new JTextField(8);
final JLabel outputInPanel = new JLabel();
panel1.add(jbtComputeButton, FlowLayout.LEFT);
panel2.add(jtxInputTextField, FlowLayout.LEFT);
panel3.add(outputInPanel);
add(panel1, BorderLayout.NORTH);
add(panel2, BorderLayout.CENTER);
add(panel3, BorderLayout.SOUTH);
jbtComputeButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
int total = 0;
int parsedInputValue = Integer.parseInt(jtxInputTextField
.getText());
for (int i = 0; i < 100; i++) {
total = (parsedInputValue * i);
outputInPanel.setText("" + total);
}
}
});
}
public static void main(String[] args) {
JFrame frame = new Rekenen2();
frame.setSize(300, 300);
frame.setTitle("Compute App");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
Just put the content in JScrollPane(then add the JScrollPane To JFrame). that will do the trick.