How to add multiple instances of identical JPanels - java

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

Related

Icon Image appears Incredibly small on Jbutton | Java

For a small project, I'm trying to make a TicTacToe Game using JFrame and JButtons. The game is going to consist of 9 Buttons that when clicked will changed to either an X or O. I havent gotten too far into the project but The problem I'm running into is when I display an image as an Icon on the Jbutton, Its incredibly small and Ive tried to resize it with no avail. Any feedback is appreciated.
This is what the button looks like when i put the icon on it:
Image of small button when Icon on it
import java.awt.*;
import javax.swing.*;
import java.io.*;
public class TicTacToe
{
public static void main ( String[] args )
{
ButtonFrame frm = new ButtonFrame("TicTactToe");
frm.setSize(300, 450);
frm.setVisible( true );
}
}
class ButtonFrame extends JFrame
{
JButton bChange;
ButtonFrame(String title)
{
super( title );
ImageIcon XIcon = new ImageIcon("X.png");
Icon OIcon = new ImageIcon("O.png");
setLayout( new FlowLayout() ); // set the layout manager
//top row
bChange = new JButton(XIcon);
add( bChange );
setSize(400, 400);
bChange = new JButton("Button2");
add( bChange );
bChange = new JButton("Button3");
add( bChange );
//middle row
bChange = new JButton("Button4");
add( bChange );
bChange = new JButton("Button5");
add( bChange );
bChange = new JButton("Button6");
add( bChange );
//bottom row
bChange = new JButton("Button7");
add( bChange );
bChange = new JButton("Button8");
add( bChange );
bChange = new JButton("Button9");
add( bChange );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
your problem seems to be the image you are using. when I copied your code and used my own image the program worked properly. http://i.stack.imgur.com/io2yV.png

How would I position a JComponent to true center in a BorderLayout?

I am developing a Swing application using BorderLayout to position the components. Since BorderLayout.CENTER positions components to whatever is left after the other components are placed, and that fact is making my GUI look weird, I was wondering if there was a way to position components to true center, rather than in between the two sides. Since BorderLayout spaces make a component fill an entire space, I'm guessing the solution would be to wrap the component into a JPanel. However, positioning the component to the center of this panel will make the component be closer to one side than another if you have components on other sides. How do I work around this?
These images demonstrate the problem and the ideal solution; the gray border represents the BorderLayout.SOUTH (wrapped in a JPanel) of the main frame. The black squares represents components that are throwing the center component off. The red square represents the component that needs to be centered.
Problem:
Ideal solution:
As I see the problem, in order for the red component to be centered the right and left components must be of equal size.
You might be able to use the Relative Layout.
The RelativeLayout will allow you to make the right/left components the same size while keeping the center component at its preferred size. As the frame is resized space will be added/removed from the right/left components.
For example:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
public class SSCCE extends JPanel
{
public SSCCE()
{
JPanel left = new JPanel( new FlowLayout(FlowLayout.LEFT) );
JPanel leftBox = new JPanel();
leftBox.setPreferredSize( new Dimension(200, 50) );
leftBox.setBackground( Color.BLACK );
left.add( leftBox );
JPanel center = new JPanel( new FlowLayout(FlowLayout.CENTER) );
JPanel centerBox = new JPanel();
centerBox.setPreferredSize( new Dimension(50, 50) );
centerBox.setBackground( Color.RED );
center.add( centerBox );
JPanel right = new JPanel( new FlowLayout(FlowLayout.RIGHT) );
JPanel rightBox = new JPanel();
rightBox.setPreferredSize( new Dimension(50, 50) );
rightBox.setBackground( Color.BLACK );
right.add( rightBox );
setLayout( new RelativeLayout(RelativeLayout.X_AXIS, 5) );
add(left, new Float(1));
add(center);
add(right, new Float(1));
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SSCCE());
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
The one problem with the relative layout is that when you pack the frame the components will be dislayed too small since the preferred size is simply the sum of the components. So the left panel will be truncated.
In the example above you can add the following to get around this problem:
right.add( rightBox );
right.setPreferredSize( left.getPreferredSize() ); // added
Another option might be to use the OverlayLayout which can be set up to display the red panel over top of a panel containing the two other components:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class SSCCE extends JPanel
{
public SSCCE()
{
JPanel left = new JPanel( new FlowLayout(FlowLayout.LEFT) );
JPanel leftBox = new JPanel();
leftBox.setPreferredSize( new Dimension(200, 50) );
leftBox.setBackground( Color.BLACK );
left.add( leftBox );
JPanel center = new JPanel( new FlowLayout(FlowLayout.CENTER) );
center.setOpaque(false);
JPanel centerBox = new JPanel();
centerBox.setPreferredSize( new Dimension(50, 50) );
centerBox.setBackground( Color.RED );
center.add( centerBox );
JPanel right = new JPanel( new FlowLayout(FlowLayout.RIGHT) );
JPanel rightBox = new JPanel();
rightBox.setPreferredSize( new Dimension(50, 50) );
rightBox.setBackground( Color.BLACK );
right.add( rightBox );
JPanel main = new JPanel( new BorderLayout() );
main.add(left, BorderLayout.LINE_START);
main.add(right, BorderLayout.LINE_END);
setLayout( new OverlayLayout(this) );
add(center);
add(main);
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SSCCE());
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}

Cannot get text from JTextPane

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

JCheckbox in Java swing

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

java simple JPanel management (see screenshot)

I have a JPanel that encapsulates two JPanels, one on top of the other.
The first holds two JLabels which hold the playing cards.
The second holds the player's text (name and score).
However, when I remove the player's cards, the lower JPanel moves up to the top, which i would prefer that it not do. Is there a way to keep it in place regardless of whether the top JPanel is occupied or not?
Thanks
What layout managers are you using? The default layout manager for a JPanel is a FlowLayout which render child components one after the other.
Maybe you could set the root JPanel to have a BorderLayout. Then set the top JPanel to the root panel's "top" spot:
JPanel rootPanel = ...;
JPanel topPanel = ...;
rootPanel.add(topPanel, BorderLayout.TOP);
Then set a minimum size for your top JPanel:
topPanel.setMinimumSize(new Dimension(someWidth, someHeight));
And add the second panel to the bottom or middle spot:
rootPanel.add(secondPanel, BorderLayout.CENTER);
Check out http://java.sun.com/docs/books/tutorial/uiswing/layout/border.html
I had time to do an example:
public class TestJpanel extends JFrame {
public TestJpanel() {
this.setLayout( new BorderLayout() );
final JLabel card1 = new JLabel( "card1" );
final JLabel card2 = new JLabel( "card2" );
final JPanel topPanel = new JPanel();
topPanel.setPreferredSize( new Dimension( 1024, 100 ) );
topPanel.add( card1 );
topPanel.add( card2 );
this.add( topPanel, BorderLayout.NORTH );
JPanel centerPanel = new JPanel();
final JButton hideCardsButton = new JButton( "Hide cards" );
hideCardsButton.addActionListener( new ActionListener() {
#Override
public void actionPerformed( ActionEvent e ) {
if ( topPanel.getComponentCount() == 0 ) {
topPanel.add( card1 );
topPanel.add( card2 );
hideCardsButton.setText( "Hide cards" );
} else {
topPanel.removeAll();
hideCardsButton.setText( "Show cards" );
}
topPanel.validate();
topPanel.repaint();
}
} );
centerPanel.add( hideCardsButton );
this.add( centerPanel, BorderLayout.CENTER );
}
public static void main( String[] args ) {
TestJpanel window = new TestJpanel();
window.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
window.setSize( 1024, 768 );
window.setVisible( true );
}
}
Note that this code if full of bad practices but helps demonstrate what I want with a short number of lines.

Categories

Resources