positioning jpanel at the bottom - java

I want to position the JPanel that contains the send button and the textfield (and right now uses a flowlayout) at the bottom of the JTextArea (The white area).
How can I achieve this?
public GUI()
{
mainWindow = new JFrame("Chat GUI");
lowerPanel = new JPanel(new FlowLayout());
usersPanel = new JPanel(new GridLayout(GRIDLAYOUT_ROWS, GRIDLAYOUT_COLS));
users = new JList(data);
usersPanel.add(users);
sendButton = new JButton("Send");
textField = new JTextField(TEXTFIELD_WIDTH);
textArea = new JTextArea(TEXTAREA_HEIGHT, TEXTAREA_WIDTH);
textArea.setEditable(false);
}
private void addButtonListener(JButton b) {
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
}
public void createGUI() {
addButtonListener(sendButton);
lowerPanel.add(sendButton);
lowerPanel.add(textField);
mainWindow.add(users);
mainWindow.add(textArea);
mainWindow.add(lowerPanel);
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindow.setVisible(true);
mainWindow.setLayout(new FlowLayout());
mainWindow.pack();
}

You Will have Layout :
this.add(buttonPanel,BorderLayout.SOUTH);
see this answer

Related

JRadioButton toggle unexpectedly in CardLayout panels

So the problem is: I'm trying to make a wizard-like CardLayout. In each card panel, I put back & next JButton and 3 JRadioButton to switch between 3 pages.
Now, when I select the radio buttons the 1st time, it works normally. However, the 2nd time I select the radio button, they don't get selected as expected. For example, I want to select page 2, the card panel 2 does show up, but the radio button 2 state does not show that it's being selected, instead either radio button 1 or 3 gets selected. Button 2 only gets selected when I click it again. Same thing happens when I try to select the others.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutWizardDemo extends JFrame{
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
CardLayoutWizardDemo frame= new CardLayoutWizardDemo();
frame.init();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
private static final long serialVersionUID = 1L;
private JPanel cardPanel, panel1, panel2, panel3, btnPanel1, btnPanel2, btnPanel3;
private JLabel label1, label2, label3;
private JRadioButton step1, step2, step3;
private ButtonGroup bg;
private CardLayout cl = new CardLayout();
private void init(){
setTitle("CardLayoutWizardDemo");
cardPanel = new JPanel();
cardPanel.setLayout(cl);
panel1 = new JPanel(new BorderLayout());
panel2 = new JPanel(new BorderLayout());
panel3 = new JPanel(new BorderLayout());
label1 = new JLabel("label 1");
label2 = new JLabel("label 2");
label3 = new JLabel("label 3");
panel1.add(label1, BorderLayout.NORTH);
panel2.add(label2, BorderLayout.NORTH);
panel3.add(label3, BorderLayout.NORTH);
btnPanel1 = new JPanel();
btnPanel2 = new JPanel();
btnPanel3 = new JPanel();
btnPanel1.setName("panel1");
btnPanel2.setName("panel2");
btnPanel3.setName("panel3");
btnPanel1 = initTutBtn(btnPanel1);
btnPanel2 = initTutBtn(btnPanel2);
btnPanel3 = initTutBtn(btnPanel3);
panel1.add(btnPanel1, BorderLayout.SOUTH);
panel2.add(btnPanel2, BorderLayout.SOUTH);
panel3.add(btnPanel3, BorderLayout.SOUTH);
cardPanel.add(panel1, "1");
cardPanel.add(panel2,"2");
cardPanel.add(panel3,"3");
getContentPane().add(cardPanel, BorderLayout.CENTER);
setPreferredSize(new Dimension(350,500));
setMinimumSize(new Dimension(240,320));
pack();
setLocationByPlatform(true);
}
/**create new set of 3 step buttons
*/
private JPanel initTutBtn(JPanel btnPanel){
btnPanel.setLayout(new BoxLayout(btnPanel,BoxLayout.X_AXIS));
step1 = new JRadioButton();
step2 = new JRadioButton();
step3 = new JRadioButton();
step1.setActionCommand("step1");
step2.setActionCommand("step2");
step3.setActionCommand("step3");
bg = new ButtonGroup();
bg.add(step1);
bg.add(step2);
bg.add(step3);
if (btnPanel.getName().equals("panel1")){
step1.setSelected(true);
}else if (btnPanel.getName().equals("panel2")){
step2.setSelected(true);
}else if (btnPanel.getName().equals("panel3")){
step3.setSelected(true);
}
step1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
goToStep(e);
}
});
step2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
goToStep(e);
}
});
step3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
goToStep(e);
}
});
btnPanel.add(step1);
btnPanel.add(step2);
btnPanel.add(step3);
return btnPanel;
}
private void goToStep(ActionEvent evt){
if(evt.getActionCommand().equals("step1")){
cl.show(cardPanel, "1");
}else if(evt.getActionCommand().equals("step2")){
cl.show(cardPanel, "2");
}else if(evt.getActionCommand().equals("step3")){
cl.show(cardPanel, "3");
}
}
}
I think maybe the problems lie where I create new radio buttons within initButton() and goToStep(ActionEvent evt)but I can't figure out what I did wrong

how to identify dynamically created jbutton actions

I create two jbuttons in one panel(can be Box).i create same panel dynamically several times in same frame.so if two panels created dynamically those button make with same variable name.But i want to identify buttons one by one for put actions.how to identify dynamically created buttons one by one?
button creating code
public class Jscrollpanetest extends JFrame {
JScrollPane scrollPane;
Box box;
private static int panelCount = 0;
public Jscrollpanetest() {
setPreferredSize(new Dimension(200, 400));
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
scrollPane = new JScrollPane();
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.getVerticalScrollBar().setUnitIncrement(15);
box = Box.createVerticalBox();
scrollPane.getViewport().add(box);
this.add(scrollPane);
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
Timer t = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent ae) {
box.add(new TestPanel(), box.size());
scrollPane.validate();
}
});
t.setRepeats(true);
t.start();
}
public class TestPanel extends JPanel {
int myId = panelCount++;
public TestPanel() {
this.setLayout(new GridBagLayout());
this.setBorder(BorderFactory.createBevelBorder(1));
JButton up = new JButton("^");
JLabel rate = new JLabel("1");
JButton down = new JButton("^");
JLabel label = new JLabel("" + myId);
label.setHorizontalAlignment(JLabel.CENTER);
label.setVerticalAlignment(JLabel.CENTER);
this.setMaximumSize(new Dimension(1000, 200));
this.setPreferredSize(new Dimension(1000, 100));
this.add(label);
this.add(up);
this.add(rate);
this.add(down);
}
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Jscrollpanetest testScrollPane = new Jscrollpanetest();
}
});
}
}
If use and dynamic created ActionListener there will be no problem. So each button will have its own ActionListener.
If using a common ActionListener must add tags to each button as extend JButton.

BorderLayout not working JFrame

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.

How can I align RadioButtons in a JSplitPane?

I am trying to align the RadioButtons in my program here. Can someonee help me? I am not exactly sure how to go about it. I have searched here and elsewhere and cannnot seem to find an applicable example. Here is, what I believe the relevant portion of my code. If more is needed, I will edit.
final class SplitPanel extends JFrame {
private FlowLayout flowLayout = new FlowLayout();
private GridLayout gridLayout = new GridLayout(4, 1);
private DiagLayout diagLayout = new DiagLayout();
private JRadioButton jrbFlowLayout = new JRadioButton("Horizontal");
private JRadioButton jrbGridLayout = new JRadioButton("Verticle");
private JRadioButton jrbDiagLayout = new JRadioButton("Diagonal");
private JButton jbt1 = new JButton("Button 1");
private JButton jbt2 = new JButton("Button 2");
private JButton jbt3 = new JButton("Button 3");
private JButton jbt4 = new JButton("Button 4");
private JSplitPane jSplitPane;
private JPanel jPanel1, jPanel2;
public SplitPanel() {
this.setTitle("Split Panel with Diagonalization");
this.setSize(600, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createPanel1();
createPanel2();
jSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
jPanel1, jPanel2);
jSplitPane.setOneTouchExpandable(true);
jSplitPane.setContinuousLayout(true);
jSplitPane.setDividerLocation(150);
getContentPane().add(jSplitPane);
}
public void createPanel1() {
jPanel1 = new JPanel();
jPanel1.setBorder(new TitledBorder("Select a Layout Manger"));
jPanel1.add(jrbFlowLayout);
jPanel1.add(jrbGridLayout);
jPanel1.add(jrbDiagLayout);
ButtonGroup buttonGroup1 = new ButtonGroup();
buttonGroup1.add(jrbFlowLayout);
buttonGroup1.add(jrbGridLayout);
buttonGroup1.add(jrbDiagLayout);
}
public void createPanel2() {
jPanel2 = new JPanel();
jPanel2.setLayout(diagLayout);
jPanel2.add(jbt1);
jPanel2.add(jbt2);
jPanel2.add(jbt3);
jPanel2.add(jbt4);
jrbFlowLayout.addActionListener (new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
jPanel2.setLayout(flowLayout);
jPanel2.validate();
}
});
jrbGridLayout.addActionListener (new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
jPanel2.setLayout(gridLayout);
jPanel2.validate();
}
});
jrbDiagLayout.addActionListener (new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
jPanel2.setLayout(diagLayout);
jPanel2.validate();
}
});
}
Thank you for your help!
Try a different layout manager for your jPanel1
public void createPanel1() {
jPanel1 = new JPanel();
//jPanel1.setLayout(new GridLayout(0, 1);
jPanel1.setLayout(new BoxLayout(jPanel1, BoxLayout.Y_AXIS));
jPanel1.setBorder(new TitledBorder("Select a Layout Manger"));
jPanel1.add(jrbFlowLayout);
jPanel1.add(jrbGridLayout);
jPanel1.add(jrbDiagLayout);
ButtonGroup buttonGroup1 = new ButtonGroup();
buttonGroup1.add(jrbFlowLayout);
buttonGroup1.add(jrbGridLayout);
buttonGroup1.add(jrbDiagLayout);
}
GridLayout...
BoxLayout...
I'd almost suggest GridBagLayout, but that's probably overkill in this case...
The default layout of a JPanel is a FlowLayout.
http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html
You might want to set the panels layout to a GridLayout or even a BoxLayout.
in
createPanel1(){
// create panel.
jPanel1.setLayout(new GridLayout(0,1));

adding new jpanel with default components in swing

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();
}
}

Categories

Resources