I have created one checkbox this way:
JCheckbox field = new JCheckBox("EDEX:", true);.
I was add this to Jpanel and layout is FormLayout using CellConstraints xy positions.
but it is not displayed EDEX text after checkbox.
this is code:
panel.add(field , cc.xy(5, 3));
please help me
Thank You
This works fine:
import java.awt.EventQueue;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Example {
public Example() {
FormLayout layout =
new FormLayout( "left:pref, 15px, center:pref, 15px, right:pref, 15px, fill:pref, 15px, pref",
"pref, 12px, pref, 4px, pref, 4px, pref, 4px, pref, 4px, pref" );
JPanel panel = new JPanel( layout );
CellConstraints cc = new CellConstraints();
JCheckBox field = new JCheckBox( "EDEX:", true );
panel.add( field, cc.xy( 5, 3 ) );
JFrame f = new JFrame();
f.setBounds( 10, 10, 100, 100 );
f.setDefaultCloseOperation( 3 );
f.getContentPane().add( panel );
f.setVisible( true );
}
public static void main( String[] args ) {
EventQueue.invokeLater( new Runnable() {
#Override
public void run() {
new Example();
}
} );
}
}
Related
I want to add on the same frame multiple instances of the same component which extends JPanel but unfortunately when I compile the code it adds on the frame only the last instance.
Here is my main class which extends JFrame:
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Interface extends JFrame
{
JPanel jPanel;
JPanel jPanel02;
JPanel jPanel03;
public static void main( String[] args )
{
new Interface( );
}
public Interface( )
{
setTitle( "Tile" );
setSize( 300, 300 );
setVisible( true );
jPanel = new MyOwnJPanel( "My Own JPanel 01" );
jPanel02 = new MyOwnJPanel( "My Own JPanel 02" );
jPanel03 = new MyOwnJPanel( "My Own JPanel 03" );
add( jPanel );
add( jPanel02 );
add(jPanel03);
}
}
And here is the class which extends JPanel:
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
*
*/
public class MyOwnJPanel extends JPanel
{
JLabel jLabel;
MyOwnJPanel(String headerTitle){
jLabel = new JLabel( headerTitle );
add(jLabel );
}
}
So, even though I add all three panels, on the frame appears only one, the last one.
Thank you!
I think you are adding jpanels at the same location so you are seeing the last one. Try setting borderlayout to jframe. I think you will see all panels.
Edit: Working code is below:
setTitle( "Tile" );
setSize( 300, 300 );
setVisible( true );
setLayout(new BorderLayout());
jPanel = new MyOwnJPanel( "My Own JPanel 01" );
jPanel02 = new MyOwnJPanel( "My Own JPanel 02" );
jPanel03 = new MyOwnJPanel( "My Own JPanel 03" );
add( jPanel, BorderLayout.CENTER );
add( jPanel02, BorderLayout.NORTH );
add(jPanel03, BorderLayout.SOUTH);
I've got (again) a problem: i got a jbutton with an image on background, but when i want to put some text on it, it will apears on the right side of background, not in button, but aside...
Here is working code, you must only link some image :)
package program;
import java.awt.Image;
import java.awt.Insets;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class Program {
private static JFrame frame;
private static JPanel panel;
private static JButton button;
private static Image buttonImage;
private static ImageIcon buttonIcon;
public static void main(String[] args) {
frame = new JFrame("Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
frame.add(panel);
frame.pack();
frame.setVisible(true);
frame.setSize(200,100);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
String imgUrl = "images/";
try {
buttonImage = ImageIO.read(new File(imgUrl+"img.png"));
} catch (IOException e) {
Logger.getLogger(Program.class.getName()).log(Level.SEVERE, null, e);
}
buttonIcon = new ImageIcon(buttonImage);
button = new JButton("TEST", buttonIcon);
panel.add(button);
button.setMargin(new Insets(0, 0, 0, 0));
button.setBounds(0, 0, 146, 67);
button.setOpaque(false);
button.setContentAreaFilled(false);
button.setBorderPainted(false);
}
}
Here are 4 ways to display text on an image:
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
public class LabelImageText extends JPanel
{
public LabelImageText()
{
JLabel label1 = new JLabel( new ColorIcon(Color.ORANGE, 100, 100) );
label1.setText( "Easy Way" );
label1.setHorizontalTextPosition(JLabel.CENTER);
label1.setVerticalTextPosition(JLabel.CENTER);
add( label1 );
//
JLabel label2 = new JLabel( new ColorIcon(Color.YELLOW, 200, 150) );
label2.setLayout( new BoxLayout(label2, BoxLayout.Y_AXIS) );
add( label2 );
JLabel text = new JLabel( "More Control" );
text.setAlignmentX(JLabel.CENTER_ALIGNMENT);
label2.add( Box.createVerticalGlue() );
label2.add( text );
label2.add( Box.createVerticalStrut(10) );
//
JLabel label3 = new JLabel( new ColorIcon(Color.GREEN, 200, 150) );
label3.setLayout( new GridBagLayout() );
add( label3 );
JLabel text3 = new JLabel();
text3.setText("<html><center>Text<br>over<br>Image<center></html>");
text3.setLocation(20, 20);
text3.setSize(text3.getPreferredSize());
label3.add( text3 );
//
JLabel label4 = new JLabel( new ColorIcon(Color.CYAN, 200, 150) );
add( label4 );
JTextPane textPane = new JTextPane();
textPane.setText("Add some text that will wrap at your preferred width");
textPane.setEditable( false );
textPane.setOpaque(false);
SimpleAttributeSet center = new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
StyledDocument doc = textPane.getStyledDocument();
doc.setParagraphAttributes(0, doc.getLength(), center, false);
textPane.setBounds(20, 20, 75, 100);
label4.add( textPane );
}
public static class ColorIcon implements Icon
{
private Color color;
private int width;
private int height;
public ColorIcon(Color color, int width, int height)
{
this.color = color;
this.width = width;
this.height = height;
}
public int getIconWidth()
{
return width;
}
public int getIconHeight()
{
return height;
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
g.setColor(color);
g.fillRect(x, y, width, height);
}
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("LabelImageText");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new LabelImageText() );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
you never set the background,you'v set the icon of the button
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class StringTest extends JFrame
implements ActionListener
{
private JTextField input, result;
private int i;
public StringTest()
{
super("String test");
i=0;
Box box1 = Box.createVerticalBox();
box1.add(new JLabel(" Input:"));
box1.add(Box.createVerticalStrut(10));
box1.add(new JLabel("Result:"));
input = new JTextField(40);
input.setBackground(Color.WHITE);
input.addActionListener(this);
input.selectAll();
result = new JTextField(40);
result.setBackground(Color.YELLOW);
result.setEditable(false);
Box box2 = Box.createVerticalBox();
box2.add(input);
box2.add(Box.createVerticalStrut(10));
box2.add(result);
Box box3 = Box.createHorizontalBox();
box3.add(box1);
box3.add(box2);
Box box4 = Box.createVerticalBox();
JButton new_game = new JButton("NEW GAME");
JLabel game_title =new JLabel("***Welcome to Hangman Game***");
box4.add(game_title);
box4.add(box3);
box4.add(Box.createVerticalStrut(10));
box4.add(box3);
box4.add(Box.createVerticalStrut(10));
box4.add(new_game);
new_game.setAlignmentX(Component.CENTER_ALIGNMENT);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(box4);
//c.add(box2);
// c.add(ok_button);c.add(ok1_button);
input.requestFocus();
}
public void actionPerformed(ActionEvent e)
{
String str = input.getText();
result.setText(str);
if (i%2==0)
result.setBackground(Color.WHITE);
else
result.setBackground(Color.YELLOW);
input.selectAll();
}
public static void main(String[] args)
{
StringTest window = new StringTest();
window.setBounds(100, 100, 600, 100);
window.setDefaultCloseOperation(EXIT_ON_CLOSE);
window.setVisible(true);
}
}
I'm trying to get the ">>" button to be centered vertically, using a GroupLayout.
But as you can see it is putting it near the top. I've tried messing with the layout and can't seem to get it to work. Please help! =)
horizontal_group = layout.createParallelGroup GroupLayout::Alignment::CENTER
horizontal_group.addComponent tags_label
available_across_hor = layout.createSequentialGroup
available_down_hor = layout.createParallelGroup GroupLayout::Alignment::CENTER
available_down_hor.addComponent available_label
available_down_hor.addComponent available_pane
available_down_hor.addComponent new_tag_button
selected_down_hor = layout.createParallelGroup GroupLayout::Alignment::CENTER
selected_down_hor.addComponent selected_label
selected_down_hor.addComponent selected_pane
selected_down_hor.addComponent remove_tag_button
available_across_hor.addGroup available_down_hor
available_across_hor.addComponent move_button
available_across_hor.addGroup selected_down_hor
horizontal_group.addGroup available_across_hor
vertical_group = layout.createSequentialGroup
vertical_group.addComponent tags_label
available_across_ver = layout.createParallelGroup
available_down_ver = layout.createSequentialGroup
available_down_ver.addComponent available_label
available_down_ver.addComponent available_pane
available_down_ver.addComponent new_tag_button
selected_down_ver = layout.createSequentialGroup
selected_down_ver.addComponent selected_label
selected_down_ver.addComponent selected_pane
selected_down_ver.addComponent remove_tag_button
available_across_ver.addGroup available_down_ver
available_across_ver.addComponent move_button
available_across_ver.addGroup selected_down_ver
vertical_group.addGroup available_across_ver
layout.setHorizontalGroup horizontal_group
layout.setVerticalGroup vertical_group
Here is the solution:
package com.zetcode;
import java.awt.Container;
import java.awt.EventQueue;
import javax.swing.GroupLayout;
import static javax.swing.GroupLayout.Alignment.CENTER;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
public class GroupLayoutTags extends JFrame {
public GroupLayoutTags() {
initUI();
setTitle("Tags");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void initUI() {
Container pane = getContentPane();
GroupLayout gl = new GroupLayout(pane);
pane.setLayout(gl);
JLabel avLbl = new JLabel("Available");
JLabel tagsLbl = new JLabel("Tags");
JLabel selLbl = new JLabel("Selected");
JButton newBtn = new JButton("New");
JButton moveBtn = new JButton(">>");
JButton remBtn = new JButton("Remove");
JList leftList = new JList();
JScrollPane spleft = new JScrollPane(leftList);
JList rightList = new JList();
JScrollPane spright = new JScrollPane(rightList);
gl.setAutoCreateGaps(true);
gl.setAutoCreateContainerGaps(true);
gl.setHorizontalGroup(gl.createParallelGroup(CENTER)
.addComponent(tagsLbl)
.addGroup(gl.createSequentialGroup()
.addGroup(gl.createParallelGroup(CENTER)
.addComponent(avLbl)
.addComponent(spleft, 100, 200, Short.MAX_VALUE)
.addComponent(newBtn))
.addComponent(moveBtn)
.addGroup(gl.createParallelGroup(CENTER)
.addComponent(selLbl)
.addComponent(spright, 100, 200, Short.MAX_VALUE)
.addComponent(remBtn)))
);
gl.setVerticalGroup(gl.createSequentialGroup()
.addComponent(tagsLbl)
.addGroup(gl.createParallelGroup(CENTER)
.addGroup(gl.createSequentialGroup()
.addComponent(avLbl)
.addComponent(spleft, 100, 250, Short.MAX_VALUE)
.addComponent(newBtn))
.addComponent(moveBtn)
.addGroup(gl.createSequentialGroup()
.addComponent(selLbl)
.addComponent(spright, 100, 250, Short.MAX_VALUE)
.addComponent(remBtn)))
);
pack();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
GroupLayoutTags ex = new GroupLayoutTags();
ex.setVisible(true);
}
});
}
}
If find MigLayout manager easier to use than GroupLayout.
However with some practice, GroupLayout is also feasible.
Try create filler components:
Component topFiller = Box.createVerticalGlue();
Component bottomFiller = Box.createVerticalGlue();
and then add topFiller before the >> button, and bottomFiller after it for both horizontal and vertical group.
I'm almost blind after reading lots of java swing articles, and still can not get panel to work.
When i add 2 JLabels, they are nicely aligned to left, with 5px padding defined by EmptyBorder, just as i want them to be.
I found that after adding ProgressBar with extra border for padding, does not work as expected, added 1 more panel, where i add ProgressBar. Progress looks good, but all my labels are displaced.
And finally it look like this (RED background is for debug, to see how JPanel draws):
Question1: How to fix this?
Question2: Is it standard approach for swing to place panel inside of other panel with other panels just to get formating i want?
Source:
public class AppInitProgressDialog {
private static final int VIEW_PADDING_VAL = 5;
private static final Border viewPaddingBorder = new EmptyBorder( VIEW_PADDING_VAL, VIEW_PADDING_VAL, VIEW_PADDING_VAL, VIEW_PADDING_VAL );
private JPanel view; // Dialog view
private JPanel panel;
private JPanel progressPanel;
private JLabel title;
private JLabel progressDesc;
private JProgressBar progressBar;
private void initPanel( int w, int h ) {
view = new JPanel();
view.setBorder( BorderFactory.createRaisedSoftBevelBorder() );
view.setBackground( Color.LIGHT_GRAY );
view.setSize( w, h );
view.setLayout( new BorderLayout() );
panel = new JPanel();
panel.setBorder( viewPaddingBorder );
panel.setLayout( new BoxLayout(panel, BoxLayout.PAGE_AXIS) );
//panel.setLayout( new SpringLayout() );
panel.setOpaque( false );
JFrame parent = AppContext.getMe().getAppWindow().getFrame();
int posx = (parent.getWidth() - w)/2;
int posy = (parent.getHeight() - h)/2;
view.add( panel, BorderLayout.CENTER );
view.setLocation( posx, posy );
}
private void initTitle() {
title = new JLabel( "Progress title" );
title.setAlignmentX( JComponent.LEFT_ALIGNMENT );
panel.add(title);
}
private void initProgress() {
progressPanel = new JPanel( new BorderLayout() );
progressPanel.setBackground( Color.LIGHT_GRAY );
progressPanel.setBorder( new EmptyBorder( 15, 30, 15, 30) );
progressPanel.setBackground( Color.RED );
progressBar = new JProgressBar(0, 10000);
progressBar.setStringPainted(true);
progressBar.setAlignmentX( JComponent.LEFT_ALIGNMENT );
progressPanel.add(progressBar);
panel.add( progressPanel );
progressDesc = new JLabel( "Progress description" );
panel.add(progressDesc);
}
public AppInitProgressDialog() {
initPanel( 400, 100 );
initTitle();
initProgress();
}
public JComponent getView() {
return view;
}
}
Alternatively, you can use BorderLayout for panel:
panel = new JPanel(new BorderLayout());
...
panel.add(title, BorderLayout.NORTH);
...
panel.add(progressPanel); // default CENTER
...
panel.add(progressDesc, BorderLayout.SOUTH);
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.border.EmptyBorder;
/**
* #see http://stackoverflow.com/a/16837816/230513
*/
public class Test {
private JFrame f = new JFrame("Test");
private void display() {
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new AppInitProgressDialog().getView());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
class AppInitProgressDialog {
private static final int VIEW_PADDING_VAL = 5;
private JPanel view; // Dialog view
private JPanel panel;
private JPanel progressPanel;
private JLabel title;
private JLabel progressDesc;
private JProgressBar progressBar;
private void initPanel(int w, int h) {
view = new JPanel();
view.setBorder(BorderFactory.createRaisedBevelBorder());
view.setBackground(Color.LIGHT_GRAY);
view.setSize(w, h);
view.setLayout(new BorderLayout());
panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createLineBorder(Color.blue));
panel.setOpaque(false);
view.add(panel, BorderLayout.CENTER);
}
private void initTitle() {
title = new JLabel("Progress title");
title.setAlignmentX(JComponent.LEFT_ALIGNMENT);
panel.add(title, BorderLayout.NORTH);
}
private void initProgress() {
progressPanel = new JPanel(new BorderLayout());
progressPanel.setBackground(Color.LIGHT_GRAY);
progressPanel.setBorder(new EmptyBorder(15, 30, 15, 30));
progressPanel.setBackground(Color.RED);
progressBar = new JProgressBar(0, 10000);
progressBar.setStringPainted(true);
progressBar.setAlignmentX(JComponent.LEFT_ALIGNMENT);
progressPanel.add(progressBar);
panel.add(progressPanel);
progressDesc = new JLabel("Progress description");
panel.add(progressDesc, BorderLayout.SOUTH);
}
public AppInitProgressDialog() {
initPanel(400, 100);
initTitle();
initProgress();
}
public JComponent getView() {
return view;
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Test().display();
}
});
}
}
Use Layout Managers rather than just adding panels to each other. Specifically in your case I think you can use GridLayout.
panel.setLayout(new GridLayout(1,1,0,0));
Fore more details on layout refer to this tutorial.
So I have a JTextPane, and I have a method that returns a String containing the text in the JTextPane. I have been trying to fix this for weeks. The getText() method returns a blank line. I tried getting the document length, but that returns 0.
Here is the code:
import java.awt.*;
import javax.swing.*;
public class CodeTabs extends JTabbedPane {
private JTextPane codearea;
private JScrollPane scroll;
public CodeTabs() {
setTabPlacement(JTabbedPane.BOTTOM);
codearea = new JTextPane();
scroll = new JScrollPane(codearea, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll.setPreferredSize(new Dimension(Toolkit.getDefaultToolkit().getScreenSize()));
addTab("Code", scroll);
}
public String getCode() {
String s = codearea.getText();
System.out.println(s);
return s;
}
}
I took your code and added a main method and a button to trigger the getCode() method. Everything works as expected. When I type something in the text area, it gets printed when I press the button.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CodeTabs extends JTabbedPane {
private JTextPane codearea;
private JScrollPane scroll;
public CodeTabs() {
setTabPlacement(JTabbedPane.BOTTOM);
codearea = new JTextPane();
scroll = new JScrollPane(codearea, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll.setPreferredSize(new Dimension( 300,300 ));
JPanel panel = new JPanel( new BorderLayout() );
panel.add( scroll, BorderLayout.CENTER );
JButton comp = new JButton( "Print text" );
comp.addActionListener( new ActionListener() {
#Override
public void actionPerformed( ActionEvent e ) {
getCode();
}
} );
panel.add( comp, BorderLayout.SOUTH );
addTab( "Code", panel );
}
public String getCode() {
String s = codearea.getText();
System.out.println(s);
return s;
}
public static void main( String[] args ) {
EventQueue.invokeLater( new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame( "TestFrame" );
frame.getContentPane().add( new CodeTabs() );
frame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );
frame.pack();
frame.setVisible( true );
}
} );
}
}
Note: there is no need to extend JTabbedPane. Use it instead of extending it (I left it in the code posted in this answer to match your code as closely as possible)
Do this way:-
codearea.getDocument().getText(0, codearea.getDocument().getLength());