I want to keep my two JLabel text have Left alignment and at the same time place my boxLayout in the center of the JFrame.
I tried setAlignmentX(CENTER_ALIGNMENT) on my boxlayout panel but it's not placing my boxlayout in the center.
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GuiTest extends JFrame {
private static final long serialVersionUID = 1L;
private JLabel jLabelOne = new JLabel();
private JLabel jLabelTwo = new JLabel();
private JPanel panel = new JPanel();
private BoxLayout boxLayout = new BoxLayout(panel,BoxLayout.Y_AXIS);
public GuiTest() {
jLabelOne.setAlignmentX(LEFT_ALIGNMENT);
jLabelTwo.setAlignmentX(LEFT_ALIGNMENT);
jLabelOne.setText("This is text one");
jLabelTwo.setText("This is text two");
panel.setLayout(boxLayout);
panel.add(jLabelOne);
panel.add(jLabelTwo);
panel.setAlignmentX(CENTER_ALIGNMENT);
add(panel);
pack();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(1024,768);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String args[]) {
new GuiTest();
}
}
This won't achieve anything I believe:
panel.setAlignmentX(CENTER_ALIGNMENT);
because you're adding the panel to the JFrame's contentPane, a Container that uses BorderLayout, and are in fact adding it in a default way, meaning BorderLayout.CENTER.
Consider giving the contentPane a GridBagLayout, and adding the panel JPanel in a default way, which should center it. This will only be seen if its preferred size is smaller than that JFrame's contentPane.
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GuiTest extends JFrame {
private static final long serialVersionUID = 1L;
private JLabel jLabelOne = new JLabel();
private JLabel jLabelTwo = new JLabel();
private JPanel panel = new JPanel();
private BoxLayout boxLayout = new BoxLayout(panel,BoxLayout.Y_AXIS);
public GuiTest() {
panel.setLayout(boxLayout);
jLabelOne.setAlignmentX(CENTER_ALIGNMENT);
jLabelTwo.setAlignmentX(CENTER_ALIGNMENT);
jLabelOne.setText("This is text one");
jLabelTwo.setText("This is text two");
panel.add(jLabelOne);
panel.add(jLabelTwo);
panel.setAlignmentX(CENTER_ALIGNMENT);
add(panel);
setSize(1024,768);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String args[]) {
new GuiTest();
}
}
This should get you what you want. You had some things out of order.
Related
I failed to change the height of JPanel or JScrollPane to make more lines to appear, I used GridLayout. It seems that, every component in it should have the same size even when I use setSize(). Should I use another layout?
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class Main {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
private imagePanel image;
JTextField textField = new JTextField(20);
public Main() throws IOException{
prepareGUI();
}
class imagePanel extends JPanel {
private static final long serialVersionUID = 1L;
public void paint(Graphics g) {
try {
BufferedImage image = ImageIO.read(new File("file.jpg"));
g.drawImage(image, 170, 0, null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException{
Main swingControlDemo = new Main();
swingControlDemo.showEventDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("Java SWING Examples");
mainFrame.setSize(400,500);
GridLayout gridlayout = new GridLayout(4, 1);
gridlayout.setVgap(1);
mainFrame.setLayout(gridlayout);
headerLabel = new JLabel("",JLabel.CENTER );
statusLabel = new JLabel("",JLabel.CENTER);
JScrollPane scroller = new JScrollPane(statusLabel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
image = new imagePanel();
image.setLayout(new FlowLayout());
// mainFrame.add(headerLabel);
mainFrame.add(image);
mainFrame.add(controlPanel);
mainFrame.add(scroller);
mainFrame.setVisible(true);
}
private void showEventDemo(){
headerLabel.setText("Control in action: Button");
JButton okButton = new JButton("reload");
JButton submitButton = new JButton("Submit");
JButton cancelButton = new JButton("Cancel");
okButton.setActionCommand("reload");
submitButton.setActionCommand("Submit");
cancelButton.setActionCommand("Cancel");
okButton.addActionListener(new ButtonClickListener());
submitButton.addActionListener(new ButtonClickListener());
cancelButton.addActionListener(new ButtonClickListener());
controlPanel.add(okButton);
controlPanel.add(submitButton);
//controlPanel.add(cancelButton);
controlPanel.add(textField);
System.out.println("---------------------");
mainFrame.setVisible(true);
}
private class ButtonClickListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if( command.equals( "reload" )) {
statusLabel.setText(convertToMultiline("Line1\nLine2\nLine3\nLine4\nLine5\nLine6\nLine7\nLine8\nLine9\nLine2\nLine3\nLine\nLine2\nLine3\nLine\nLine2\nLine3\nLine\nLine2\nLine3\nLine\nLine2\nLine3\nLine\nLine2\nLine3\nLine\nLine2\nLine3\nLine"));
}
else {
statusLabel.setText("Submit Button clicked.");
}
}
}
public static String convertToMultiline(String orig)
{
return "<html>" + orig.replaceAll("\n", "<br>");
}
}
The GUI need to look like this
I want to remove the large vertical gaps between the componets, and the jLabel should use that space
Well in your comment you say you want the label to use the space. But in your picture you show the text area with all the space. How can we answer a question when you give us conflicting requirements? Be specific and accurate when describing a problem.
In any case, the default layout of a JFrame is a BorderLayout so you would probably start with that.
Then the component that you want to grow/shrink as the frame is resized should be added to the CENTER of the frame.
Then you create a second panel to contain your other components. This panel would then be added to either the PAGE_START or PAGE_NORTH of the frame depending on your exact requirement.
The layout manager of this panel can then be whatever your want. Maybe a GridLayout, or a GridBagLayout or a vertical BoxLayout.
Read the section from the Swing tutorial on Layout Managers for more information and working examples. The key point is you create nest panels each with a different layout manager to achieve your layout.
Problem - the given codes below is not displaying my JPanel(PageOne) and I am not sure why is it not displaying my JPanel(PageOne). Please Help.
I have added the JPanel(PageOne) to my panel which has a cardLayout();
I have set my JFrame to visible already.
PageOne.java
import javax.swing.JLabel;
import javax.swing.JPanel;
public class PageOne extends JPanel {
public PageOne() {
JLabel label = new JLabel("Page 1");
JPanel panel = new JPanel();
panel.add(label);
} }
PageTwo.java
import javax.swing.JLabel;
import javax.swing.JPanel;
public class PageTwo extends JPanel {
public PageTwo() {
JLabel label = new JLabel("Page 2");
JPanel panel = new JPanel();
panel.add(label);
}
}
DisplayUI.java
import java.awt.CardLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class DisplayUI {
public static void main(String[] args) {
new DisplayUI();
}
public DisplayUI() {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
CardLayout cardLayout = new CardLayout();
JFrame frame = new JFrame("frame");
JPanel panel = new JPanel();
panel.setLayout(cardLayout);
panel.add(new PageOne(), "1");
panel.add(new PageTwo(), "2");
cardLayout.show(panel,"1");
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
You're not actually adding anything to PageOne or PageTwo panels...
public PageOne() {
JLabel label = new JLabel("Page 1");
JPanel panel = new JPanel();
panel.add(label);
// But nothing is actually added to "this"...
}
Unless you "really" need it, you can get rid of the second JPanel and add the label directly to PageOne (and the same thing goes for PageTwo)
public PageOne() {
JLabel label = new JLabel("Page 1");
add(label);
}
Or add the JPanel you create (which contains the label)
public PageOne() {
JLabel label = new JLabel("Page 1");
JPanel panel = new JPanel();
panel.add(label);
add(panel);
}
Remember, JPanel is type of Container, it can have child components.
Get the content pane of frame and than try adding :
Container container=frame.getContentPane();
container.add(panel);
Hope this helps you.
Whenever I run my program my JTextArea does not follow the dimension that I have given it, but if I resize my JFrame it updates and sets its size to what I put.
What is the issue?
public ControlPanel() {
// create our list of players
list = new JList(model);
// create our scroll panes
userspane = new JScrollPane(list);
consolepane = new JScrollPane(console);
// set sizes
userspane.setSize(100, 500);
jta.setSize(100, 500);
list.setSize(100, 500);
consolepane.setSize(100, 500);
console.setSize(100, 500);
// add to panel
panel.add(userspane, BorderLayout.CENTER);
panel.add(kick);
panel.add(ban);
panel.add(info);
panel.add(consolepane, BorderLayout.CENTER);
// set frame properties
setTitle("RuneShadows CP");
setSize(280, 400);
//setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setContentPane(panel);
setVisible(true);
}
Don't set the sizes to anything.
For JTextArea you can use the constructor JTextArea(int rows, int charSpaces)
Just pack() the JFrame and it will respect all the preferred sizes of the components inside.
Also instead of setting the content pane to the panel, just add the panel. That will respect the preffered size of the panel when pack() is called
I'm not exactly sure what variable was what (or the sizes you wanted the), so I assumed text areas, and others as well. See this example where I just used the JTextArea constructor I mentioned and just packed.
EDITED with no sizes set
import java.awt.BorderLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class ControlPanel extends JFrame {
JScrollPane userspane;
JList list;
DefaultListModel model = new DefaultListModel();
JScrollPane consolepane;
JTextArea console = new JTextArea(20, 50);
JTextArea jta = new JTextArea(6, 50);
JPanel panel = new JPanel();
JButton kick = new JButton("Kick");
JButton ban = new JButton("Ban");
JButton info = new JButton("Info");
public ControlPanel() {
// create our list of players
list = new JList(model);
// create our scroll panes
userspane = new JScrollPane(list);
consolepane = new JScrollPane(console);
// add to panel
panel.add(userspane, BorderLayout.CENTER);
panel.add(kick);
panel.add(ban);
panel.add(info);
panel.add(consolepane, BorderLayout.CENTER);
add(panel);
pack();
setTitle("RuneShadows CP");
//setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new ControlPanel();
}
});
}
}
UPDATE - with positioning
Keep in mind also, with BorderLayout you need to specify a position for every component you add or else it will default to CENTER and each position an only have one component. I noticed you trying to add two components to the CENTER
import java.awt.BorderLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class ControlPanel extends JFrame {
JScrollPane userspane;
JList list;
DefaultListModel model = new DefaultListModel();
JScrollPane consolepane;
JTextArea console = new JTextArea(20, 50);
JTextArea jta = new JTextArea(6, 50);
JPanel panel = new JPanel(new BorderLayout());
JButton kick = new JButton("Kick");
JButton ban = new JButton("Ban");
JButton info = new JButton("Info");
public ControlPanel() {
// create our list of players
list = new JList(model);
// create our scroll panes
userspane = new JScrollPane(list);
consolepane = new JScrollPane(console);
// add to panel
panel.add(userspane, BorderLayout.SOUTH);
JPanel buttonPanel = new JPanel();
buttonPanel.add(kick);
buttonPanel.add(ban);
buttonPanel.add(info);
panel.add(buttonPanel, BorderLayout.CENTER);
panel.add(consolepane, BorderLayout.NORTH);
add(panel);
pack();
setTitle("RuneShadows CP");
//setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new ControlPanel();
}
});
}
}
I'm trying to create a panel that uses a JLayeredPane to have a panel centered above another, larger panel. I can't seem to get the smaller panel to display though. Any ideas as to what I'm doing wrong?
import java.awt.Dimension;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
public class MainPanel extends JPanel {
private JLayeredPane pane;
private AllPlayersPanel players; //Larger panel, uses circleLayout
private GamePanel game; //Smaller panel, simple BorderLayout
public MainPanel(){
super();
setSize(900, 900);
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
pane = new JLayeredPane();
pane.setPreferredSize(new Dimension(900, 900));
players = new AllPlayersPanel();
players.setPreferredSize(players.getPreferredSize());
players.setLocation(0,0);
//players.setOpaque(false);
pane.add(players, new Integer(0));
game = new GamePanel();
game.setPreferredSize(game.getPreferredSize());
game.setLocation(385, 405);
//game.setOpaque(false);
pane.add(game, new Integer(2));
add(pane);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
JComponent newPane = new MainPanel();
newPane.setOpaque(true);
frame.setContentPane(newPane);
frame.pack();
frame.setVisible(true);
}
}
I've tried every combination of .setOpaque() I can think of as well.
CircleLayout can be found here
Basically, you're using setSize when you shouldn't be and preferredSize when you don't need to...
For example.
In you constructor of the MainPanel you call setSize(900, 900); when you should have overridden getPreferredSize then on the panels you are adding to the JLayeredPane you're calling setPreferredSize but the JLayeredPane has no layout manager with which to check this value, so the size of these panels remain as 0x0.
Besides, I'm not sure what you expected to gain from calling players.setPreferredSize(players.getPreferredSize());, which basically will set the preferred size to 0x0 any way :P
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
public class MainPanel extends JPanel {
private JLayeredPane pane;
private JPanel players; //Larger panel, uses circleLayout
private JPanel game; //Smaller panel, simple BorderLayout
public MainPanel() {
super();
// setSize(900, 900);
// setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
setLayout(new BorderLayout());
pane = new JLayeredPane();
// pane.setPreferredSize(new Dimension(900, 900));
pane.setBorder(new LineBorder(Color.RED));
players = new JPanel();
players.setBackground(Color.RED);
players.setSize(getPreferredSize());
players.setLocation(0, 0);
//players.setOpaque(false);
pane.add(players, new Integer(0));
game = new JPanel();
game.setSize(game.getPreferredSize());
game.setBackground(Color.BLUE);
game.setLocation(385, 405);
//game.setOpaque(false);
pane.add(game, new Integer(2));
add(pane);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(900, 900);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
JComponent newPane = new MainPanel();
newPane.setOpaque(true);
frame.setContentPane(newPane);
frame.pack();
frame.setVisible(true);
}
}
I want to put JTabbedPane in the middle,and clicking any tab I want to change will reflect in both above and below panel of tabbedpane.
I tried it but it works only on the below panel.
How to overcome from this problem? Please help me.
Thanks in advance.
Here is my code:
jTabbedPane1 = new javax.swing.JTabbedPane();
jTabbedPane1.addTab("Daily Market", jScrollPane1);
jTabbedPane1.addTab("Weekly Market", jScrollPane2);
On assumption that you want to change something in the panels above and below your tabbed pane. Sample code changing text of a label in top and bottom panel below:
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TestJTabbedPane extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private void init(){
this.setLayout(new BorderLayout());
JPanel topPanel = new JPanel();
final JLabel topLabel = new JLabel("North");
topPanel.add(topLabel);
this.add(topPanel, BorderLayout.NORTH);
JTabbedPane tabbedPane = new JTabbedPane();
JPanel firstTabCont = new JPanel();
firstTabCont.add(new JLabel("First"));
tabbedPane.addTab("First", firstTabCont);
JPanel secondTabCont = new JPanel();
secondTabCont.add(new JLabel("Second"));
tabbedPane.addTab("Second", secondTabCont);
this.add(tabbedPane, BorderLayout.CENTER);
JPanel bottomPanel = new JPanel();
final JLabel bottomLabel = new JLabel("South");
bottomPanel.add(bottomLabel);
this.add(bottomPanel, BorderLayout.SOUTH);
tabbedPane.addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent evt) {
JTabbedPane pane = (JTabbedPane)evt.getSource();
int selectedIndex = pane.getSelectedIndex();
if(selectedIndex == 0){
topLabel.setText("");
topLabel.setText("Hi");
bottomLabel.setText("");
bottomLabel.setText("Bye");
} else {
topLabel.setText("");
topLabel.setText("Bye");
bottomLabel.setText("");
bottomLabel.setText("Hi");
}
}
});
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new TestJTabbedPane().init();
}
}