I want to wrap a 'Tab' inside a JTabbedPane with JScrollPane.But i can't find a way to do it.This is my code so far ...
"HomeDelivery.java"
import java.sql.*;
import java.util.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.LineBorder;
class HomeDelivery extends JFrame
{
JTabbedPane menu;
JPanel pizza,pastry,drinks;
public HomeDelivery()
{
setSize(800,800);
setVisible(true);
setLayout(new BorderLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
menu = new JTabbedPane();
menu.addTab("Burger",new Burger());
pizza = new JPanel();
menu.addTab("Pizza",pizza);
pastry = new JPanel();
menu.addTab("Pastry",pastry);
drinks = new JPanel();
menu.addTab("Drinks",drinks);
add(menu);
}
public static void main(String args[])
{
HomeDelivery h = new HomeDelivery();
}
}
"Burger.java"
import java.sql.*;
import java.util.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.LineBorder;
class Burger extends JPanel
{
JFrame f;
//JPanel s;
JPanel p[];
JLabel image[];
JLabel name[];
JLabel price[];
JButton b[];
JScrollPane scroll;
int row;
public Burger()
{
p = new JPanel[40];
image = new JLabel[40];
name = new JLabel[40];
price = new JLabel[40];
b = new JButton[40];
for(int i=0;i<40;i++)
{
p[i] = new JPanel();
p[i].setLayout(new BoxLayout(p[i],BoxLayout.LINE_AXIS));
p[i].add(Box.createRigidArea(new Dimension(100,50)));
image[i] = new JLabel("Image");
b[i] = new JButton("ADD");
name[i] = new JLabel("Burger");
price[i] = new JLabel("150");
p[i].add(image[i]);
p[i].add(name[i]);
p[i].add(price[i]);
p[i].add(b[i]);
add(p[i]);
}
scroll = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setPreferredSize(new Dimension(100,50));
add(scroll);
}
}
At the last of the portion of "Burger.java",i tried to add a JScrollPane,though i know it is not the right way i did it . If i was not extending JPanel in "Burger.java" , i'll able to add a scrollpane by doing this :
"JScrollPane scroll = new JScrollPane(JPanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);"
But,how can i do it in "Burger.java" now ? How can i add a JScrollPane in my "Burger" 'Tab' ? ...
First stop for problems like this is the public Java API documentation. There you would find another constructor of JScrollPane:
http://docs.oracle.com/javase/8/docs/api/javax/swing/JScrollPane.html#JScrollPane-java.awt.Component-int-int-
According to this your problem could be solved by:
scroll = new JScrollPane(p, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
Edit: Don't extend JScrollPane, it is not necessary here.
You can:
Extend JPanel
Add JPanel to JScrollPane through
JScrollPane(Component view, int vsbPolicy, int hsbPolicy)
Add JScrollPane to JTabbedPane
For example:
import java.awt.EventQueue;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
public class TabExample {
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
final JFrame frame = new JFrame("Example");
final JTabbedPane tp = new JTabbedPane();
final BurgerTab burgerTab = new BurgerTab();
final JScrollPane scrollPane = new JScrollPane(burgerTab,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
tp.addTab("Burgers", scrollPane);
frame.getContentPane().add(tp);
frame.pack();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setVisible(true);
});
}
private static class BurgerTab extends JPanel {
BurgerTab() {
setLayout(new BoxLayout(this, SwingConstants.VERTICAL));
final JPanel[] burgers = new JPanel[40];
for(int i = 0; i < burgers.length; i++) {
burgers[i] = new JPanel();
burgers[i].add(new JLabel("Burger #" + (i + 1)));
add(burgers[i]);
}
}
}
}
Related
I want to create something like the image below, it's something simple, I am still newbie
So far I have done this, the problem is that it does't run, what is the problem?, could you help me?
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Checklist extends JFrame {
private JLabel description;
private JButton send;
private JTextField text[];
private JCheckBox cb[];
public Checklist() {
setTitle("Activities");
setSize(400,400);
setupWidgets();
setVisible(true);
}
private void setupWidgets() {
JPanel pn_center = new JPanel(new GridLayout(10,1));
JPanel pn_west = new JPanel(new GridLayout(10,1));
description = new JLabel("List your activities and uncheck the irrelevant ones");
send = new JButton("Send Checklist");
for (int i=0; i<10; i++) {
text[i] = new JTextField();
cb[i] = new JCheckBox();
}
add(description, BorderLayout.NORTH);
add(pn_center, BorderLayout.CENTER);
add(pn_west, BorderLayout.WEST);
add(send, BorderLayout.SOUTH);
for (int i=0; i<10; i++){
pn_center.add(text[i]);
pn_west.add(cb[i]);
}
}
public static void main(String[] args) {
new Checklist();
}
}
You need to create the arrays first:
private JTextField text[] = new JTextField[10];
private JCheckBox cb[]= new JCheckBox[10];
The source code:
package Homework_B05;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Checklist extends JFrame {
private JLabel description;
private JButton send;
private JTextField text[]=new JTextField[10];
private JCheckBox cb[]=new JCheckBox[10];
public Checklist() {
setTitle("Activities");
setSize(400,400);
setupWidgets();
setVisible(true);
}
private void setupWidgets() {
JPanel pn_center = new JPanel(new GridLayout(10,1));
JPanel pn_west = new JPanel(new GridLayout(10,1));
description = new JLabel("List your activities and uncheck the irrelevant ones");
send = new JButton("Send Checklist");
for (int i=0; i<10; i++) {
text[i] = new JTextField();
cb[i] = new JCheckBox();
}
add(description, BorderLayout.NORTH);
add(pn_center, BorderLayout.CENTER);
add(pn_west, BorderLayout.WEST);
add(send, BorderLayout.SOUTH);
for (int i=0; i<10; i++){
pn_center.add(text[i]);
pn_west.add(cb[i]);
}
}
public static void main(String[] args) {
new Checklist();
}
}
I'm trying to populate a Panel with a textfield and a label, the label is being reflecting as expected, however the textfield is not showing up.
Below is the code that is being used:
package qmutility;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class panetest1
{
public static void main(String[] args)
{
createSubframe();
}
public static void createSubframe()
{
final JFrame subframe = new JFrame("Object Choice");
subframe.setSize(1000, 500);
subframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
subframe.getContentPane().setLayout(new GridLayout(1, 1));
JTextArea out = new JTextArea();
out.setEditable (false);
JScrollPane jp = new JScrollPane(out);
jp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
JPanel queue = new JPanel();
JLabel lblqname = new JLabel("Please enter the queue name");
JTextField txtqname = new JTextField(20);
queue.add(lblqname, txtqname);
JPanel chl = new JPanel();
tabbedPane.addTab("Queues", queue);
tabbedPane.addTab("Channels", chl);
subframe.getContentPane().add(tabbedPane);
subframe.getContentPane().add(jp);
tabbedPane.setVisible(true);
subframe.setVisible(true);
}
}
Edit: Attached the screengrabresult
Try changing the Layout of your panel.
Like, queue.setLayout(new FlowLayout());
or Add components to panel one by one,
queue.add(lblqname);
queue.add(txtqname);
The method Container.add(Component comp,Object constraints) adds a given Component with given constraints, it is not meant to add two Component at once.
Replace :
queue.add(lblqname, txtqname);
With
queue.add(lblqname);
queue.add(txtqname);
I have JTextPane, where are inserted 2 JLabels. If any of the labels are clicked, they change content from AAA to clicked.
This code iterates over the elements in the JTextPane:
for(int i = 0; i < tp.getDocument().getLength(); i++) {
System.out.println(((StyledDocument) tp.getDocument()).getCharacterElement(i));
}
How can I access the labels to print "clicked" "AAAA" to the std out?
package texteditor;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;
import javax.swing.JButton;
public class JTextPaneExample extends JPanel {
private JTextPane tp;
public JTextPaneExample() {
setLayout(new BorderLayout(0, 0));
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder(null, "Text Content", TitledBorder.LEADING, TitledBorder.TOP, null, null));
add(panel, BorderLayout.CENTER);
panel.setLayout(new BorderLayout(0, 0));
JScrollPane scrollPane = new JScrollPane();
panel.add(scrollPane, BorderLayout.CENTER);
tp = new JTextPane();
tp.setEditable(false);
scrollPane.setViewportView(tp);
JLabel lbl = new JLabel("AAAA ");
lbl.setOpaque(true);
lbl.setBorder(BorderFactory.createLineBorder(Color.black, 1));
lbl.addMouseListener(new LabelAdapter2(lbl));
tp.insertComponent(lbl);
lbl = new JLabel("BBBB ");
lbl.setOpaque(true);
lbl.setBorder(BorderFactory.createLineBorder(Color.black, 1));
lbl.addMouseListener(new LabelAdapter2(lbl));
tp.insertComponent(lbl);
JButton btnNewButton = new JButton("Write content");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
iterateOverContent(tp);
}
});
panel.add(btnNewButton, BorderLayout.SOUTH);
}
private void iterateOverContent(JTextPane tp2) {
for(int i = 0; i < tp.getDocument().getLength(); i++) {
System.out.println(((StyledDocument) tp.getDocument()).getCharacterElement(i));
}
}
private class LabelAdapter2 extends MouseAdapter {
private JLabel lblNewLabel;
public LabelAdapter2(JLabel lbl) {
this.lblNewLabel = lbl;
}
public void mouseClicked(MouseEvent evt) {
lblNewLabel.setText("clicked");
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("GoBoard");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JTextPaneExample());
frame.setPreferredSize(new Dimension(400, 400));
frame.pack();
frame.setVisible(true);
}
}
i looked into your Question and found a Solution:
just replace your method to iterate the elements of the TextPane with this:
private void iterateOverContent(JTextPane tp2) {
for(int i = 0; i < tp.getDocument().getLength(); i++) {
Element elem = ((StyledDocument) tp.getDocument()).getCharacterElement(i);
AttributeSet as = elem.getAttributes();
if (as.containsAttribute(AbstractDocument.ElementNameAttribute, StyleConstants.ComponentElementName)) {
if(StyleConstants.getComponent(as) instanceof JLabel) {
JLabel myLabel = (JLabel)StyleConstants.getComponent(as);
System.out.println(myLabel.getText());
}
}
System.out.println(((StyledDocument) tp.getDocument()).getCharacterElement(i));
}
}
as you can see, i first save the element into a new variable and then read out all the attributes (yes, the code could be alot shorter, but this way it is more clear - i hope :-) )
After that, we check if the attributes say, that this element is a component.
then the important part: we can get a Component from the attribute set via the StyleConstants.getComponent Method.
Finally just some sanity checks, to see if we can really typecast it to a JLabel.
Best regards,
David
Like this!
And the setOpaque(false); in JTabbedPane didn't work.
Would someone help me please?
part of my code:
for (int x=1; x<6; x++){
newsPanel[x]=new JPanel();
newsPanel[x].add(newspicLabel[x]);
NewsTab.addTab("", Controlbtn, newsPanel[x]);
}
NewsTab.setTabPlacement(JTabbedPane.RIGHT);
NewsTab.setOpaque(false);
thank you very much!!!
I have done the JTabbedPane, but i wanna the tab button placed in the JTabbedPane(Panel) like this ---enter link description here
Here is a sample code.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
public class SidePane extends JPanel {
Color c[] = {Color.RED, Color.CYAN, Color.BLACK, Color.BLUE, Color.YELLOW};
public SidePane() {
setLayout(new BorderLayout(10, 10));
ButtonGroup group = new ButtonGroup();
JToggleButton[] buttons = new JToggleButton[5];
JPanel leftTop = new JPanel(new GridLayout(buttons.length, 1, 10, 10));
leftTop.setOpaque(false);
JPanel left = new JPanel();
left.setOpaque(false);
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JToggleButton(i + "");
group.add(buttons[i]);
leftTop.add(buttons[i]);
buttons[i].addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(SidePane.this, ((JToggleButton)e.getSource()).getText()); // Write whatever you like.
}
});
}
buttons[0].setSelected(true);
left.add(leftTop);
add(left, "East");
setBackground(Color.red);
}
public static void main(String[] args) {
JFrame frame = new JFrame("My Side Pane");
frame.add(new SidePane());
frame.setSize(600, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
I'm fairly new to GUI. I'm trying to make it so that depending on which radio button is selected, a JLabel changes its value. For example, if "id" is selected, it'll display "http://steamcommunity.com/id/" and if "profile" is selected, it'll display "http://steamcommunity.com/profiles/". I have some code up and running and it's nearly complete:
package sgt;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class RadioButtonPrompt extends JPanel
implements ActionListener {
private static final long serialVersionUID = 1L;
static String idString = "ID";
static String profileString ="Profile";
static String type = idString;
public RadioButtonPrompt() {
super(new BorderLayout());
// Create radio buttons.
JRadioButton idButton = new JRadioButton(idString, true);
idButton.setMnemonic(KeyEvent.VK_I);
idButton.setActionCommand(idString);
JRadioButton profileButton = new JRadioButton(profileString);
profileButton.setMnemonic(KeyEvent.VK_P);
profileButton.setActionCommand(profileString);
// Group radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(idButton);
group.add(profileButton);
idButton.addActionListener(this);
profileButton.addActionListener(this);
JPanel radioPanel = new JPanel(new GridLayout(0, 1));
radioPanel.add(idButton);
radioPanel.add(profileButton);
JPanel textPanel = new JPanel ();
JLabel URL = new JLabel(setJLabelValue());
JTextField text = new JTextField("sampletextfield");
text.setPreferredSize(new Dimension(100, 20));
textPanel.add(URL);
textPanel.add(text);
JPanel buttonPanel = new JPanel(new GridLayout(1, 0));
JButton submit = new JButton("Submit");
submit.setMnemonic(KeyEvent.VK_S);
buttonPanel.add(submit);
add(radioPanel, BorderLayout.LINE_START);
add(textPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.PAGE_END);
setBorder(BorderFactory.createCompoundBorder());
}
private String setJLabelValue() {
if (type.equals("ID")) {
return "http://steamcommunity.com/id/";
}
return "http://steamcommunity.com/profiles/";
}
public void actionPerformed(ActionEvent e) {
// Returns either "Profile" or "ID"
type = ((JRadioButton)e.getSource()).getText();
System.out.println(type);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Steam Game Tracker");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new RadioButtonPrompt();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
// Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Take a look at this SO thread.
in actionPerformed() you need to textpanel.setText() to whatever you want based on which button was clicked. I'm guessing at the method name, haven't done any UI stuff with Java for a while.