i'm trying to make nested tabs in java . It works but how can i resize my nested tabs ? ( addStudent, addTeacher ) because when it runs, they are very narrow. Thx for help
JPanel students = new JPanel();
JPanel teachers = new JPanel();
JPanel lessons = new JPanel();
JPanel courses = new JPanel();
JPanel addPanel = new JPanel();
JPanel translations = new JPanel();
JPanel addStudent = new JPanel();
JPanel addTeacher = new JPanel();
JButton bAddStudent = new JButton();
//addStudent.setBounds(x, y, width, height);
tp=new JTabbedPane();
tp2 = new JTabbedPane();
Container pane = this.getContentPane();
pane.add(tp);
tp.addTab("Uczniowie",students);
tp.addTab("Nauczyciele",teachers);
tp.addTab("Harmonogram zajec",lessons);
tp.addTab("Kursy",courses);
tp.addTab("Tlumaczenia", translations);
tp.addTab("Panel administratora", addPanel);
tp2.add("Nowy uczen",addStudent);
tp2.add("Nowy nauczyciel",addTeacher);
addPanel.add(tp2);
Change the layout manager of addPanel to a BorderLayout
This will allow tp2 to occupy the entire available space provided by addPanel and tp
I'd also be careful with nested tabs, it becomes very messy and confusing to user very quickly
Related
I'm working with JPanel and BoxLayout and I want to Center it on the window, I have this code:
JPanel jorder = new JPanel();
jorder.setLayout(new BoxLayout(jorder, BoxLayout.Y_AXIS));
jorder.setBorder(BorderFactory.createTitledBorder("Order"));
JLabel jusuari = new JLabel("User name: edetorres");
JLabel jproduct = new JLabel("Product: Tallat");
JButton jserve = new JButton("Serve");
jorder.add(jusuari);
jorder.add(jproduct);
jorder.add(jserve);
And it returns this (only the left panel of the photo):
public UserInterface(){
super(new BorderLayout());
fc = new JFileChooser();
setComponents();
}
public void setComponents(){
//top section
openButton = new JButton("Charger fichier");
openButton.addActionListener(this);
JPanel buttonPanel = new JPanel();
buttonPanel.add(openButton);
//left section
//class panel
JPanel classe = new JPanel();
classes = new JTextArea(25,15);
classes.setMargin(new Insets(5,5,5,5));
classes.setEditable(false);
JScrollPane classeScrollPane = new JScrollPane(classes);
classe.setBorder(new TitledBorder("Classes"));
classe.add(classeScrollPane);
//right section
JPanel right = new JPanel(new BorderLayout());
JPanel right_top = new JPanel(new GridLayout(2,2));
//attribut panel
JPanel attribut = new JPanel();
attributs = new JTextArea(8,19);
attributs.setMargin(new Insets(5,5,5,5));
attributs.setEditable(false);
JScrollPane attributScrollPane = new JScrollPane(attributs);
attribut.setBorder(new TitledBorder("Attributs"));
attribut.add(attributScrollPane);
//function panel
JPanel methode = new JPanel();
methodes = new JTextArea(8,19);
methodes.setMargin(new Insets(5,5,5,5));
methodes.setEditable(false);
JScrollPane methodeScrollPane = new JScrollPane(methodes);
methode.setBorder(new TitledBorder("Methodes"));
methode.add(methodeScrollPane);
//subclass panel
JPanel sousclasse = new JPanel();
sousclasses = new JTextArea(8,19);
methodes.setMargin(new Insets(5,5,5,5));
methodes.setEditable(false);
JScrollPane sousclasseScrollPane = new JScrollPane(sousclasses);
sousclasse.setBorder(new TitledBorder("Sous-classes"));
sousclasse.add(sousclasseScrollPane);
//relation panel
JPanel relation = new JPanel();
relations = new JTextArea(8,19);
relations.setMargin(new Insets(5,5,5,5));
relations.setEditable(false);
JScrollPane relationScrollPane = new JScrollPane(relations);
relation.setBorder(new TitledBorder("Relations"));
relation.add(relationScrollPane);
right_top.add(attribut);
right_top.add(methode);
right_top.add(sousclasse);
right_top.add(relation);
//detail panel
JPanel detail = new JPanel();
details = new JTextArea(5,40);
details.setMargin(new Insets(5,5,5,5));
details.setEditable(false);
JScrollPane detailScrollPane = new JScrollPane(details);
detail.setBorder(new TitledBorder("Détails"));
detail.add(detailScrollPane);
right.add(right_top,BorderLayout.CENTER);
right.add(detail,BorderLayout.SOUTH);
add(buttonPanel, BorderLayout.NORTH);
add(classe, BorderLayout.WEST);
add(right, BorderLayout.CENTER);
}
the layout created from code above is:
You can see the text area of 'sousclass' panel is smaller than other JTextArea with the same parameters(8,19). Could anyone tell me why?
Also, assume that i have some data, I'd like to display class name in class section, once i click one of classes, it displays attributs of that class in the attribut section, how could i realize this function? Thanks。
JPanel sousclasse = new JPanel();
sousclasses = new JTextArea(8,19);
methodes.setMargin(new Insets(5,5,5,5)); // wrong variable
methodes.setEditable(false); // wrong variable
You didn't set the margin on the text area. You are using the wrong variable name.
So in my GUI, I have a JFrame that's a borderlayout. There's a menubar, and some GUI stuff in NORTH and WEST. In CENTER, there is one JLabel. I want it to move to the center (both horizontally and vertically) of the JPanel. How do I do that correctly? I tried box layout and grid layout. One requirement is that I cannot use gridbag layout.
public class NewClass extends JFrame{
public NewClass () {
setVisible(true);
setSize(500,500);
setDefaultCloseOperation (EXIT_ON_CLOSE);
//menubar
JMenuBar bar = new JMenuBar();
JMenu editMenu = new JMenu("Edit");
JMenuItem mItem = new JMenuItem("Cut"); // edit->cut
editMenu.add(mItem);
mItem = new JMenuItem("Copy"); // edit->copy
editMenu.add(mItem);
mItem = new JMenuItem("Paste"); // edit->paste
editMenu.add(mItem);
bar.add(editMenu);
this.setJMenuBar(bar);
//north panel
JPanel topPanel = new JPanel();
this.add(topPanel,BorderLayout.NORTH);
JLabel myLabel = new JLabel ("Label:") ;
topPanel.add(myLabel);
JButton mytopButton = new JButton ("Push Me");
topPanel.add(mytopButton);
//left panel
JPanel leftPanel = new JPanel();
leftPanel.setBorder (new TitledBorder("Commands:"));
leftPanel.setLayout (new GridLayout (10,1));
this.add(leftPanel,BorderLayout.WEST);
JButton myLeftButton1 = new JButton ("Button 1");
leftPanel.add(myLeftButton1);
JButton myLeftButton2 = new JButton ("Button 2");
leftPanel.add(myLeftButton2);
JButton myLeftButton3 = new JButton ("Button3");
leftPanel.add(myLeftButton3);
//center panel
JPanel centerPanel = new JPanel();
this.add(centerPanel,BorderLayout.CENTER);
JLabel mapLabel = new JLabel("Test_String"); //move this to center of JPanel
centerPanel.add(mapLabel);
centerPanel.setBorder (new EtchedBorder(Color.black,Color.black));
centerPanel.setBackground (Color.white);
}
}
Check the API for methods that affect the alignment of the component.
There are methods that affect the alignment of the component within the layout manager and others that affect the alignment of the text within the label itself.
Start by taking a look at the JavaDocs for JLabel
Specifically, JLabel#setHorizontalAlignment and JLabel#setVerticalAlignment
Answered. Thanks everyone.
JPanel centerPanel = new JPanel();
centerPanel.setLayout (new GridLayout ()); //added
this.add(centerPanel,BorderLayout.CENTER);
JLabel mapLabel = new JLabel("Test_String");
mapLabel.setHorizontalAlignment(SwingConstants.CENTER); //added
mapLabel.setVerticalAlignment(SwingConstants.CENTER); //added
centerPanel.add(mapLabel);
centerPanel.setBorder (new EtchedBorder(Color.black,Color.black));
centerPanel.setBackground (Color.white);
So I want the button Select to be above button Back and I don't want them overlapping each other. But when I set them both the PAGE_END, they overlap. How do I get around this?
Here is the code for the problem:
public void methodName() {
JPanel controls = new JPanel(new BorderLayout(5,5));
final CardLayout cl = new CardLayout();
final JPanel panel = new JPanel(cl);
controls.add(panel);
this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEADING));
list = new JList<Object>(objectName);
list.setVisibleRowCount(7);
select = new JButton("Select");
back = new JButton("Back");
select.addActionListener(this);
controls.add(new JScrollPane(list));
controls.add(select, BorderLayout.PAGE_END);
controls.add(back, BorderLayout.PAGE_END);
controls.setBorder(new EmptyBorder(25,25,0,0));
add(controls);
refreshFrame();
}
Here is what it looks like when they are both added in but are overlapping:
This is what I want it to look like:
Any ideas?
Thanks in advance!
Place the 2 JButtons on a new JPanel using GridLayout like this
JPanel basePanel = new JPanel(new GridLayout(0, 1, 0, 3));
basePanel.add(select);
basePanel.add(back);
controls.add(basePanel, BorderLayout.PAGE_END);
GridLayout can provide a vertical gap between buttons in its constructor as shown in your question
create a new jpanel with gridLayout(2,1) then add the two buttons to the jpanel then add the Jpanel to the Jframe
I'm working with GUI in Java and I've made several JDialogs opening one above the other.
I tried to create a JTabbedPane and I have succeed. However, I have to make the JTabbedPane in a JFrame. I've tried but the JPanel opens all blank.
Second of all when I use JFrame (so the new JTabbedPane became operational) that same frame appears behind the previous one.
So my questions are:
How can I create the tabbed pane in a JDialog ?
How do I make the JTabbedPane appear in front of all other frames, if I use JFrame ?
Here's my code, this JFrame opened when I click on a JButton from a previous JDialog
public class AddComponents extends JDialog {
private String[] arr = {"House", "Microgrid", "CSP", "VPP"};
public AddComponents(JDialog pai, String titulo)
{
super(pai, titulo);
frame = new JFrame(titulo);
// Display the window.
frame.setSize(500, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set grid layout for the frame
frame.getContentPane().setLayout(new GridLayout(1, 1));
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
pack();
for (int i = 0; i < arr.length; i++) {
String tmp = arr[i];
tabbedPane.addTab(tmp, makePanel(tmp));
}
frame.getContentPane().add(tabbedPane);
frame.setMinimumSize(new Dimension(getWidth(), getHeight()));
frame.setLocation(pai.getX() + 85, pai.getY() + 25);
frame.setEnabled(true);
}
private JPanel makePanel(String text) {
JPanel p = new JPanel();
//p.setLayout(new GridLayout(0,1));
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
if(text.equals("House"))
{ //CADA UM DOS ifs chama a class correspondente para criar o interface
p1.setLayout(new GridLayout(4, 2));
idLabel = new JLabel("Component ID:");
idText = new JTextField("");
p1.add(idLabel);
p1.add(idText);
maxUsageLabel = new JLabel("Max usage per hour:");
maxUsageText = new JTextField("");
p1.add(maxUsageLabel);
p1.add(maxUsageText);
minUsageLabel = new JLabel("Min usage per hour:");
minUsageText = new JTextField("");
p1.add(minUsageLabel);
p1.add(minUsageText);
averageUsageLabel = new JLabel("Average usage per hour:");
averageUsageText = new JTextField("");
p1.add(averageUsageLabel);
p1.add(averageUsageText);
// emptyLabel = new JLabel("");
saveButton = new JButton("Save");
// p.add(emptyLabel);
p2.add(saveButton);
p.add(p1);
p.add(p2);
}
if(text.equals("Microgrid"))
{
p.setLayout(new GridLayout(5, 2));
outroLabel = new JLabel(" Microgrid");
p.add(outroLabel);
}
if(text.equals("VPP"))
{
p.setLayout(new GridLayout(5, 2));
outroLabel = new JLabel(" VPP");
p.add(outroLabel);
}
if(text.equals("CSP"))
{
p.setLayout(new GridLayout(5, 2));
outroLabel = new JLabel(" CSP");
p.add(outroLabel);
}
return p;
}
}
"How can I create the tabbed pane in a JDialog ?"
same as you would if you added it to a JFrame. There is essentially no difference here whatsoever.
"How do I make the JTabbedPane appear in front of all other frames, if I use JFrame ?"
you don't. You use a JDialog if you want to display a window above other windows.
For creating the JDialog use:
final JDialog dialog = new JDialog();
dialog.add(tabbedPane);
dialog.setVisible(true);
Java applications normally have only one JFrame, so nobody worries about the Z-order. If you like them, you can use JInternalFrame. Here is the tutorial. You can, however, use dialogs instead.